PDA

View Full Version : Java code execution problem


Kassam
09-13-2005, 05:28 PM
I've started learning java few days ago and I have a problem executing this code :

public class IfDemo
{
public static void main(String [] args)
{

int x = Integer.parseInt(args[0]);
double half = 0.0;
if(x!=0)
{
half = x/2.0;
System.out.println(x+"/2="+half);

}
if(x==0)
{
System.out.println("Nothing found.");
}
int y = x*5;
char grade ='F';
if(y >= 85)
{
grade='A';
}

if(y>=70 &&y<85)
grade='c';
System.out.println("y= "+y+" and grade = "+grade);
}

}

It compiles fine ( I use Jcreator) but when I execute it I get this message :

"Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at IfDemo.main(IfDemo.java:7)"

I wrote it exactly as I found it in the book ( "Java in 60 Minutes A Day"), so what went wrong ??

nikkiH
09-13-2005, 06:05 PM
args[0]: the first argument sent when you call it.

How are you calling it?
Should be
java IfDemo 1
or another number

KeZZeR
09-13-2005, 06:48 PM
As NikkiH said, it's down to how you execute the actual program I believe

Kassam
09-13-2005, 07:43 PM
args[0]: the first argument sent when you call it.

How are you calling it?
Should be
java IfDemo 1
or another number

I am not sure if I really understand what you say, but would you please tell me what I should exactly write ?
Thanks.

nikkiH
09-13-2005, 08:03 PM
It's not a problem in the code.

You aren't calling this from the console, are you? You're running it from JCreator.
You need to give the program arguments. It's been awhile since I've used JCreator, but I know there's a way to have it prompt you for command-line arguments to your code. Check the docs.

Most tutorials I have seen assume you are running java from the command line (dos prompt).
You might want to familiarize yourself with doing it that way before you use the IDE, since then you'll know what people mean when they tell you things like "your classpath is wrong" and "what arguments are you passing it?". ;)

Kassam
09-14-2005, 02:36 AM
I tried to execute it using the dos prompt, the same error message appears :(

nikkiH
09-14-2005, 06:17 PM
Works fine for me, so it must be how you are calling it.
Of course, I just HAD to add a try/catch... ;)


public class IfDemo
{
public static void main(String [] args)
{
int x = 0;
try
{
x = Integer.parseInt(args[0]);
double half = 0.0;
if ( x != 0 )
{
half = x/2.0;
System.out.println(x+"/2="+half);
}
else
{
System.out.println("Nothing found.");
}

int y = x*5;
char grade ='F';
if ( y >= 85 )
{
grade='A';
}
else if ( y>=70 &&y<85 )
{
grade='c';
}

System.out.println("y= "+y+" and grade = "+grade);
}
catch (NumberFormatException e)
{
System.out.println("Can't parse " + args[0] + " as int!");
}
catch (ArrayIndexOutOfBoundsException e2)
{
System.out.println("No argument given!");
}
}

}

Invoked as
java IfDemo 12
yields
12/2=6
y=60 and grade=F

Invoked, as was mentioned, with no args, yields the exception you mentioned.
Until, of course, I modified the code a little. :D
Now it says "no argument given".