Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Input in Java

 
Mike Shn
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
  • 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;
 
Pho Tek
Ranch Hand
Posts: 782
Chrome Python Ubuntu
  • Mark post as helpful
  • send pies
  • 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
  • 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).]

 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • 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
  • 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"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • 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
  • 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"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • 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).]
 
Bosun Bello
Ranch Hand
Posts: 1511
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic