Home > Back-end >  Java input
Java input

Time:10-14

Want to write a student achievement input but thought about the possible input error input string to determine if a string is suggested to let he again input is under the Numbers you went to consult bigwigs use input method to design train of thought

CodePudding user response:

For example
 Scanner sc=new Scanner (System. In); 
String s="";
While (true) {
System. The out. Println (" please enter the grade: ");
S=sc. NextLine ();
If (s.m atches (" ^ \ \ d + ([.] \ \ d +)? Break $"));//legal data is cycle
}
Double d=double. The valueOf (s);//convert the input to a double
//do your process

CodePudding user response:

reference 1st floor qybao response:
for example
 Scanner sc=new Scanner (System. In); 
String s="";
While (true) {
System. The out. Println (" please enter the grade: ");
S=sc. NextLine ();
If (s.m atches (" ^ \ \ d + ([.] \ \ d +)? Break $"));//legal data is cycle
}
Double d=double. The valueOf (s);//convert the input to a double
//do your process

What these letters mean?

CodePudding user response:

refer to the second floor 7 big Ming reply:
what these letters mean?

Google, baidu, imagine a regular expression

CodePudding user response:

reference qybao reply: 3/f
Quote: refer to the second floor 7 big Ming reply:

What these letters mean?

Google, baidu, imagine the regular expression

hasnext but that seems only I use to judge whether to receive the Numbers do not variables

CodePudding user response:

Hasnext is judgment have the next input data
Another way is to use abnormal
 Scanner sc=new Scanner (System. In); 
Double d=0
While (true) {
System. The out. Println (" please enter the grade: ");
Try {
D=sc. NextDouble ();//if the correct input has quit the loop
break;
The catch (Exception e) {
//do nothing
}
}

  • Related