This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Commandline arg question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Commandline arg question" Watch "Commandline arg question" New topic
Author

Commandline arg question

Dinesh Tahiliani
Ranch Hand

Joined: Aug 06, 2007
Posts: 486
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


Thanks<br />Dinesh
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

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...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Dinesh Tahiliani
Ranch Hand

Joined: Aug 06, 2007
Posts: 486
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

Joined: Aug 03, 2008
Posts: 9291
    
  17

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

Joined: Oct 18, 2004
Posts: 136

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.


SCJP 5.0 and now fighting with ExtJS
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Commandline arg question
 
Similar Threads
Wrapper class variable in switch
xxxValue()
confusing var-arg constructors
overloading
i don't get it!.please somebody help me.