Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Commandline arg question

 
Dinesh Tahiliani
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anyone please explain me the commandline argument through wrapper class



its giving me error at //2 canyone explain me this why??
i want to take commandline arg by wrapper class only and than put in map and display it
 
Ankit Garg
Sheriff
Posts: 9528
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dinesh if you want to parse the argument passed to the main method, then you'll have to do this

Integer i = new Integer(args[0]);

And if you want user to enter a number on prompt, then you'll have to use API classes like DataInputStream or Scanner with System.in...
 
Dinesh Tahiliani
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you explain me that with example using datainputstream please

As susgested by you i have written the code using args[0]



output:
Enter args
null
can you explain what is happening here>
 
Ankit Garg
Sheriff
Posts: 9528
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The program that you gave will not work. This is a program to do the same with scanner (it will only work on JDK 5.0 or later)

 
Balagopal Kannampallil
Ranch Hand
Posts: 136
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all, you cannot assign System.in directly like that while making an Integer object because it accepts only accepts a String or an int.

System.in returns the standard input stream which in no way can be directly used to create an Integer object.

You can use a scanner class as suggested by Ankit , or else you read the data using an InputStreamReader chained along with BufferedReader and read the contents from the standard input stream. Something like this:

But when you do this, you should be careful enough to type in only numbers to the command line. If you type anything other than numbers, NumberFormatException will be thrown at Runtime when it tries to convert the value you entered into Integer.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic