#I don't understand why firstShape() is not working properly

8 messages · Page 1 of 1 (latest)

hard pier
#

So I want to ask the user for two shapes so that they can print whatever they want in a really cool design, basically making it more engaging.

But I don't have a clue why my firstShape() is not working properly

(also for some reason its telling me to upload my code as a file but its not pasting one sec)

#
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

upper steeple
hard pier
upper steeple
# hard pier 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.

rancid hinge
#

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.