File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Input in Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Input in Java" Watch "Input in Java" New topic
Author

Input in Java

Mike Shn
Ranch Hand

Joined: May 26, 2001
Posts: 149
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

Joined: Nov 05, 2000
Posts: 761

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


Regards,

Pho
Mike Shn
Ranch Hand

Joined: May 26, 2001
Posts: 149
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

Joined: Sep 29, 2000
Posts: 8521
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:


"JavaRanch, where the deer and the Certified play" - David O'Meara
Mike Shn
Ranch Hand

Joined: May 26, 2001
Posts: 149
Hello
But if I the input is doble( not an integer).. What I have to do than??
Thanks
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
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

Joined: May 26, 2001
Posts: 149
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

Joined: Sep 29, 2000
Posts: 8521
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

Joined: Nov 06, 2000
Posts: 1510
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


Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Input in Java