CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Nov 2004
    Posts
    32

    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);

    }

    }

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    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.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  3. #3
    Join Date
    Nov 2004
    Posts
    32

    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.

  4. #4
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    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;
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  5. #5
    Join Date
    Nov 2004
    Posts
    32

    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
  •  





Click Here to Expand Forum to Full Width

Featured