3

I have created a program as part of my computing work for checking stock and it works fine however, after the user has gone through the program once checking stock of one item, they are asked if they would like to check stock of another item which is where the same line is printed twice and I can't figure out why?. This is the code:

import java.util.*;

public class stock {

public static void main(String[] args) {

    //initialising the scanners

    Scanner stock = new Scanner(System.in);
    Scanner levels = new Scanner(System.in);
    Scanner bar = new Scanner(System.in);
    Scanner choice = new Scanner(System.in);

    //initialising the string variables

    String chocolate;
    String chocolate2;
    String change;
    String choiceb;

    //initialising the integer variables

    int mars = 200;
    int twix = 200;
    int bounty = 200;
    int doubled = 200;
    int galaxy = 200;        
    int change2;        
    int counter = 1;
    int a = 1; 


    //asking the user what chocolate bar they want to check stock of

    System.out.println("Enter the chocolate bar to check stock of: Mars, Twix, Bounty, Double and Galaxy");
    System.out.println("***********************************");
    chocolate = stock.nextLine();
    System.out.println("***********************************");

    //depending on the users choice, this switch statement outputs the appropriate stock level of the bar entered

    switch (chocolate.toLowerCase()) {
        case ("mars"):
            System.out.println("There is currenty " + mars + " in stock");
            break;
        case ("twix"):
            System.out.println("There is currenty " + twix + " in stock");
            break;
        case ("bounty"):
            System.out.println("There is currenty " + bounty + " in stock");
            break;
        case ("double"):
            System.out.println("There is currenty " + doubled + " in stock");
            break;
        case ("galaxy"):
            System.out.println("There is currenty " + galaxy + " in stock");
            break;
        default:
            System.out.println("Your an idiot, try again");
            chocolate = stock.nextLine();
    }

    //the user is then asked if they want to change stock level of any of the chocolate bars

    System.out.println("Do you want to change stock levels?");
    System.out.println("***********************************");
    change = levels.nextLine();
    System.out.println("***********************************");

    //if the answer is yes it carries on with the program and ignores this if statement. if the answer is no, the program closes        

     if (change.equals("no")) {
        System.exit(0);
    }

     //this while loop and switch statement is used to check what chocolate bar stock level the user wants to change. 1 is  subtracted from the counter
     // on the users first input so that the message of checking if the user wants to change any more appears. this 

    while (a == 1){

        if (counter == 0) {
            System.out.println("Do you want to change the stock of any more");
            choiceb = choice.nextLine();
            counter = counter + 1;

        }else{
        System.out.println("Which chocolate do you want to change stock levels of?");
        System.out.println("***********************************");
        chocolate2 = bar.nextLine();
        System.out.println("***********************************");

        switch (chocolate2.toLowerCase()) {
            case ("mars"):
                System.out.println("Enter the amount of Mars Bars currently in stock");
                mars = bar.nextInt();
                System.out.println("There is now " + mars + " in stock");
                counter = counter - 1;

                break;
            case ("twix"):
                System.out.println("Enter the amount of Twix currently in stock");
                twix = bar.nextInt();
                System.out.println("There is now " + twix + " in stock");
                counter = counter - 1;
                break;
            case ("bounty"):
                System.out.println("Enter the amount of Bounty Bars currently in stock");
                bounty = bar.nextInt();
                System.out.println("There is now " + bounty + " in stock");
                counter = counter - 1;
                break;
            case ("double"):
                System.out.println("Enter the amount of Double Bars currently in stock");
                doubled = bar.nextInt();
                System.out.println("There is now " + doubled + " in stock");
                counter = counter - 1;
                break;
            case ("galaxy"):
                System.out.println("Enter the amount of Galaxy currently in stock");
                galaxy = bar.nextInt();
                System.out.println("There is now " + galaxy + " in stock");
                counter = counter - 1;
                break;

        }

    }
    }
}

}

This is the output when the program is ran:

output

Mike G
  • 4,022
  • 9
  • 41
  • 63
haroldj97
  • 57
  • 1
  • 5
  • 5
    Good job for posting this with a short, compilable example. – christopher Nov 03 '14 at 18:57
  • 1
    I think the problem is with 4 different Scanners, all reading from the System.in, add a default statement to your switch-clause and put out chocolate2.toLowerCase(). I could imagine that chocolate2 holds the input "yes" as well, and this is not recognised by the switch clause – Michael Nov 03 '14 at 19:02
  • OK, so can i use one scanner for all user input? – haroldj97 Nov 03 '14 at 19:03
  • Of course you can use one scanner instead of x scanners as long as all read from the same resource (in your case `System.in`) – msrd0 Nov 03 '14 at 19:04
  • @Michael you should answer with that comment. 1 scanner solves the problem – Alexandru Severin Nov 03 '14 at 19:05
  • This is really a duplicate of [Skipping nextLine() after use nextInt()](http://stackoverflow.com/questions/13102045/skipping-nextline-after-use-nextint). The problem is mixing `nextLine` and `nextInt`. – Radiodef Nov 03 '14 at 19:21

3 Answers3

3

The problem is the mix of reading lines and integers:

    System.out.println("Which chocolate do you want to change stock levels of?");
    System.out.println("***********************************");
    chocolate2 = bar.nextLine();
    System.out.println("***********************************");

    switch (chocolate2.toLowerCase()) {
        case ("mars"):
            System.out.println("Enter the amount of Mars Bars currently in stock");
            mars = bar.nextInt();

First, you are reading from bar using nextLine(). The user will enter mars\r\n (\r\n is the linebreak caused by hitting return) and the scanner reads mars\r\n

Then you are reading nextInt() (!) from bar. The user enters 2\r\n, but nextInt() will only read 2, leaving \r\n on the bar scanner, with the cursor just ahead of \r\n.

Your logic enters the second loop, reprints the message, but when your bar scanner hits the nextLine() again, it will just proceed and read \r\n - the switch fails on this and your logic enters the third loop (Message printed second time)

Now, bar is empty again, so bar.readLine() will wait for a user input again.

To fix this issue, make sure you skip the current line, after reading the integer, so when your scanner hits nextLine() again while prompting for the type it will not just consume a linebreak.:

mars = bar.nextInt();
bar.nextLine();
dognose
  • 18,985
  • 9
  • 54
  • 99
0

I think the problem is with 4 different Scanners, all reading from the System.in, add a default statement to your switch-clause and put out chocolate2.toLowerCase(). I could imagine that chocolate2 holds the input "yes" as well, and this is not recognised by the switch clause :)

one scanner should do it

Michael
  • 815
  • 1
  • 6
  • 21
  • Just tried using one scanner and the line doesn't print twice now however, the user doesn't get the choice as to whether they want to check the stock level of any more https://imageshack.com/i/eyaBJA30p – haroldj97 Nov 03 '14 at 19:09
  • are you sure? Pls post your code again, with one scanner. should work then – Michael Nov 03 '14 at 19:13
0

The counter you initialized with 1 in the beginning of your main method, which sets the counter to 1 always and so it is getting printed twice. Try initializing your counter as 0 and then run your code.

vembutech
  • 1,596
  • 1
  • 9
  • 15