| Author |
won't let me input integer values
|
Garry Meax
Ranch Hand
Joined: Feb 11, 2005
Posts: 31
|
|
hello ppl. i am new to java. i am using JCreato (Version 3.10.009). thing is when i run this program it wont prompt me to enter the integer values. can anyone of you tell me why. import java.io.*; class max_of_two_numbers { public static void main(String args[]) { int a; int b; try { System.out.println("Enter an integer: "); a = Integer.parseInt(args[0]); System.out.println("Enter an integer: "); b = Integer.parseInt(args[0]); if(a > b) { System.out.println(a + " is the biggest."); } else { System.out.println(b + " is the biggest."); } }catch(Exception e) { System.out.println("Error"); } } }
|
 |
rahul V kumar
Ranch Hand
Joined: May 20, 2003
Posts: 82
|
|
There is no code there to read the values from the command prompt. Please follow the link below to get an understanding of how to prompt the user to enter values from command prompt. http://www.devdaily.com/java/edu/pj/pj010005/pj010005.shtml Hope that helps.
|
 |
Joe Myn
Greenhorn
Joined: Oct 19, 2004
Posts: 25
|
|
|
I faced the same problem using JCreator, I guess that you cannot use the command line with JCreator.
|
 |
Yuliyan Tsarev
Greenhorn
Joined: Feb 11, 2005
Posts: 1
|
|
The way you've wrote this, the numbers a and b must be entered like arguments of the main methode when you're running your program ex. java max_of_two_numbers 6 7 (only if you use a command prompt to run your program), where 6 is a and 7 is b If you really want to be prompted to enter the numbers and read them from the ketboard, you should read that thing posted by Rahul By the way, I suggest to you to switch to blueJ - www.bluej.org which is much more easier to use for a beginner
|
 |
Garry Meax
Ranch Hand
Joined: Feb 11, 2005
Posts: 31
|
|
thanks a lot guys. i have managed to do it but it is quite long. is there a short way for this. import java.io.*; class max_of_two_num { public static void main(String args[]) { DataInputStream a=new DataInputStream(System.in); DataInputStream b=new DataInputStream(System.in); String as=new String(); String bs=new String(); try { System.out.print("Enter an integer: "); as=a.readLine(); System.out.print("Enter an integer: "); bs=b.readLine(); }catch(Exception e) { } int x=Integer.valueOf(as); int y=Integer.valueOf(bs); if(x > y) { System.out.println(x + " is the biggest."); } else { System.out.println(y + " is the biggest."); } } }
|
 |
Ilja Preuss
author
Sheriff
Joined: Jul 11, 2001
Posts: 14112
|
|
Garry, please [url=http://faq.javaranch.com/view?UseCodeTags]add code tags[/code] to your post. You can edit your post using the icon at the top right of it. Thanks! And welcome to the Ranch! [ February 12, 2005: Message edited by: Ilja Preuss ]
|
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
|
 |
Liam Tiarnach
Ranch Hand
Joined: Aug 06, 2004
Posts: 51
|
|
Originally posted by Garry Meax: thanks a lot guys. i have managed to do it but it is quite long. is there a short way for this.
Long ??? That program is tiny... you should would not worry about how long ( or how many lines) your program has at this point. It easier to debug this way and you can clearly see the logic behind your own code ( readability )... but here is a "short" version of your program have fun...
|
- Liam...<br />- ' He who never sleeps... '
|
 |
 |
|
|
subject: won't let me input integer values
|
|
|