开发者

What is Difference between using the Scanner class in Java for input and using Strings Args[] with parseint?

开发者 https://www.devze.com 2023-04-01 23:27 出处:网络
What is the difference between the following import java.util.Scanner; //Creating the scanner Scanner input=new Scanner(System.in);

What is the difference between the following

import java.util.Scanner;
//Creating the scanner
Scanner input=new Scanner(System.in);
System.out.println("Enter the number开发者_运维百科 1");
int number1=input.nextInt();

vs

int number1=parseInt(args[0]);


The first is reading from Standard Input the second is reading arguments passed in on the command line. The first is interactive input, the second is a one shot type input.

For the second it is much better to use a library like Java Simple Argument Parser ( JSAP ). Rather than parsing and converting things yourself.


The first one reads an integer from standard input. So you might run it as follows:

$ java Foo 
Enter the number 1               <<-- prompt from program.
42

The second one gets an integer from the command line. So you might run it as follows:

$ java Foo 42
0

精彩评论

暂无评论...
验证码 换一张
取 消