File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Soft Skills: The software developer's life manual this week in the Jobs Discussion forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

won't let me input integer values

 
Garry Meax
Ranch Hand
Posts: 31
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 82
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 25
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I faced the same problem using JCreator, I guess that you cannot use the command line with JCreator.
 
Yuliyan Tsarev
Greenhorn
Posts: 1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 31
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14112
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Liam Tiarnach
Ranch Hand
Posts: 51
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic