Hi all

I was trying to make it so my small program that gets user input would first ask for a string and then print out the string that the user entered, then it would ask for a number it would read it in as a string and then convert it to an int and print out the number the user entered or then print an error message if a number wasn’t entered.

I tried the try-catch but I can’t get it to work, I have probably missed something out but I have no idea what seen as I have never done this before.
Here is my code
if anyone can give me a hand it would be much appreciated

/**
 * @(#)GetUserInput.java
 *
 *
 * @author 
 * @version 1.00 2008/8/2
 */
import java.io.*;

public class GetUserInput 
{
         private static BufferedReader stdin = new BufferedReader( new InputStreamReader( System.in ) );

        
    
    
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws IOException
    {

     
            System.out.print( "Type some data for the program: " );


            // Read a line of text from the user.
            String input = stdin.readLine();
		


            // Display the input back to the user.
            System.out.println( "input = " + input );
            
            System.out.print( "Type a number for the program: " );

	
            // Read a line of text from the user.
            // then convert it to int but print error message if it goes wrong
             try
	    {
		String number = stdin.readLine();
	    int numberValue = Integer.parseInt(number);	    
            
            // Display the input back to the user.
            System.out.println( "Number Inputted = " + numberValue );
	
	    }
            // i want it to print the error message if if cant convert the string to an int
		catch ( IOException e )
	   {
	System.out.println(e);
      		System.out.println("Please Make suer you enter a number");
           }
   
    }
}

Many thanks

HLA91

Recommended Answers

All 3 Replies

The
int numberValue = Integer.parseInt(number) ;
does not throw a IOException. You are right to use it in the catch since readLine() does throw the IOException, but the parseInt() throws a NumberFormatException:

/**
 * @(#)GetUserInput.java
 *
 *
 * @author 
 * @version 1.00 2008/8/2
 */
import java.io.*;

public class GetUserInput 
{
         private static BufferedReader stdin = new BufferedReader( new InputStreamReader( System.in ) );

        
    
    
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws IOException
    {

     
            System.out.print( "Type some data for the program: " );


            // Read a line of text from the user.
            String input = stdin.readLine();
		


            // Display the input back to the user.
            System.out.println( "input = " + input );
            
            System.out.print( "Type a number for the program: " );

	
            // Read a line of text from the user.
            // then convert it to int but print error message if it goes wrong
             try
	    {
		String number = stdin.readLine();
	    int numberValue = Integer.parseInt(number);	    
            
            // Display the input back to the user.
            System.out.println( "Number Inputted = " + numberValue );
	
	    }
            // i want it to print the error message if if cant convert the string to an int
		catch ( IOException e )
	   {
	System.out.println(e);
      		System.out.println("Error while reading input: " + e.getMessage());
           }
catch ( NumberFormatException e )
	   {
	System.out.println(e);
      		System.out.println("Given input not a number: "+e.getMessage());
           }
   
    }
}
commented: Helpful individual +3

Thankyou, its just how I wanted it and i have now learn't somehting new, thankyou for your time

Just a few tips:

Don't use:

} catch(Exception e)
   System.out.println(e);
}

The above will simply print the .toString() method of the Exception. Use e.getMessage() to print the Exception message to the user.

System.out.println(e.getMessage());

Also for debugging reasons you can e.printStackTrace() .
Assume you have method A() . And inside it you have B1() , B2() , B3() . And each one of them might throw an exception. If when you call A() , you get an exception how will you know which of the above methods (B1, B2, B3), threw that exception? Use 3 different try-catch? No, too much effort and it will not work in most cases. If you write this:

try {
  A();
} catch (Exception e) { 
  e.printStackTrace();
}

The call hierarchy of methods called will be printed. Meaning that it will be printed the method that invoked the exception and which method called that method, and which method called that and so on. If you have too many methods calling others, this is the way to find out where the error happened. As I said it is usually used for debugging. For the programmer to find what went wrong wrong when testing the program. Once you get it to work, you can use the e.getMessage() . It is not pretty for the user to see the stack trace of the exception.

class TestStackTrace {

public static void main(String [] args) {
  try {
     A();
  } catch (Exception e) {
    System.out.println("Exception : "+e.getMessage()); //check what it prints
    e.printStackTrace();
  }
}

static void A() throws Exception {
  B1();
  B2();
  B3();
}

static void B1() throws Exception {
  System.out.println("Method B1");
}
static void B2() throws Exception {
  throw new Exception("Exception invoked at B2");
}
static void B3() throws Exception {
  System.out.println("Method B3");
}
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.