View Full Version : Java help! Please, I've been searching...
Invader Z
03-26-2007, 07:00 AM
...forever!? I just need to know what would be the best way to receive console input? Like, what methods and classes do I need to use? Thanks, it's greatly appreciated; I've been searching everywhere and strangely enough I don't think my book even touches on console input...
......I have that's why I'm asking on Yahoo! Answers...
matconco
03-26-2007, 09:00 AM
Try the sun java site.
Logan K
03-26-2007, 10:00 AM
Try using Scanner if you are using java 5.
It would look a bit like:
Scanner scanner = new Scanner (System.in);
java.sun.com (http://java.sun.com/j2se/1.5.0/docs/api/java/util/Scanner.html
http://java.sun.com/j2se/1.5.0/docs/api/)
http://www.exampledepot.com/egs/java.io/ReadFromStdIn.html
http://www.devdaily.com/java/edu/pj/pj010005/pj010005.shtml
you can either use the stream reader or just pass the required attributes as attributes for main method at runtime
Liviawarty J
03-26-2007, 12:30 PM
If you are using j2se 1.5.0 and above
import java.util.Scanner;
.........
Scanner input = new Scanner( System.in );
..........
System.out.print( "Enter a number: ");
int num = input.nextInt();
.......
System.out.print( "Enter a text: ");
String str = input.next();
..........
System.out.print( "Enter a floating point number: ");
double real = input.nextDouble();
..........
If you are using j2se below 1.5.0
import java.io.*;
............
BufferedReader input = new BufferedReader( InputStreamReader( System.in ) );
..........
System.out.print( "Enter name: ");
String name = input.readLine();
..........
System.out.print( "Enter age: ");
int age = Integer.parseInt( input.readLine() );
.........
System.out.print( "Enter average point: ");
double point = Double.parseDouble( input.readLine() );
.......
/* Good LucK */
java.sun.com (http://java.sun.com/j2se/1.5.0/docs/api/java/util/Scanner.html
http://java.sun.com/j2se/1.5.0/docs/api/java/io/BufferedReader.html)
vBulletin® v3.6.8, Copyright ©2000-2012, Jelsoft Enterprises Ltd.