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