-
November 25th, 2010, 09:16 AM
#1
what is wrong with this code
hi guys what is wrong with the following code:
public class example {
public static final int value = 1;
public int value2 = 2;
public static void showInt (){
int i = value + 2;
System.out.println ("the value is " + value);
System.out.println ("the value of i is " + i);
}
public static void main(String args[]){
int i;
int j;
int k;
if (args.length > 0)
i = Integer.parseInt(args[0]);
if (args.length > 1)
j = Integer.parseInt(args[1]);
k = j * i;
showInt();
System.out.println("the value is " + k);
}
}
-
November 25th, 2010, 11:40 AM
#2
Re: what is wrong with this code
There is only something wrong with it if it doesn't do what you intended it do and as you haven't told us what you expect it to do we can't tell you what is wrong with it.
If it doesn't compile show the compiler messages, if it throws an exception show us the full stack trace, else explain what it is/isn't doing that is wrong.
-
November 25th, 2010, 12:06 PM
#3
Re: what is wrong with this code
i am justing trying to print numbers coming from the arguments variables but the compiler gives me error message as that variable j and i is not initialized. maybe because they are in the if statement block.
-
November 25th, 2010, 01:48 PM
#4
Re: what is wrong with this code
The problem is if one or both of the if statement conditions isn't true then the variable(s) will not be given a value so when you try to multiply the i & j variables one or both of them may not have a value.
The solution is to give the variables a default value before the if statement ie
Code:
int i = 0;
int j = 0;
-
November 25th, 2010, 02:13 PM
#5
Re: what is wrong with this code
thanks thats solved the problem
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|