Powered by Blogger.

Java Solution: Console Input/Output Program in Java

Console Output:

System.out.print("Hello ");
System.out.println("world");

Console Input:

BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String text = in.readLine();

Console Input/Output:-

Java 2 SDK support console I/O with three public variables in the java.lang.Syste class:

1. System.out (System.out is a PrintStream object)
2. System.in (System.in is a InputStream object)
3. System.err (System.err is a PrintStream onject)

Following code provides an example of keybord input, which import the java.io.*; package. In line 6, InputStreamReader reads characters and converts the row bytes into Unicode characters. In line 7, BufferedReader provides the readLine() method (in line 10), which enables the program to read from standard input (keyboard) one line at a time. Line 12, close the buffered reader. The readLine() method can throw an I/O exception, so this code is in a try-catch block.

Console Input/Output Program:-

 import java.io.*;

 public class ConsoleInput{
   public static void main(String[]args){
     String s;
     InputStreamReader isr = new InputStreamReader(System.in);
     BufferedReader in = new BufferedReader(isr);
     try{
        System.out.println(”Write something: ”);
        s = in.readLine();
        System.out.println(”Read: ” +s);
        in.close();
     }catch(IOException e){
        e.printStackTrac e();
     }
   }
 }

Console Input-Output Program:-

import java.util.*;

public class IntroScanner {

    public static void main(String[] args) {
        //... Initialization
        String name;                // Declare a variable to hold the name.
        Scanner in = new Scanner(System.in);

        //... Prompt and read input.
        System.out.println("What's your name, Earthling?");
        name = in.nextLine();      // Read one line from the console.

        //... Display output
        System.out.println("Take me to your leader, " + name);
    }
}

Related Tags for Console Input-Output:-

java, c, class, console, ole, scanner, if, ie, e, il, can, li, im, as, m, scan, ca, j, cl, s, so, ha, mpl, ann, cons, sim, util, va, uti, sca, s, s, av, lif, pl, on, ol, o

2 comments:

  1. Came for JavaScript. Got Java. I'm disappointed in you if you don't know the difference between Java and JavaScript.

    ReplyDelete
  2. Pretty good post. I just came across your site and wanted to say that I’ve really enjoyed reading your posts. In any case I’ll be subscribing to your feed and I hope you will keep a good work!Cheer!

    sap online training
    software online training
    sap sd online training
    hadoop online training
    sap-crm-online-training

    ReplyDelete