0

Im trying to stop a loop that takes integers after typing in the console "End", but i cant seem to find a way to do it.

Scanner scan = new Scanner(System.in);
        int bottles = Integer.parseInt(scan.nextLine()) * 750;
        int cnt = 1;
        int platesTotal;
        int potsTotal;
        int nrPlates = 0;
        int nrPots = 0;
        while(true){
            int plates = scan.nextInt();
            platesTotal = plates * 5;
            if(cnt%3==0) {
                int pots = scan.nextInt();
                nrPots = nrPots + pots;
                nrPlates = nrPlates + pots;
                potsTotal = pots * 15;

                if (bottles < potsTotal + platesTotal) {
                    System.out.println("Not enough detergent, " + (potsTotal + platesTotal - bottles) + " ml. more necessary!");
                    break;
                }
                else
                    if(bottles >= potsTotal + platesTotal) {
                        String enough = scan.nextLine();
                        if (enough.equals("End")) {
                            if (bottles >= potsTotal + platesTotal) {
                                System.out.println("Detergent was enough!");
                                System.out.println(nrPlates + " dishes and " + nrPots + "pots were washed.");
                                System.out.printf("Leftover detergent %d ml.", bottles - potsTotal - platesTotal);
                                break;
                            }
                        }
                    }
            }
            cnt++;
        }

after inputting the string ("End"), it needs to show me the total of dishes and pots, and how much detergent it has left, and if the required amount of detergent is more than the available amount, it needs to show me how much more is needed.

MickeyMoise
  • 161
  • 8
  • 2
    Does this answer your question? [Scanner is skipping nextLine() after using next() or nextFoo()?](https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-or-nextfoo) – Arvind Kumar Avinash May 06 '20 at 18:21
  • 1
    Use `Integer.parseInt(nextLine())` instead of `nextInt()`. – Arvind Kumar Avinash May 06 '20 at 18:21
  • If you want to read single word use `next()` instead of `nextLine()`. If you have to use `nextLine()` consider using `scan.skip("\\R")` before it to ensure that there is no dandling line separators after calling `nextInt()`. – Pshemo May 06 '20 at 18:24

3 Answers3

0

Try this String enough = scan.next(); instead of scan.nextLine();

enter image description here

Ganesh Thorat
  • 193
  • 2
  • 8
0

I would suggest printing messages to console to confirm to the user you want input. Otherwise, it'll just show a blank window. I would also suggest you define a boolean variable to escape the while loop while switching your enough to .next rather than .nextLine. This way, you redefine your boolean as false to break the while loop while also getting the output you desire to control your program output a bit more. This compiled with all entries of 1:

package com.climatedev.test;

import java.util.Scanner;

public class Test {


    public static void main(String[] args) {
        System.out.println("How many bottles?");
        Scanner scan = new Scanner(System.in);
        int bottles = scan.nextInt() * 750;
        int cnt = 1;
        int platesTotal;
        int potsTotal;
        int nrPlates = 0;
        int nrPots = 0;
        boolean run = true;
        while(run){
             System.out.println("How many plates?");
             int plates = scan.nextInt();
             platesTotal = plates * 5;
             if(cnt%3==0) {
                 int pots = scan.nextInt();
                  nrPots = nrPots + pots;
                  nrPlates = nrPlates + pots;
                  potsTotal = pots * 15;

              if (bottles < potsTotal + platesTotal) {
                   System.out.println("Not enough detergent, " + (potsTotal + platesTotal - bottles) + " ml. more necessary!");
                   break;
               }
              else
                  if(bottles >= potsTotal + platesTotal) {
                      System.out.println("Would you like to end the program? (Enter End)");
                      String enough = scan.next();
                  if (enough.equals("End")) {
                      if (bottles >= potsTotal + platesTotal) {
                          System.out.println("Detergent was enough!");
                          System.out.println(nrPlates + " dishes and " + nrPots + "pots were washed.");
                          System.out.printf("Leftover detergent %d ml.", bottles - potsTotal - platesTotal);
                          run = false;
                          System.out.println("Goodbye");
                          break;
                        }
                    }
                }
            }
        cnt++;
         }
    }
}

I also changed your bottles slightly to use nextInt, though that depends on your use. If you're thinking the end user might type something like "The number of bottles are..." that'll be better, but otherwise why not use a simpler nextInt call?

Zach Rieck
  • 223
  • 2
  • 16
0

if you want to use a scanner object for both strings and numbers you must parse the number:

 Scanner scanner = new Scanner(System.in);
 String str = scanner.nextLine();
 int num = Integer.parseInt(scanner.nextLine());

or you can have two scanners(one for strings and another for numbers):

 Scanner strScanner = new Scanner(System.in);
 Scanner numScanner = new Scanner(System.in);
 String str = strScanner.nextLine();
 int num  = numScanner.nextInt();