while ( input.hasNext() ) {
i = input.nextInt();
//System.out.println();
System.out.print("Bla: ");
}
this was just a test app/code. When I hit ctrl+z I just hear the ding Windows sound (aka you're doing it wrong sound <.<) and nothing happens, program continues running normally. I tested it in another app and input.hasNext() does return the corr开发者_运维技巧ect True boolean value, it just won't recognize Ctrl+Z. Using Netbeans 6.9.1 and Windows 7.
CTRL-Z indeed does end input in the console input stream for System.in on a Windows 7 platform.
It helps to understand the way this works. A large number of the Scanner methods, particularly the hasNext... and next... methods, block execution until something shows up in the System.in byte stream being processed by the Scanner object instanced. Once there is input of some kind, the methods will unblock and proceed as they are advertised.
The hasNext... methods will return boolean false if the input was end of input stream (i.e. CTRL-Z in Windows, CTRL-D in UNIX/Linux). If the hasNext... is specific to a primitive or other type, it will return a boolean true only if the input parses to a valid primitive or object type for which the hasNext... tests, otherwise it will return false.
In the case of hasNext(), it is not primitive or object specific, so any input other than an end of input stream will return true. This makes it very useful for the kind of determination this question implies.
The following code works on a Windows 7 Ultimate platform and, instead of nextInt() method, it uses a more general nextLine() method to demonstrate how this works. The nextInt() method restricts the user to integers and will throw an exception if something other than end of input or an integer that is not too big for the int primitive gets entered. This code will echo whatever the user types in until it gets an end of input character.
// ControlZEndInputTestApp.java
// Demonstrate how end of input
// works with Scanner.hasNext() method
import java.util.Scanner;
public class ControlZEndInputTestApp {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
// hasNext() blocks until something appears in the System.in
// byte stream being processed by Scanner or an end of stream
// occurs which is CTRL-Z in Windows and CTRL-D in UNIX/Linux
// When hasNext() unblocks, it returns boolean false
// if the stream ended or boolean true if the input was anything else
// other than an end of input stream
// repeat as long as we don't receive an end of input
// i.e. CTRL-Z on Windows or CTRL-D on UNIX/Linux...
while ( input.hasNext() ) {
System.out.println(input.nextLine());
}
}
}
For those working within an IDE, we all get the same result for Deital training example. The IDE masks the " CTRL z " so it does appear to not work. If you run it from the command line, it will work. Fo the purpose of the training class and allow you to continue with some satisfaction about the rest of the example, replace the "input.hasNext" with "input.hasNextInt". You can try other snippits of code also. When you run it, instead of entering the 'CTRL z ", just enter a character and press the enter key and your example will work.
//loop until user enters the end-of-file indicator
// or in this case . a non numeric character
while ( input.hasNextInt() )
{
grade = input.nextInt(); // read grade
total +=grade; // add grade to total
++gradeCounter; // increment number of grades
// call method to increment appropriate counter
incrementLetterGradeCounter( grade );
} // end while
} // end method inputGrades
The problem that arises here is that input.hasNext()
evaluates true whatever the keyboard input is. The program given above needs to be terminated when the user's input is anything other than an integer number. For this reason you can use the boolean input.hasNextInt()
inside your while(...)
statement. This returns true when the next token is an integer number but returns false when a non integer token is given as input. This is a very straightforward solution (it is also proposed above but maybe is not so clear). Typically you don't want a program to terminate when you press a combination of keys such as CTRL+z or CTRL+d (this is only possible if you run the program from the terminal). The code in this simple solution looks like this:
import java.util.Scanner;
public class StackO_4742445 {
static Scanner input = new Scanner(System.in);
public static void main(String[] args) {
int i;
while ( input.hasNextInt() ) {
i = input.nextInt();
System.out.print("Bla: \n");
}
}
}
A test run would give you this output in NetBeans 8.1
run:
1
Bla:
2
Bla:
3
Bla:
d
BUILD SUCCESSFUL (total time: 8 seconds)
So as can be seen here when you press the letter d or any other key which is not an integer the while loop terminates.
Now if one still wants to use .hasNext()
method inside while(...)
you can
can use an if() {}
statement and break;
the loop as follows
int i;
while ( input.hasNext() ) {
if (input.hasNextInt()) {
i = input.nextInt();
System.out.print("Bla: \n");
}//if ends here
else {break;}
}//while loop ends here
A test run will give the same output but this is a bit ugly in this case, in my opinion...Hope this helps anyone who has the same question.
精彩评论