Total members 11891 |It is currently Wed Jun 26, 2024 4:41 am Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





Hi, this is a sample code for taking input from Shell demonstrating string and int input.

I think the class is small enough to be posted as code in the topic
java code
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 !!!");
}

}




_________________
Please recommend my post if you found it helpful


Author:
Beginner
User avatar Posts: 109
Have thanks: 5 time
Post new topic Reply to topic  [ 1 post ] 

  Related Posts  to : Simple code for taking input from shell
 simple code to read properties in java     -  
 not taking items using jquery     -  
 Shell style comments     -  
 How can I call a C program in a Shell Script from Java     -  
 program to run shell command line functions     -  
 small-minimal shell for Linux- functions for builtin command     -  
 input suggestion using Javascript     -  
 Input-Output Operations     -  
 Only numeric input in JTextField     -  
 getting a full line input     -  









Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
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