1

I'm trying to make a program that counts the number of steps someone made (the program should stop if the number is bigger or equal to 10000), but I can't seem to find a way to type "Going home" and then input the amount of steps that are necessary to get home.

Scanner scan = new Scanner(System.in);
        int totalSteps = 0;

        while(true)
        {
            int steps = scan.nextInt();
            totalSteps = totalSteps + steps;
            if(totalSteps >= 10000) {
                System.out.println("Goal reached! Good job!");
                break;
            }
           else if(steps < 10000)
            {
                String home = scan.nextLine();
                if(home.equals("Going home"))
                {
                    int extraSteps = scan.nextInt();
                    totalSteps = totalSteps + steps + extraSteps;
                    System.out.println(10000 - totalSteps + " more to reach goal.");
                }
            }
        }
MickeyMoise
  • 161
  • 8

2 Answers2

0

You should have a look at this: Why is nextLine() returning an empty string?

This would work:

else if(steps < 10000) {
                scan.nextLine();
                String home;
                while (!(home = scan.nextLine()).isEmpty()) {
                    if (home.equals("Going home")) {
                        int extraSteps = scan.nextInt();
                        totalSteps = totalSteps + steps + extraSteps;
                        System.out.println(10000 - totalSteps + " more to reach goal.");
                    }
                }
            }
kikeee95
  • 13
  • 2
  • when i use this, it gives an error after i input the extra steps, it doesn't do the calculation corectly and the first part of the program (the one with goal reached no longer happens) is not working – MickeyMoise May 06 '20 at 10:14
0

This seems to be a problem with reading a newline. A simple (yet possible not the best) solution is as follows:

        Scanner scan = new Scanner(System.in);
        scan.useDelimiter("\n");
        int totalSteps = 0;

        while(true)
        {
            int steps = Integer.parseInt(scan.nextLine());
            totalSteps = totalSteps + steps;
            if(totalSteps >= 10000) {
                System.out.println("Goal reached! Good job!");
                break;
            }
            else if(steps < 10000)
            {
                String home = scan.nextLine();
                if(home.equals("Going home"))
                {
                    int extraSteps = Integer.parseInt(scan.nextLine());
                    totalSteps = totalSteps + steps + extraSteps;
                    System.out.println(10000 - totalSteps + " more to reach goal.");
                }
            }
        }

For more details on this problem, you could check out the discussion to a very similar problem here: https://www.daniweb.com/programming/software-development/threads/232053/nextline-after-nextint-or-nextdouble-gives-trouble