Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Java hasNext, Ctrl-Z not working

  1. Feb 4, 2012 #1
    I am using Windows 7, netbeans 7.0.1 and the latest Java package
    I am writing a simple while loop with hasNext() method as its condition, and supposedly the end-of-input indicator for Windows is Ctrl+Z, but that does not work for me. I also tried Ctrl-d, which supposedly works for Macs, unix, etc, but that also does not work.

    Does anyone here know the proper end-of-input indicator?
  2. jcsd
  3. Feb 6, 2012 #2


    User Avatar
    Gold Member

    The 'end' of an input is going to be whatever you program it to be. For example, this loops until the user enters 999 and hits return.

    Code (Text):

        public void readInput(){
            System.out.println("Type 999 and return to end:");
            Scanner input = new Scanner(System.in);
                System.out.println("Type 999 and return to end:");
  4. Feb 10, 2012 #3
    ok but you are using some different methods. I just wanted to know how hasNext() method works..like for example:

    Scanner input = new Scanner(System.in);
    while(input.hasNext() ) {...}

    this loop is supposed to exit once i hit Ctrl-Z for Windows XP and linux and some other systems i think, but for Macs it is Ctrl-D. I have Windows 7, and neither works..I was wondering if someone knows what does work...i tried Ctrl-<other letters> but to no avail

  5. Feb 10, 2012 #4
    note: on other programming forums, i received answers similar to the one above, basically just suggestions to use other methods, or to use sentinel-controlled repetition, etc. Just to prevent that from happening further: I REALLY just want to know about the hasNext() method (and I also was told to just read the java documentation on their website about this method, which I did look at, but it did not help) so if anyone could answer my question directly I would really appreciate it
  6. Feb 10, 2012 #5


    User Avatar
    Gold Member

    Here is the javadoc for Scanner. Scanner is an API to parse input data but it's up to the programmer to tell the code inside the loop what to do.

    There isn't anything in the javadoc about Control-Z causing it to break out of a loop. I don't know why you think that it would work that way.

    Depending on how you started the program, it is possible to kill the entire program at any time by pressing Control-C (from a DOS window). But, that is just a command in DOS that kills the running program. It has nothing to do with any Java class or methods.
  7. Feb 10, 2012 #6
    Thanks for the javadoc, but as I have already indicated, I have already read it and did not find the answer to my question.
    And the reason I think that Ctrl-Z should exit a loop if its condition is the hasNext() method is because my book uses

    "the Scanner method hasNext to determine whether there's more data to input. This method returns the boolean value true if there's more data; otherwise, it returns false. The returned value is then used as the value of the condition in the while statement. Method hasNext returns false once the user types the end-of-file indicator." It continues that: "The end of file indicator is a system-dependent keystroke combination which the user enter to indicate that there's no more data to input....On UNIX/Linux/MAC OS X systems, it is <Ctrl> d; on Windows systems, it is <Ctrl> z"

    This excerpt is from Deitel, "Java How to Program" (2012),but I looked through other beginner's Java books in my library and the hasNext method is introduced in all of the beginner's books, so idk how it is that no one has heard of this method...

    the reason I am being so persistent about this method hasNext() is because the chapter my professor asked us to read contains it, and I really want to actually understand what I am reading and why it is not working on my system
  8. Feb 10, 2012 #7


    User Avatar
    Gold Member

    This appears to be a DOS behavior. According to this excerpt from Deitel Java for Programmers, "On Windows systems, end-of-file can be entered by typing <Ctrl> z."

    However, there is a nearly identical link for their C# book which states the following: "The notation <Ctrl> z means to... when typing in a Command Prompt."

    I use IntelliJ to write my programs and Ctrl-z did nothing. But, when I run my compiled program in a DOS window, it works as they say. If you're using NetBeans to run your program, it's probably doing the same as my IntelliJ and not interpreting Ctrl-z like the Command Prompt does. Try it in a DOS window and see if it works.
  9. Feb 10, 2012 #8


    User Avatar
    Science Advisor
    Homework Helper

    Ctrl-Z is the shortcut for "undo" in any "standard" (or "compliant", or whatever the right term is) Windows application.

    If you are running your Java program within an IDE, it's possible the IDE is trapping the Ctrl-Z and doing nothing, because there are no actions to undo.

    Try reading the input from a file instead of typing it at the terminal. If that works, you know that hasnext() is doing what you expected, so your problem then is "how to signal the end of a file form the keyboard" not "how to use hasnext()".

    Looking at the bigger picture, this is probably a non-problem, because "real world" Java applications don't read data from the user that way.
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook