My if (choice.isEmpty()) {} is not working

时间:2017-04-10 00:02:14

标签: java is-empty

I want to display an error message if the user does not enter a value so I used this. If you could help me to display my error when the user does not enter a value that would be great.

while (!choice.equalsIgnoreCase("y") && !choice.equalsIgnoreCase("n"))
    { //start of while

        if (choice.isEmpty()) {
            System.out.println("Error! This entry is required. Try again."); //display error message when the user enters invalid response


       }


        else {
            System.out.println("Error! Entry must be 'y' or 'n'. Try again."); // display error message when the user enters no response
        }

        System.out.print ("Continue (y/n): "); //promt users for input
        choice = sc.next();
} // End of while

The else part works but not the if. It just shows blank lines when I hit enter and I don't enter anything. Here is the full code for the whole program. It is a batting average app.

import java.util.*;
import java.text.NumberFormat;

public class BattingAverage

{



public static void main(String[] args)
{

System.out.println("Welcome to the Batting Average Calculator.");
System.out.println();

Scanner sc = new Scanner(System.in); // scanner for input
String choice = "y"; // initialize string
while (choice.equalsIgnoreCase("y")) //start of while to continue while the user answers y to the continue prompt below

{

int numberOfTimesAtBat;


System.out.print("Enter Number of times at bat: ");
numberOfTimesAtBat = sc.nextInt();

System.out.println("\n" + "0 = out, 1 = single, 2 = double, 3 = triple, 4 = home run");

   int[] atBats = new int[numberOfTimesAtBat];

   for(int counter = 0; counter < numberOfTimesAtBat; counter++){
    System.out.print("Result for at-bat "+(counter+1)+": ");
    atBats[counter] = sc.nextInt();

} //End of for



int sum = 0;


for (int i=0; i < atBats.length; i++) {

sum += atBats[i];

}
double Avg = sum / atBats.length;



// System.out.print("Batting average:" );
System.out.print("Slugging percent: " + Avg);


System.out.print ("\n" + "Continue (y/n): "); //promt users for input
    choice = sc.next(); //make choice the next input


    while (!choice.equalsIgnoreCase("y") && !choice.equalsIgnoreCase("n"))
    { //start of while

        if (choice.isEmpty()) {
            System.out.println("Error! This entry is required. Try again."); //display error message when the user enters invalid response


       }


        else {
            System.out.println("Error! Entry must be 'y' or 'n'. Try again."); // display error message when the user enters no response
        }

        System.out.print ("Continue (y/n): "); //promt users for input
        choice = sc.next();
} // End of while




} // end of while




} //end of main
} // end of class

3 个答案:

答案 0 :(得分:1)

You just have to change sc.next() to sc.nextLine() before the While loop,

choice = sc.nextLine(); //make choice the next input


    while (!choice.equalsIgnoreCase("y") && !choice.equalsIgnoreCase("n"))
    { //start of while

        if (choice.isEmpty()) {
            System.out.println("Error! This entry is required. Try again."); //display error message when the user enters invalid response


       }


        else {
            System.out.println("Error! Entry must be 'y' or 'n'. Try again."); // display error message when the user enters no response
        }

        System.out.print ("Continue (y/n): "); //promt users for input
        choice = sc.next();
}

答案 1 :(得分:0)

It just shows blank lines when I hit enter and I don't enter anything.

scanner.next() does not read the new line, so you need to replace choice = sc.next(); to choice = sc.nextLine();

答案 2 :(得分:0)

suppose you modify the code like this.

System.out.print ("\n" + "Continue (y/n): "); //promt users for input
        sc.useDelimiter("[;\r\n]+");
        choice = sc.next(); //make choice the next input


            while (!choice.equalsIgnoreCase("y") && !choice.equalsIgnoreCase("n"))
            { //start of while


                if (choice.trim().isEmpty()) {
                    System.out.println("Error! This entry is required. Try again."); //display error message when the user enters invalid response


               }else {
                    System.out.println("Error! Entry must be 'y' or 'n'. Try again."); // display error message when the user enters no response
                }

                System.out.print ("Continue (y/n): "); //promt users for input
                choice = sc.next();
        } /