#I don't understand why firstShape() is not working properly
8 messages · Page 1 of 1 (latest)
public class starryNights
{
//literally magic
private static Scanner keyboard = new Scanner(System.in);
public static void main (String[] args)
{
printIdentification();
printInstructions();
printRows(toAskForRows(), firstShape(), secondShape());
}
public static String firstShape()
{
System.out.println("What is the first out of the two shapes you would like to see: ");
String shape1 = keyboard.nextLine();
System.out.println("Done");
return shape1;
}
public static String secondShape()
{
System.out.println("What is the second shape: ");
String shape2 = keyboard.nextLine();
System.out.println("Doneso");
return shape2;
}
private static void printRows(int num, String shape1, String shape2)
{
int numHold = num;
int i = 0;
while (i < numHold)
{
int j = 0;
while (j < numHold)
{
if(numHold - j > i + 1)
{
System.out.print(shape1);
}
else
{
System.out.print(shape2);
}
j ++;
}
System.out.println();
i ++;
}
}
private static int toAskForRows()
{
boolean isLegit = true;
int num = 0;
System.out.println("Give me a number");
while(isLegit)
{
num = keyboard.nextInt();
if(num > 0)
{
isLegit = false;
}
else
{
System.out.println("Please try again");
System.out.println();
}
}
return num;
}
excuse the printIdentification()
and printInstructions(), its the school stuff
also plz dont fix my code, just tell me what im not getting here, cuz i want to try it out myself.
THANKS!
also if there's a better way to write my code, plz enlighten me its kind of fun writing codes and i want to learn how to write like a GOD
nextInt() doesn't clear the scanner of the current line, so you have to add keyboard.nextLine(); after each input if they're one by one. This might have something to do with why it's not working as you would expect, but I'm not 100% sure as I didn't have time to thoroughly check everything
wait that fixed the problem! ! !
how does that even make sense?
nextInt() is designed to help you read multiple integers in a single line of text, so when you take the next int, there's still the rest of the current line, which would be an empty string if there's nothing left. By adding nextLine(); after reading each number, you allow the user to enter numbers on different lines.
From what I can see, the firstShape() function should prompt the user for input and return the entered string, and the secondShape() function should do the same. Then, in the main method, printRows is called with the number of rows to print and the strings returned by firstShape() and secondShape() as arguments.