read Int and String from Console - Java Language Basics

Java examples for Language Basics:Console

Description

read Int and String from Console

Demo Code


//package com.java2s;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
    public static void main(String[] argv) throws Exception {
        System.out.println(readInt());
    }/*from  w  w  w.  ja v  a 2s . c  o m*/

    private static BufferedReader bufferedReader = new BufferedReader(
            new InputStreamReader(System.in));

    public static int readInt() {
        int i;

        try {
            i = Integer.parseInt(readString());
        } catch (NumberFormatException nfe) {
            i = readInt();
        }

        return i;
    }

    public static String readString() {
        String s = "";

        try {
            s = bufferedReader.readLine();
        } catch (IOException io) {
            s = readString();
        }

        return s;
    }

    public static void writeMessage(String message) {
        System.out.println(message);
    }
}

Related Tutorials