| | |
Sample code for taking user input from Shell
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Sample code for taking input from Shell demonstrating string and int input
For simple programs or testing purpose its not always required to take input thru GUI.
For simple programs or testing purpose its not always required to take input thru GUI.
/** * Sample code for taking input from Shell demonstrating * string and int input * For simple programs or testing purpose its not always * required to take input thru GUI. * * @author aj.wh.ca * #link www.swiftthoughts.com */ 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 !!!"); } }
-1
•
•
•
•
For taking input of an integer use this code
import java.io.*;
class inputno
{
public static void main(String args[]) throws IOException
{
int a;
BufferedReader br=new BufferedReader (new InputStreamReader(System.in));
a=Integer.parseInt(br.readLine());
}
}
import java.io.*;
class inputno
{
public static void main(String args[]) throws IOException
{
int a;
BufferedReader br=new BufferedReader (new InputStreamReader(System.in));
a=Integer.parseInt(br.readLine());
}
}
0
•
•
•
•
Whenever you do this:
you keep creating a new BufferedReader every time you call the method.
Not very efficient.
Try and have the BufferedReader as static and then use it form the static methods.
Java Syntax (Toggle Plain Text)
return new BufferedReader(new InputStreamReader(System.in)).readLine();
Not very efficient.
Try and have the BufferedReader as static and then use it form the static methods.
Similar Threads
- Visual Studio 2008 VB.Net Generating Custom Code For A Windows Form From User Input (VB.NET)
- User input css code that doesn't interfere with site's layout (HTML and CSS)
- taking user input and searching a .txt file for matching records need a hint please (C++)
- User input sample code (Java)
- taking input (Java)
| Thread Tools | Search this Thread |
android api applet application array arrays automation awt bidirectional binary birt bluetooth businessintelligence busy_handler(null) chat class classes client code columns component constructor database designadrawingapplicationusingjavajslider draw eclipse editor error errors event eventlistener exception expand fractal game givemetehcodez graphics gui guidancer html ide image inetaddress input integer intellij j2me java javafx javamicroeditionuseofmotionsensor javaprojects jme jni jpanel jtree julia linux list loop map method methods mobile mobiledevelopmentcreatejar myaggfun netbeans newbie oracle parsing plazmic print problem program programming project recursion scanner screen server set sharepoint size smart sms smsspam sort sortedmaps sql string subclass support swing textfield threads time tree unlimited utility webservices windows



