Results 1 to 3 of 3
  1. #1
    wantakill is offline Member
    Join Date
    Sep 2010
    Posts
    8
    Rep Power
    0

    Unhappy Syntax error when returning an array

    Hi,

    I'm totally new to Java. I try to return an array of two strings. Somehow, I can't return the array value. In the suggestion box from java, it suggests that I create a local array value (I already defined this array value in the for loop). Please help me point out the error in my code.

    Thx in advance.

    P.V.

    public String[] AccInformaion(int CustomerAccNumber)
    {
    String[] name = {"X","Y","Z"};
    double[] balance = {1500,10000,10};
    int[] AccNumber = {123,456,789};

    for(int i = 0; i <= (AccNumber.length -1);i++)
    {
    if (CustomerAccNumber == AccNumber[i])
    {
    String customerBalance_S = Double.toString(balance[i]);
    String[] value = {name[i],customerBalance_S};

    }
    else
    {
    ...
    }

    }

    return value;
    }

  2. #2
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    4

    Default

    This is a scope issue. When you exit the if and for statements, the variable 'value' no longer exists as it is local to that part of the code only.

    That is,
    Java Code:
    int a = 5;
    if (true) {
        int b = 6;
        while (false) {
            int c = 7;
            // Here, a, b, and c are accessible.
        }
        // Here, a and b are accessible.
    }
    // Here, a is accessible.
    What you'll want to do is either declare value as an empty String array at the start of the function, or return the value as soon as it is defined.

    More on scope: Variable Scope : Variable Scope**Language**Java Tutorial

  3. #3
    wantakill is offline Member
    Join Date
    Sep 2010
    Posts
    8
    Rep Power
    0

Similar Threads

  1. No more syntax error
    By ideyatech in forum Java Software
    Replies: 2
    Last Post: 04-22-2010, 04:20 AM
  2. Returning An Array
    By elektronika in forum New To Java
    Replies: 2
    Last Post: 12-07-2009, 03:43 PM
  3. stuck on same syntax error....
    By Moltisanti in forum New To Java
    Replies: 2
    Last Post: 09-01-2009, 04:26 AM
  4. Returning array problem.
    By Chase in forum New To Java
    Replies: 4
    Last Post: 10-21-2008, 09:07 PM
  5. syntax error
    By gabriel in forum New To Java
    Replies: 3
    Last Post: 08-03-2007, 03:26 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •