logo

Get Java Data From Console

Java Get User Input

Category: JAVA

by terminator

2014-10-11 18:53:38

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class GetConsoleInput {

  public static void main(String[] args) throws IOException {

    int i=0;

    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    System.out.print("Enter String: ");
    String s = br.readLine();
    System.out.print("Enter Integer: ");

    try {
      i = Integer.parseInt(br.readLine());
    } catch(NumberFormatException nfe) {
      System.err.println("Invalid Format!");
      System.exit(0);
    }

    if (s != null && !s.isEmpty()) {
      System.out.println("You entered " + s + " with int " + i);
    } else {
      System.out.println("You need to enter a string!");
    }

  }

}

comments powered by Disqus