How do I test user input to determine if a character has been entered

+3 Number Double07 · September 25, 2015
I want to test to see if a char has been entered so I can put the input through a switch statement.  I want the input to be in the form of a letter grade, and then I will print something on the screen based on that grade.  However, if the user puts in an integer, I want the program to continue to run until the user enters a char.  I have used a do-while loop to ensure the program runs at least once, but I am unsure how to test for characters.  Is there a hasNext...blah blah for characters?
import java.util.Scanner;

public class ConditionalEx {

?
public static void main(String[] args) {
Scanner gradeinput = new Scanner(System.in);
char i = '\0';
do{
System.out.println("Enter a letter grade: ");
i = gradeinput.next().charAt(0);
}while (sc.gradeinput(".").charAt(0));


//char i = 'A';




switch (i){
case 'A':
case 'a':
System.out.println("Excellent");
break;
case 'B':
case 'b':
System.out.println("Good");
break;
case 'C':
case 'c':
System.out.println("OK");
break;
case 'D':
case 'd':
System.out.println("Poor");
default:
System.out.println("Ridiculous");
break;
}

}

}


Post a Reply

Replies

Oldest  Newest  Rating
0 Number Double07 · September 25, 2015
What is in the while condition is jargon.  I was just testing things out, it doesn't make sense. 
0 Gary Whitney · September 25, 2015
It's kind of ironic that your second post appears as a reply to your first. I thought someone had replied.

It appears to me that you are cutting and pasting code because your while loop is using "sc" which is sort of a standard name for a new scanner object but your code created gradeinput.

The Scanner object i.e. GradeInput blocks the program from going further until an enter key is pressed when using System.in as the source.
The charAt(0) grabs the first character. This is  what you  want to test in your while condition. I am not sure your test condition would even compile.

Here is how I would test:
1. Move the switch logic inside a boolean method called repeat(char test) and pass it the char you got from the scanner.
2. Have the case statement return False but have the default return True.
3. Make the while loop call repeat() e.g. while(repeat(firstChar); // you really ought to use descriptive names for your variables
0 Number Double07 · September 28, 2015
Yes, what is in the while loop is pasted code.  It was looking for how to scan a char, which i have figured out. It is jargon, please ignore it.  I should have taken it out before I posted the code.  What I really want to know is how to test to see if the input entered in the loop is a char.  You know like the hasnextint() does, but for char.  
0 Number Double07 · September 28, 2015
*I mean the condition of the while is jargon.  While(jargon) ... This jargon is what I want to test to see if I have a char or not.  I want my loop to keep running until I have entered a char.  I can do this easily for an int, but I don't know how to do this for char.  I could turn this into a string, but I'm stubborn and since I started with char, I want to achieve this with a char input.  Oh, and variable i is lettergrade.  There, now it's descriptive xD 
0 Gary Whitney · September 28, 2015
Let me try to explain it another way.
Scanner has methods for parsing.
If you read a line from a file,  then the line  has a beginning and an  end.
When you read from the system input i.e. keyboard you don't get the input until the user presses Enter.
At  this time you  may have zero or many characters.

You prompted for a single letter input.
Char(0) is going to rerun the first byte as a ASCII character. This is what you want to pass to the function I described.

If the user cant follow your instructions you want to wipe the input with nextline() and ask again.
It makes no sense to me to  continue to parse the input beyond the first character if I asked for a single letter.

If you are asking if there is a hasChar() function like there is a hasInt(), the answer is I don't see one in the Java API. The hasNext() will wait for something to be typed but it doesn't care if it is a letter, a number, or a String.

If I remember right what you get is an array of bites that can me interpreted as primitive data types or as a String which you can then parse by using tokens e.g. a comma as in a comma separated value (csv).

I am sorry if this doesn't help you. It has been a long time since I used the Scanner class. It is a simple tool, and  beginning students are directed to use it, but you won't use it when you start making GUIs.

It works great if the user provides the input you asked for, but you are left to validate the data before you use it. If you prompt for a number between 1 and the and the user types nine, the program will crash if you assign it to an int data type.
0 Number Double07 · October 28, 2015
This helps.  Thank you Gary.
  • 1

Java / Android Development

107,207 followers
About

Very popular language used to create desktop applications, website applets, and Android apps.

Links
Moderators
Bucky Roberts Administrator