|
| ||||||||||
| Tags: console, input output, java, programming language |
![]() |
| | Thread Tools | Search this Thread |
|
#1
| |||
| |||
| Java - Reading from console
I am learning java I/O. I went through some programs and read some context on it. But truly speaking I could not understand much. So, if anyone can post some I/O examples for me it will be is pleasure. Your help and grow my knowledge and interest in java. Thanks in advance and hopping for your reply. |
|
#2
| ||||
| ||||
| Re: Java - Reading from console
Take a look at this example. This may help you Code: public class InputHandler{
public static void main(String[] args) {
System.out.println("Reading from console.");
double numberFromConsole;
try {
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
String s = br.readLine();
DecimalFormat df = new DecimalFormat();
Number n = df.parse(s);
numberFromConsole = n.doubleValue();
} catch (IOException e) {
numberFromConsole = 0;
} catch (ParseException e) {
numberFromConsole = 0;
}
System.out.println(numberFromConsole );
}
} |
|
#3
| ||||
| ||||
| Re: Java - Reading from console
The above example is good, but you can have a glance at this example. Code: public class Echo {
public static void main(String args[]) throws Exception{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String input = "";
while(true){
System.out.print("ECHO< ");
input = in.readLine();
if ("".equals(input)){
break;
}else
if ("ok".equals(input)){
System.out.println("OK command received: do something �");
System.out.println("ECHO> " + input.toUpperCase());
}
}
} |
|
#4
| ||||
| ||||
| Re: Java - Reading from console
Take a look at this example. Reading from the console is done in a different way Code: import java.util.Scanner;
public class InputExp {
public static void main(String[] args) {
String nm1;
int ag;
Scanner in = new Scanner(System.in);
// Reads a single line from the console
// and stores into nm1 variable
nm1 = in.nextLine();
// Reads a integer from the console
// and stores into ag variable
ag=in.nextInt();
in.close();
// Prints nm1 and ag to the console
System.out.println("Name :"+nm1);
System.out.println("Age :"+ag);
}
}
__________________ The FIFA Manager 2009 PC Game |
![]() |
|
| Thread Tools | Search this Thread |
| |
Similar Threads for: "Java - Reading from console" | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Reading an XML file in Java | Chetna | Software Development | 7 | 13-07-2011 04:31 PM |
| Reading tables in java.io | Miles Runner | Software Development | 5 | 20-01-2010 09:30 AM |
| Java For Reading PDF Files | Sheenas | Software Development | 5 | 18-01-2010 08:43 AM |
| Recovery Console Installation : Error reading information from netmap.inf | Custidio | Operating Systems | 3 | 24-11-2009 05:08 PM |
| Java reading from a file | xqc72 | Software Development | 1 | 20-11-2009 07:20 AM |