hasNext, Ctrl-Z not working
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?
Re: hasNext, Ctrl-Z not working
Post your code, and I may be able to help. I know that ctrl+z is the "Stop" indicator in Terminal/Command Prompt.
Re: hasNext, Ctrl-Z not working
import java.util.Scanner;
public class HasNext {
public static void main(String[] args) {
int i=0;
double grade;
Scanner input = new Scanner(System.in);
while(input.hasNext()) {
System.out.print("input: ");
grade = input.nextDouble();
i++;
}
System.out.print("i = "+i);
}
}
...it never prints the last system.out statement because it never breaks the loop, and Ctrl-Z is supposed to break it.. ive tried in netbeans, eclipse, and command prompt
Re: hasNext, Ctrl-Z not working
Ah, I see, you should try limiting the amount of inputs you can have to say maybe 10, with a for loop, otherwise it probably will never stop.
Re: hasNext, Ctrl-Z not working
well, the point of this program is that I don't know ahead of time how many things I want to input. the hasNext() method is supposed to return false and hence break the loop once Ctrl-Z is typed on windows system, and ctrl-d for macs, unix, etc...but neither of those are working on my windows 7
Re: hasNext, Ctrl-Z not working
Try using..
Code:
while(input >= 0 && input <= 100)
inside the while loop already there.
Re: hasNext, Ctrl-Z not working
what on earth is that? on what level of programming are you?
could someone else please help me???
Re: hasNext, Ctrl-Z not working
Judging by your programming, you are using a grade score system, so you only need 0 - 100. So in order to finish the program, just go outside the boundaries.
Re: hasNext, Ctrl-Z not working
no I am actually trying to average an arbitrary amount of numbers, not just between 0 and 100..the name "grade" was just random...specifically, I want to see how hasNext() works
Re: hasNext, Ctrl-Z not working
if you want to see how hasNext() works, read the API.
Re: hasNext, Ctrl-Z not working
You could maybe prompt them for the number of grades they want to enter first, then loop that many times?
Re: hasNext, Ctrl-Z not working
thats the first place i looked.
unfortunately, they do not say what the end of input indicator is for each operating system
Re: hasNext, Ctrl-Z not working
Bestsanchez, the name "grades" was random, I actually just want to input an arbitrary amount of numbers, so I don't want to use counter controlled repetition, because I don't know how many number I am inputing before I start looping
Re: hasNext, Ctrl-Z not working
Quote:
ive tried in netbeans, eclipse, and command prompt
The code in #3 works fine for me.
Note that Windows command processor may interpret Ctrl+Z as end-of-file, but there's no guarantee that other software will. When you use an IDE then System.in/System.out are commonly not the ordinary console streams, but are redirected to a little window where you see output and can enter input. The IDE may interpret Ctrl+Z as end-of-file, or it may not. Running your code reveals that Eclipse Helios (SR 2) does, and Netbeans 7.0 does not.
Re: hasNext, Ctrl-Z not working
In not work in netbean, it work only in cmd.
Re: hasNext, Ctrl-Z not working
If works fine in Eclipse and in an ordinary terminal as well; I don't know Netbeans though ... (type ^Z followed by <enter>)
kind regards,
Jos