Sat Jun 09, 2007 9:00 pm
import java.io.*;
public class ShellUtils
{
//get String or simply enter from shell
public static String getStringFromShell(String prompt)
{
try
{
System.out.print(prompt);
return new BufferedReader(new InputStreamReader(System.in)).readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return null ;
}
// get an int. Keep asking until not
public static int getIntFromShell(String prompt)
{
String line = "" ;
int num = 0 ;
while(line.equals(""))
{
line = getStringFromShell(prompt);
try
{
num = Integer.parseInt(line);
}
catch(NumberFormatException e)
{
System.out.println("Error: Invalid number");
line = "" ;
}
}
return num ;
}
// similiar methods can be made for getting char , double etc
public static void main(String args[])
{
String name = ShellUtils.getStringFromShell("Please enter your name ");
int age = ShellUtils.getIntFromShell("Please enter your age ");
System.out.println(name + " is "+ age + " years old !!!");
}
}
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com
Powered by phpBB © phpBB Group.