0

This is my first Java program I've ever written, so go easy on me.

Have the java.util.Scanner command...

Then the command Scanner sc = new scanner(System.in)

then I'm using different words for answers and Java bypasses every other question. However, the code words perfect if I were to answer N or n for everything.

Here's whats happening in the output window of Netbeans when answering y

First we have Cambridge Shingles
Would you like some (Y/N)y
How many would you like? 10
Next we have ArmorPlast Classic
Would you like some (Y/N)Next we have 250 Torch applied cap sheet
Would you like some (Y/N)n
Next we have 2.2mm Torch applied base sheet
Would you like some (Y/N)n
Next we have R5 Poly Iso insulation One Inch
Would you like some (Y/N)n

here's what happens when I enter N or n...

First we have Cambridge Shingles.
Would you like some (Y/N)n
Next we have ArmorPlast Classic torch applied cap sheet.
Would you like some (Y/N)n
Next we have 250 Torch applied cap sheet.
Would you like some (Y/N)n
Next we have 2.2mm Torch applied base sheet.
Would you like some (Y/N)n
Next we have R5 Poly Iso insulation One Inch.
Would you like some (Y/N)n

// Input block for products

        System.out.println("First we have " + shingleName);
        System.out.print("Would you like some " +"(Y/N)");
            String answer = sc.nextLine();
            if (answer.equals("Y") || answer.equals("y")) {
                System.out.print("How many would you like? ");
                int shingleUnits = sc.nextInt();
                costShingleUnits = shingleUnits * shinglePrice;
            } else {
                costShingleUnits = 0;
            }


        System.out.println("Next we have " + appRoll);
        System.out.print("Would you like some " +"(Y/N)");
            String answerOne = sc.nextLine();
            if (answerOne.equals("Y") || answerOne.equals("y")) {
                System.out.print("How many would you like? ");
                int appRollUnits = sc.nextInt();
                costAppRoll = appRollUnits * appRollPrice;
            } else {
                costAppRoll = 0;
            }

        System.out.println("Next we have " + torchIKO);
        System.out.print("Would you like some " +"(Y/N)");
            String answerTwo = sc.nextLine();
            if (answerTwo.equals("Y") || answerTwo.equals("y")) {
                System.out.print("How many would you like? ");
                int torchIKOUnits = sc.nextInt();
                costTorchIKO = torchIKOUnits * torchIKOPrice;
            } else {
                costTorchIKO = 0;
            }

        System.out.println("Next we have " + torchBase);
        System.out.print("Would you like some " +"(Y/N)");
            String answerThree = sc.nextLine();
            if (answerThree.equals("Y") || answerThree.equals("y")) {
                System.out.print("How many would you like? ");
                int torchBaseUnits = sc.nextInt();
                costTorchBase = torchBaseUnits * torchBasePrice;
            } else {
                costTorchBase = 0;
            }

        System.out.println("Next we have " + polyISO);
        System.out.print("Would you like some " +"(Y/N)");
            String answerFour = sc.nextLine();
            if (answerFour.equals("Y") || answerFour.equals("y")) {
                System.out.print("How many would you like? ");
                int polyISOUnits = sc.nextInt();
                costPolyISO = polyISOUnits * polyISOPrice;
            } else {
                costPolyISO = 0;
            }
nhouser9
  • 6,572
  • 3
  • 18
  • 39
  • 1
    probably because nextInt() is not consuming the newline when you press 10 and then enter. So the next nextLine() will consume it. You could probably just add a nextLine() after you get each int to solve this. – nhouser9 Feb 05 '17 at 01:29
  • Can you please explain what "You could probably just add a nextLine() after you get each int to solve this". I don't understand what you're saying. :) – freeloader1969 Feb 05 '17 at 01:42
  • @freeloader1969 When you use a nextFoo() method, the trailing "\n" which is the enter, is actually kept in the scanner. To clean it out, and prevent it from becoming the next input automatically, use sc.nextLine() without setting it equal to anything directly after you input each int. Like so: "int a = sc.nextInt(); sc.nextLine();" – Lupus Nox Feb 05 '17 at 01:49
  • Ok...fixed it. Found the problem on board as well. Thanks for the answer. :) – freeloader1969 Feb 05 '17 at 01:51

0 Answers0