Java Standard Input(Stdin) and Standard Output(Stdout)

Almost every Code challenges require you to read input from stdin (standard input) and write output to stdout (standard output).

One popular way to read input from stdin in Java language is by using the Scanner class and specifying the Input Stream as System.in. Let check it using an example:

Scanner sc = new Scanner(System.in);
String str = sc.next();
int year = sc.nextInt();
scanner.close();

System.out.println("My Motherland is: " + str);
System.out.println("Independent in: " + year);

The code above creates a Scanner object named sc and uses it to read a String and an int. It then closes the Scanner object because there is no more input to read, and prints to stdout using System.out.println(String). So, if our input is

Bangladesh 1971

Our code will print:

My Motherland is Bangladesh
Independent in 1971

Task 1
In this challenge, you must read  integers from stdin and then print them to stdout. Each integer must be printed on a new line. To make the problem a little easier, a portion of the code is provided for you in the editor below.

Input Format

There are  lines of input, and each line contains a single integer.

Sample Input

100
200
300

Sample Output

100
200
300

Solutions

import java.util.*;

public class Solution {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        int b = sc.nextInt();        
        int c = sc.nextInt();

        System.out.println(a);
        System.out.println(b);
        System.out.println(c);
    }
}

Task 2

Read the following data types using Java Scanner class:

Read input as byte.

Read input as short.

Read input as float.

Read input as int.

Read input as long.

Read input as double.

Read input as String.

Sample Input

Read byte value: 
10
Read short value: 
20
Read float value: 
30.25
Read int value: 
40
Read long value: 
50
Read double value: 
60.99
Read String value: 
We are done.

Sample Output

Read byte value is: 10
Read short value is: 20
Read float value is: 30.25
Read int value is: 40
Read long value is: 50
Read double value is: 60.99
Read String value is: We are done.

Solution

import java.util.Scanner;

/**
 *
 * @author Mohammad Rajaul Islam
 */
public class Solution {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Read byte value: ");
        byte readBtye = sc.nextByte();
        System.out.println("Read short value: ");
        short readShort = sc.nextShort();
        System.out.println("Read float value: ");
        float readFloat = sc.nextFloat();
        System.out.println("Read int value: ");
        int readInt = sc.nextInt();
        System.out.println("Read long value: ");
        long readLong = sc.nextLong();
        System.out.println("Read double value: ");
        double readDouble = sc.nextDouble();
        System.out.println("Read String value: ");
        sc.nextLine();
        String readStr = sc.nextLine();
        System.out.println("Read byte value is: " + readBtye);
        System.out.println("Read short value is: " + readShort);
        System.out.println("Read float value is: " + readFloat);
        System.out.println("Read int value is: " + readInt);
        System.out.println("Read long value is: " + readLong);
        System.out.println("Read double value is: " + readDouble);
        System.out.println("Read String value is: " + readStr);

    }

}

 

Happy Coding.

Tagged on:                 

Leave a Reply

X