[英]java Scanner.hasNext() usage
public static void main(String args[]){
Scanner in = new Scanner(System.in);
String a = in.next();
if (in.hasNext()) {
System.out.println("OK")
} else {
System.out.println("error");
}
}
我想要的是:如果用戶鍵入的字符串中包含多個單詞,請打印“確定”。 如果用戶僅輸入一個單詞輸入字符串,則輸出“錯誤”。
但是,它不能很好地工作。 當我輸入單個單詞作為輸入時,它不會顯示“錯誤”,也不知道為什么。
讀一行,然后檢查是否有多個單詞。
String a = in.nextLine();
if( a.trim().split("\\s").length> 1 ){
System.out.println("OK");
} else {
System.out.println("error");
}
如果您有任何新輸入,則您的條件解決為true。 嘗試使用諸如contains(" ")
類的東西來測試您的輸入是否包含空格。 如果要確保輸入不僅包含空格,還包含一些其他字符,請在之前使用trim()
。
hasNext()是一個阻止調用。 您的程序將一直待到有人鍵入字母,然后轉到System.out.println(“ OK”);。 線。 我建議使用將System.in傳遞給構造函數的InputStreamReader,然后讀取輸入並從那里確定其長度。 希望能幫助到你。
如果此掃描程序的輸入中包含另一個令牌,則返回true。 在等待輸入掃描時,此方法可能會阻塞 。 掃描儀不會前進超過任何輸入。
因此,如果只有一個字掃描程序將等待下一個輸入阻止您的程序。
考慮使用nextLine()
閱讀整行,並檢查其中是否包含少量單詞。
您可以像現在一樣執行此操作,但是這次基於您從用戶那里獲得的行中的數據創建Scanner。
您還可以使用line.trim().indexOf(" ") == -1
條件來確定String是否在單詞中間不包含空格。
Scanner#hasNext()
將返回一個布爾值,該布爾值指示whether or not
有more input
只要用戶尚未輸入end-of-file
指示符,hasNext()就會返回true
文件結束指示符是與
system-dependent keystroke
組合,用戶輸入該組合即可指示沒有更多數據可輸入。在UNIX / Linux / Mac OS X上為ctrl + d,在Windows上為ctrl + z
看這個簡單的例子,看看如何使用它
// Fig. 5.9: LetterGrades.java
// LetterGrades class uses the switch statement to count letter grades.
import java.util.Scanner;
public class LetterGrades
{
public static void main(String[] args)
{
int total = 0; // sum of grades
int gradeCounter = 0; // number of grades entered
int aCount = 0; // count of A grades
int bCount = 0; // count of B grades
int cCount = 0; // count of C grades
int dCount = 0; // count of D grades
int fCount = 0; // count of F grades
Scanner input = new Scanner(System.in);
System.out.printf("%s%n%s%n %s%n %s%n",
"Enter the integer grades in the range 0–100.",
"Type the end-of-file indicator to terminate input:",
"On UNIX/Linux/Mac OS X type <Ctrl> d then press Enter",
"On Windows type <Ctrl> z then press Enter");
// loop until user enters the end-of-file indicator
while (input.hasNext())
{
int grade = input.nextInt(); // read grade
total += grade; // add grade to total
++gradeCounter; // increment number of grades
// increment appropriate letter-grade counter
switch (grade / 10)
{
case 9: // grade was between 90
case 10: // and 100, inclusive
++aCount;
break; // exits switch
case 8: // grade was between 80 and 89
++bCount;
break; // exits switch
case 7: // grade was between 70 and 79
++cCount;
break; // exits switch
case 6: // grade was between 60 and 69
++dCount;
break; // exits switch
default: // grade was less than 60
++fCount;
break; // optional; exits switch anyway
} // end switch
} // end while
// display grade report
System.out.printf("%nGrade Report:%n");
// if user entered at least one grade...
if (gradeCounter != 0)
{
// calculate average of all grades entered
double average = (double) total / gradeCounter;
// output summary of results
System.out.printf("Total of the %d grades entered is %d%n",
gradeCounter, total);
System.out.printf("Class average is %.2f%n", average);
System.out.printf("%n%s%n%s%d%n%s%d%n%s%d%n%s%d%n%s%d%n",
"Number of students who received each grade:",
"A: ", aCount, // display number of A grades
"B: ", bCount, // display number of B grades
"C: ", cCount, // display number of C grades
"D: ", dCount, // display number of D grades
"F: ", fCount); // display number of F grades
} // end if
else // no grades were entered, so output appropriate message
System.out.println("No grades were entered");
} // end main
} // end class LetterGrades
輸出將是這樣的
Enter the integer grades in the range 0–100.
Type the end-of-file indicator to terminate input:
On UNIX/Linux/Mac OS X type <Ctrl> d then press Enter
On Windows type <Ctrl> z then press Enter
99
92
45
57
63
71
76
85
90
100
^Z
Grade Report:
Total of the 10 grades entered is 778
Class average is 77.80
Number of students who received each grade:
A: 4
B: 1
C: 2
D: 1
F: 2
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.