Reputation: 33
In this function, I want to get text in the file then convert it from string to decimal values, by add this text to an array apply some loops and put the result to array of integer. While returning this integer array it gives me an error "cannot find symbol variable integerArray"
int[] inputfile () throws Exception{
BufferedReader br = null;
String sCurrentLine;
String message;
br = new BufferedReader(new FileReader("\input.txt"));
while ((sCurrentLine = br.readLine()) != null) {
message=sCurrentLine.toString();
char[] messageArray=message.toCharArray();
int[] integerArray = new int[messageArray.length];
for(int i=0; i<messageArray.length; i++)
integerArray[i] = (int)messageArray[i];
}
return integerArray;
}
How can I solve it?
Update:
I declared variable integerArray
outside the while
loop, but it always returns the null
value, Is there any possible way to return integerArray
as int
values? because Arrays.toString(integerArray)
returns String representation of that array
int[] inputfile () throws Exception{
BufferedReader br = null;
String sCurrentLine;
String message;
br = new BufferedReader(new FileReader("\\input.txt"));
int[] integerArray = null
while ((sCurrentLine = br.readLine()) != null) {
message=sCurrentLine.toString();
char[] messageArray=message.toCharArray();
int[] integerArray = new int[messageArray.length];
for(int i=0; i<messageArray.length; i++)
integerArray[i] = (int)messageArray[i];
}
return integerArray;
Upvotes: 1
Views: 9500
Reputation: 8354
integerArray
is a local variable in the while loop , it's inaccessible outside it. Declare it outside the loop
int[] integerArray =null ;
while ((sCurrentLine = br.readLine()) != null) {
// other code
integerArray = new int[messageArray.length];
// other code
}
Upvotes: 4
Reputation: 6077
Your integerArray
is inside the while
loop. Declare it outside the loop and change its value inside it, then it will work.
Upvotes: 4
Reputation: 393936
int[] integerArray
must be declared before the loop, in order for you to be able to return it after the loop.
int[] inputfile () throws Exception{
int[] integerArray = null;
BufferedReader br = null;
String sCurrentLine;
String message;
br = new BufferedReader(new FileReader("\input.txt"));
while ((sCurrentLine = br.readLine()) != null) {
message=sCurrentLine.toString();
char[] messageArray=message.toCharArray();
integerArray = new int[messageArray.length];
for(int i=0; i<messageArray.length; i++)
integerArray[i] = (int)messageArray[i];
}
return integerArray;
}
Upvotes: 3