View Single Post
Old Jan 1st, 2007, 10:34 PM   #3
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 856
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
I fixed up your code. If you have any questions, feel free to ask.

import java.io.*;

public class Life42 {
    public static void main(String[] args) {
        BufferedReader stdIn = new BufferedReader(
            new InputStreamReader(System.in));

        String input;
        int a;

        while (true) {
            try {
                System.out.print("Input a number: ");
                
                input = stdIn.readLine(); // throws IOException on failure
                a = Integer.parseInt(input); // throws NumberFormatException on failure
                
                System.out.println(a);
                
                if (a == 42)
                    System.exit(0);
                
            } catch (NumberFormatException e) {
                System.out.println("Maybe if you enter a number...");
            } catch (IOException e) {
                System.err.println("There was a problem reading your input.");
            }
        }
        
    }
}
titaniumdecoy is offline   Reply With Quote