Reputation: 817
I'm using Eclipse to run the following simple program ( test scanner ) The inputs are mentioned in the Run Configuration -> Arguments section as
23 98
The program does not terminate - hangs without producing a + b
import java.io.*;
import java.util.*;
public class InputExpt
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
//PrintWriter out = new PrintWriter(System.out);
int a = in.nextInt();
int b = in.nextInt();
in.close();
System.out.println(a+b);
}
}
The program works when I dont use the Scanner utility to read inputs. Using lines below displays 121(a+b)
int a = Integer.parseInt(args[0]); // in.nextInt();
int b = Integer.parseInt(args[1]); //in.nextInt();
What is the issue here ?
Upvotes: 0
Views: 889
Reputation: 279890
There's a difference between program input and program arguments.
When you put run configuration arguments, Eclipse adds them to the java
launcher
java InputExpt 23 98
For you to get console input, you would run and enter the numbers (pressing carriage return when you are done entering what you need to)
> java InputExpt
> 23 98
>
Program arguments are bound as elements to the args
array while program input is streamed in the java process input stream which you can get through System.in
.
Upvotes: 2
Reputation: 1306
1) Scanner in = new Scanner(System.in);
the above code will read from STDIN and you are passing data using command-line arguments
2)
int a = Integer.parseInt(args[0]); // in.nextInt();
int b = Integer.parseInt(args[1]); //in.nextInt();
the code above works as you passed data using cmd-args
Upvotes: 1