tags:

views:

241

answers:

6

I am playing with Java and want to do a simple while loop that keeps going until the users does a <ctrl> z.

I have something like this

public static void main(String[] args) {

    //declare vars
    boolean isEvenResult;
    int num;

    //create objects
    Scanner input = new Scanner(System.in);
    EvenTester app = new EvenTester();

    //input user number
    System.out.print("Please enter a number: ");
    num = input.nextInt();

    while() {

        //call methods
        isEvenResult = app.isEven(num);

        if(isEvenResult) {
            System.out.printf("%d is even", num);
        } else {
            System.out.printf("%d is odd", num);
        }

    }//end while loop

}//end main

I tried while( input.hasNext() ) { ... but the code inside the while loop wouldn't execute.

thanks for the help

+1  A: 

while (num != 'z')

Although if you are expecting a 'z' why are doing input.getInt()?

You may want to check out the Console class too.

Gandalf
I think he wants to end with *control*-Z, thus ending the input stream.
rsp
He changed the question after I answered it unfortunately.
Gandalf
+2  A: 
//input user number
System.out.print("Please enter a number: ");

do {
    try {
     num = input.nextInt();
    } catch (Exception e) {
     break;
    }
    // call methods
    isEvenResult = app.isEven(num);

    if (isEvenResult) {
     System.out.printf("%d is even", num);
    } else {
     System.out.printf("%d is odd", num);
    }
} while (true);

When the user writes something non-numeric, the loop breaks.

True Soft
this is what I was looking for, except it won't let me enter <ctrl> z to exit the loop
Joe
if you want to respond to multiple keys being pressed then it's best to implement KeyBindings. You must listen to what keys are being pressed to achieve what you are asking.
ChadNC
+1  A: 

If you want to loop until the user has to force break via Ctrl+Z, then just do while(true). But you want your nextInt() to be inside the loop, and maybe also your prompting statement.

JRL
A: 

You need to implement KeyBindings. Then you can make the determination to exit based off of what keys were pressed.

ChadNC
A: 

you are doing the input outside the loop,and it will run for only once.

System.out.print("Please enter a number: ");
num = input.nextInt();

Put your above code inside the loop.

Since you are having a system out inside the loop you will also come to know whether the control went inside the loop, obviously it should.

Also, try

while(true)

I wonder if while() alone is working.

Kushal Paudyal
A: 

TrueSoft's solution is dead on. The reasons it may not be working for the asker is are kinda outside the scope of the program.

The program works for me: I'm running it under Linux and enter Ctrl-D as the first thing on a line. Ctrl-D is end-of-file for Linux the same way that Ctrl-Z is for Windows. Program stops dead in its tracks, perfectly.

The Windows console (the black DOS box, whatever you want to call it) has a wrinkle: It reads input line-by-line. It won't see the Ctrl-Z until it's read the line, so it needs an Enter keyin before it will see the Ctrl-Z.

I'm unwilling to fire up Windows just to try this, but my guess is that CTRL-Z followed by the Enter key (just like after the number entries) should cause the program to stop cleanly.

There are system-y ways to make a Java program work on a character-by-character basis so you can handle any characters directly and respond immediately to Ctrl-Z. But that's advanced stuff and doesn't belong in a simple programming exercise like this. I think Ctrl-Z / Enter is an acceptable way to have the program end.

Carl Smotricz