• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Commandline arg question

 
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)

 
Ranch Hand
Posts: 136
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Please do not shoot the fish in this barrel. But you can shoot at this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic