1

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;

3 Answers 3

4

Your integerArray is inside the while loop. Declare it outside the loop and change its value inside it, then it will work.

Sign up to request clarification or add additional context in comments.

Comments

4

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
}

1 Comment

The result after applying this is the null value !
3

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;

}

6 Comments

I tried this but the result was "[I@15db9742" , btw my text file contains "Hello"
@SarahMohamed That just means you are printing the array incorrectly. Use System.out.println(Arrays.toString(inputfile())); to print the output of this method.
While trying this I found that it returns me a string value, Is there any possible way to return these values in integer?
@SarahMohamed What do you mean ? Your method returns an integer array.
Yes, it is. but after using Arrays.toString it returns a String value!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.