Java hasNext, Ctrl-Z not working


by kahwawashay1
Tags: ctrlz, hasnext, java, working
kahwawashay1
kahwawashay1 is offline
#1
Feb4-12, 08:21 PM
P: 96
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?
Phys.Org News Partner Science news on Phys.org
SensaBubble: It's a bubble, but not as we know it (w/ video)
The hemihelix: Scientists discover a new shape using rubber bands (w/ video)
Microbes provide insights into evolution of human language
Borg
Borg is offline
#2
Feb6-12, 11:56 AM
PF Gold
Borg's Avatar
P: 729
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.

	public void readInput(){
		System.out.println("Type 999 and return to end:");
		Scanner input = new Scanner(System.in);
		while(!input.nextLine().equals("999")){
			System.out.println("Type 999 and return to end:");
		}
	}
kahwawashay1
kahwawashay1 is offline
#3
Feb10-12, 03:28 PM
P: 96
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


Quote Quote by Borg View Post
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.

	public void readInput(){
		System.out.println("Type 999 and return to end:");
		Scanner input = new Scanner(System.in);
		while(!input.nextLine().equals("999")){
			System.out.println("Type 999 and return to end:");
		}
	}

kahwawashay1
kahwawashay1 is offline
#4
Feb10-12, 04:02 PM
P: 96

Java hasNext, Ctrl-Z not working


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
Borg
Borg is offline
#5
Feb10-12, 05:23 PM
PF Gold
Borg's Avatar
P: 729
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.
kahwawashay1
kahwawashay1 is offline
#6
Feb10-12, 05:51 PM
P: 96
Quote Quote by Borg View Post
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.
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
Borg
Borg is offline
#7
Feb10-12, 06:51 PM
PF Gold
Borg's Avatar
P: 729
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.
AlephZero
AlephZero is offline
#8
Feb10-12, 08:06 PM
Engineering
Sci Advisor
HW Helper
Thanks
P: 6,386
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.


Register to reply

Related Discussions
Simple Java class + driver program help. Program isn't working, but should be. Programming & Computer Science 7
Core Java & J2EE & Advance Java Programming & Computer Science 2
Java program isn't working please help Programming & Computer Science 6
Java GUI Programming & Computer Science 3
Help in Java 3D Computing & Technology 0