Results 1 to 4 of 4

Thread: Java - Reading from console

  1. #1
    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.

  2. #2
    Join Date
    Jan 2008
    Posts
    1,521

    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. #3
    Join Date
    Feb 2008
    Posts
    1,852

    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. #4
    Join Date
    May 2008
    Posts
    2,389

    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);
        }
    }

Similar Threads

  1. Reading an XML file in Java
    By Chetna in forum Software Development
    Replies: 7
    Last Post: 13-07-2011, 04:31 PM
  2. Reading tables in java.io
    By Miles Runner in forum Software Development
    Replies: 5
    Last Post: 20-01-2010, 10:30 AM
  3. Java For Reading PDF Files
    By Sheenas in forum Software Development
    Replies: 5
    Last Post: 18-01-2010, 09:43 AM
  4. Replies: 3
    Last Post: 24-11-2009, 06:08 PM
  5. Java reading from a file
    By xqc72 in forum Software Development
    Replies: 1
    Last Post: 20-11-2009, 08:20 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,875,227.88306 seconds with 17 queries