简体   繁体   中英

Java do while not looping

I'm sure this is going to be something dumb, but I cannot get my do while loop to work can someone take look please? I need the user to enter a number, then this is validated in a while loop. Then this number gets added to an arraylist and keeps looping until the user enters "-1". Here is my code:

public void enterValues(Scanner scan, ArrayList<Double> values) {

  do {
    System.out.print("Enter value to convert: £");
    while (!scan.hasNextDouble()) {
      System.out.println("Please enter a double");
      scan.nextLine();
    }

    values.add(scan.nextDouble());
    System.out.print("Value entered. Enter -1 to stop: ");
  }

  while (!scan.next().equals("-1"));

  System.out.println("Values entered. Returning to main menu.");
  mainMenu(scan, values);

This will work for you.

public void enterValues(Scanner scan, ArrayList<Double> values){
    Double dValue= (double) 0;
    do{
        System.out.println("Enter value to convert: £");
        dValue= scan.nextDouble();
        if(dValue != -1)
            values.add(dValue);
    }while(dValue != -1);

    System.out.println("Values entered. Returning to main menu.");
    mainMenu(scan, values);
}

This is similar to the other answer below, but a little more idiomatic. There's no need to check the input value twice. Just check it once, and use break to exit the loop if it's negative.

   public void enterValues(Scanner scan, ArrayList<Double> values) {

        for( ;; ) {
            System.out.print("Enter value to convert: £");
            while (!scan.hasNextDouble()) {
                System.out.println("Please enter a double");
                scan.nextLine();
            }
            double input = scan.nextDouble();
            if( input < 0 ) break;
            values.add( input );
            System.out.println("Value entered. Enter -1 to stop: "+ values );
        }
   }

If you actually need "-1" and not just any negative number, note that it's poor practice to compare doubles with == . Use a tolerances with the difference. Also notice below that I supply my input with the program to make an SSCCE .

public class ScannerTest
{
   private static final String[] testVectors = {
      "123\n456\n890\n-1\n",
   };

   public static void main(String[] args) {
      for (int i = 0; i < testVectors.length; i++) {
         String testInput = testVectors[i];
         Scanner scan = new Scanner(testInput);
         ArrayList<Double> output = new ArrayList<>();
         new ScannerTest().enterValues( scan, output );
         System.out.println( output );
      }
   }

   public void enterValues(Scanner scan, ArrayList<Double> values) {

        for( ;; ) {
            System.out.print("Enter value to convert: £");
            while (!scan.hasNextDouble()) {
                System.out.println("Please enter a double");
                scan.nextLine();
            }
            double input = scan.nextDouble();
            if( compareDouble( input, -1.0, 0.00001 ) ) break;
            values.add( input );
            System.out.println("Value entered. Enter -1 to stop: "+ values );
        }
   }

   private boolean compareDouble( double d1, double d2, double tolerance ) {
      return Math.abs( d1 - d2 ) < tolerance;
   }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM