Results 1 to 3 of 3
- 09-03-2010, 06:16 PM #1
Member
- Join Date
- Sep 2010
- Posts
- 8
- Rep Power
- 0
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;
}
- 09-03-2010, 06:19 PM #2
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,
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.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.
More on scope: Variable Scope : Variable Scope*«*Language*«*Java Tutorial
- 09-03-2010, 06:39 PM #3
Member
- Join Date
- Sep 2010
- Posts
- 8
- Rep Power
- 0
Similar Threads
-
No more syntax error
By ideyatech in forum Java SoftwareReplies: 2Last Post: 04-22-2010, 04:20 AM -
Returning An Array
By elektronika in forum New To JavaReplies: 2Last Post: 12-07-2009, 03:43 PM -
stuck on same syntax error....
By Moltisanti in forum New To JavaReplies: 2Last Post: 09-01-2009, 04:26 AM -
Returning array problem.
By Chase in forum New To JavaReplies: 4Last Post: 10-21-2008, 09:07 PM -
syntax error
By gabriel in forum New To JavaReplies: 3Last Post: 08-03-2007, 03:26 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks