Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Register Tags Active Topics RSS Search Mark Forums Read

Sponsored Links



Java - Reading from console

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 09-11-2009
Member
 
Join Date: Nov 2009
Posts: 330
Java - Reading from console
  

Hi friends
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.

Reply With Quote
  #2  
Old 09-11-2009
Member
 
Join Date: Jan 2008
Posts: 1,515
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 );
}
}
Reply With Quote
  #3  
Old 09-11-2009
Member
 
Join Date: Feb 2008
Posts: 1,848
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());
        }
    }
}
Reply With Quote
  #4  
Old 09-11-2009
Member
 
Join Date: May 2008
Posts: 2,383
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
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


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 05:31 PM
Reading tables in java.io Miles Runner Software Development 5 20-01-2010 10:30 AM
Java For Reading PDF Files Sheenas Software Development 5 18-01-2010 09:43 AM
Recovery Console Installation : Error reading information from netmap.inf Custidio Operating Systems 3 24-11-2009 06:08 PM
Java reading from a file xqc72 Software Development 1 20-11-2009 08:20 AM


All times are GMT +5.5. The time now is 08:58 PM.