2

I see that many people use java.lang.Scanner.

Scanner sc = new Scanner(System.in);
int i = sc.nextInt();

But, I mentioned in some book the code:

class Guess {
  public static void main(String args[])
  throws java.io.IOException {
    char ch, answer = 'K';
    System.out.println("I'm thinking of a letter between A and Z.");
    System.out.print("Can you guess it: ");
    ch = (char) System.in.read(); // read a char from the keyboard
    if(ch == answer) System.out.println("** Right **");
  }
}

I just wanted to know whats the difference between using the first example and using System.in.read();

Thank you! Also do you need throws java.io.IOException when you want to add user input in your program? What does it do?

TurtleSoup
  • 29
  • 2
  • http://stackoverflow.com/questions/22708071/system-in-read-method Check this link. You may find something helpful – Wololo Aug 30 '15 at 09:03

4 Answers4

1

This reads an integer

int i = sc.nextInt();

e.g. if you type

1234

it will return the int value 1234

This read a character for each key you pressed

char ch = (char) System.in.read();
char ch2 = (char) System.in.read();
char ch3 = (char) System.in.read();
char ch4 = (char) System.in.read();
char ch4 = (char) System.in.read();

and if you type

1234<enter>

it will have

ch == '1'; // == 49
ch2 == '2'; // == 50
ch3 == '3'; // == 51
ch4 == '4'; // == 52
ch5 == '\n'; // == 10
Peter Lawrey
  • 498,481
  • 72
  • 700
  • 1,075
0

Since your guess would have to be a character, inputting a character using the first example would throw java.util.InputMismatchException, and basically scanners suck in reading single characters, with that said even the second solution has its own flaws and wont work try the input Katastrophe, and for the IOException, the method System.in.read() throws it, so you either catch it or throw it yourself!

Community
  • 1
  • 1
QuakeCore
  • 1,661
  • 2
  • 13
  • 29
0
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();

In above case Here we create object of scanner (i.e. sc) and sc.nextInt() get the input from user so if user enters an integer value suppose '3' then this value is stored to variable a;

In second case ch = (char) System.in.read(); here System.in.read() read a character for key you pressed and store that into variable ch. You can also use scanner to read character or string like this sc.next(); and if you have to read string with spaces (e.g. "hello there !!!") then you can use sc.nextLine()

Abhijit Kumbhar
  • 822
  • 3
  • 17
  • 43
0

The Scanner.next() and the System.in.read() are just several approaches to get data to a program from an "outer world" (file system, console, etc).
The System.in.read() reads only single character when the java.lang.Scanner is able to read not only lone symbol, but a whole line.

Scanner sc = new Scanner(System.in);

In the example above "sc" uses System.in.read() in its methods.
The main difference between your examples is that in the first example Scanner invokes System.in.read() itself, but in the last one a programmer invokes it explicitly.
Java I/O is an immensely big topic. You'd better to delve deeper to understand all conceptions. There are some packages which contain IO features: java.io, java.nio

Most IO methods throw the IOException. This one is a checked exception, so you must either catch it within a try-catch block or add to a method signature after 'throws' keyword.