• 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
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Input in Java

 
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello
I want to write the java, which allow to me to input from Command line two inputs.
For exampe:
class Hello {
puiblic static void main(String arg[])
{
}
java Hello 2 3
can I change the parameters from String arg[] to int a[], int[].
In other words, how the input program in java works.
For example in C++, I wrote
cin >> x;
 
Ranch Hand
Posts: 782
Python Chrome Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

can I change the parameters from String arg[] to int a[], int[].


No you may not! That's the public entry point for your class.
You need to call Integer.parseInt( arg[i] ) to convert them to ints.
Pho

[This message has been edited by Pho Tek (edited October 11, 2001).]
 
Mike Shn
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot
Can I give to the user,before he or she enter the input some warning like "You can input just two integers" something like that
Thanks a lot

Originally posted by Pho Tek:
No you may not! That's the public entry point for your class.
You need to call Integer.parseInt( arg[i] ) to convert them to ints.
Pho

[This message has been edited by Pho Tek (edited October 11, 2001).]


 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
These are two different approaches.
Either you can take input parameters when you invoke the program and read them with the args[] array, in which case the user has to type the input BEFORE the program is fired up and therefore can not give any messages.

OR you can have the user start the program with NO input parameters, and then in your main method you can print out your message, and wait for user input using the System.in stream:
 
Mike Shn
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello
But if I the input is doble( not an integer).. What I have to do than??
Thanks
 
Cindy Glass
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It will come in as a String, and you will need to convert it.
Use
double myDouble = Double.parseDouble(string);
for instance
Double.parseDouble(args[0]);

You will still need to put it in a try/catch block.
 
Mike Shn
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Double.parseDouble(args[0])
give me the error:
Mandelbrot.java:7: Method parseDouble(java.lang.String) not found in class java.
lang.Double.
double cu = Double.parseDouble(arg[0]);
 
Cindy Glass
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What version of java are you using? This method has been available since jdk1.2.

[This message has been edited by Cindy Glass (edited October 18, 2001).]
 
Ranch Hand
Posts: 1514
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you are getting that error, you may not be using jdk 1.2 and above. I believe the parseDouble method is new with jdk 1.2
Bosun
 
Look! I laid an egg! Why does it smell like that? Tiny ad, does this smell weird to you?
New web page for Paul's Rocket Mass Heaters movies
https://coderanch.com/t/785239/web-page-Paul-Rocket-Mass
reply
    Bookmark Topic Watch Topic
  • New Topic