String while loop

情到浓时终转凉″ 提交于 2019-12-11 10:18:33

问题


I need to check if the user enter a number(double) or string, everything works perfect except the last part. If the user enters "hello", the program will ask to enter the valid number but it doesn't work properly. It gives me an infinite loop unless I will enter space " ".

Here is my code:

double bill = 0.0;
System.out.print("Please enter the total amount of your bill > ");
  String strAmount = keysIn.nextLine();
  try {
    bill = Double.parseDouble(strAmount);
    while (bill < 0) {
        System.out.print("Your bill amount is less then 0, try again > ");
        bill = keysIn.nextDouble();
      }
  } catch(NumberFormatException e) {
      while (!strAmount.isEmpty()) {
        System.out.print("Enter a valid number > ");
        strAmount = keysIn.nextLine();
      }
  }

Thanks.


回答1:


Try using Scanner.nextDouble() all the time instead of once. It will only accept Doubles.

double bill = 0.0;
System.out.print("Please enter the total amount of your bill > ");
bill = keysIn.nextDouble();
while (bill < 0) {
    System.out.print("Your bill amount is less then 0, try again > ");
    bill = keysIn.nextDouble();
}



回答2:


Your infinite loop is due to while (!strAmount.isEmpty()), this means it will loop as long as strAmount is not empty, so remove that ! and move the check to the end of the loop.

        do {
            System.out.print("Enter a valid number > ");
            strAmount = keysIn.nextLine();
        } while (strAmount.isEmpty());



回答3:


You can try this:

double bill = 0.0;
    System.out.print("Please enter the total amount of your bill > ");
      String strAmount = keysIn.nextLine();
      boolean validated = false;

      //Validate
      while(!validated){
          try{
              bill = Double.parseDouble(strAmount);
              validated = true;
          }
          catch(NumberFormatException e) {
              System.out.print("Enter a valid number > ");
              strAmount = keysIn.nextLine();
          }
      }

      //Continue...
      while (bill < 0) {
        System.out.print("Your bill amount is less then 0, try again > ");
        bill = keysIn.nextDouble();
      }



回答4:


You have two while loops checking different conditions. Try this:

double bill = 0.0;
System.out.print("Please enter the total amount of your bill > ");
String strAmount = keysIn.nextLine();
String numberString = strAmount.trim();

boolean invalidNumber = false;

 try {
   bill = Double.parseDouble(numberString);
 } catch(NumberFormatException e) {
   Systeout.print("Enter a valid number...");
   invalidNumber = true;
 }



while (bill < 0) {
    if(invalidNumber){
        System.out.print("Enter a valid number...");
    } else {
        System.out.println("Your bill amount is less then 0, try again...");
    }
    strAmount = keysIn.nextLine();
    numberString = strAmount.trim();

    try {
        bill = Double.parseDouble(numberString);
        invalidNumber = false;
    } catch(NumberFormatException e) {
        invalidNumber = true;
    }
  }

}



来源:https://stackoverflow.com/questions/35203704/string-while-loop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!