0

How to assign a String from a text file into integer array for Java? I had saved the integer array into a text file, but now how can I retrieve all the integer array from the text file without any modification? I want the integer array retrieved same as before it stored into the text file. Below is part of my code:

BufferedWriter f1 = new BufferedWriter(new InputStreamReader(new FileInputStream("Input.txt")));

int a[ ] = yes(test, test.length); //go to yes method,return value to array  
for(int i = 0; i < a.length; i++)  
{  
    f1.write(a[i]);  
}  
    f1.close( );  

BufferedReader f2 = new BufferedReader(new InputStreamReader(new FileInputStream("Input.txt")));  
String src;  
while((src = f2.readLine( )) != null)  
{  
    String[ ] s = src;  
    int a[ ] = Integer.parseInt(s);//same with before it saved  
    ... ...  
}  

(incompatible types found)
How to reserve the originality of the integer array(a[ ]) after saved and retrieved from the text file? Thanks!

3 Answers 3

4

This looks like homework. Anyway, here's my test code:

// Write integers to a file, each on a different line
BufferedWriter f1 = new BufferedWriter( new FileWriter( new File( "Input.txt" ) ) );
int a[] = new int[]{ 17 , 42 , 37 };
for ( int i = 0 ; i < a.length ; i++ ) {
  f1.write( String.valueOf( a[ i ] ) + "\r\n" );
}
f1.close();

// Read integers from a file, assume each value on a different line
final BufferedReader bufferedReader = new BufferedReader( new FileReader( new File( "Input.txt" ) ) );
String line;
final List< Integer > values = new ArrayList< Integer >();
while ( ( line = bufferedReader.readLine() ) != null ) {
  values.add( Integer.valueOf( line ) );
}
bufferedReader.close();

// Convert List elements to array
final int[] valueArray = new int[ values.size() ];
int counter = 0;
for ( int value : values ) {
  valueArray[ counter ] = value;
  counter++;
}

// Print array values
for ( int value : valueArray ) {
  System.out.println( "value: |" + value + "|" );
}

The output is as follows

value: |17|
value: |42|
value: |37|
3
  • Sorry but what is mean by final List< Integer > values = new ArrayList< Integer >();? Commented Mar 18, 2009 at 9:02
  • This is a List instance holding Integer objects. Please see the collections framework in java for further information. Commented Mar 18, 2009 at 9:47
  • Small hint: A List implements the Iterable interface so you can use it in a new for loop. Commented Mar 18, 2009 at 10:01
0

You need to somehow separate the integers. The simplest solution is to write each one in a separate line. Or use a comma separated list and then use String.split(",")

3
  • Currently he is not writing integers, he is writing characters. Commented Mar 18, 2009 at 8:25
  • That is what I've suggested. Reformat the way the data are being written. Commented Mar 18, 2009 at 8:31
  • Its got nothing to do with format, he is not even writing the valid data. Commented Mar 18, 2009 at 12:58
0

The way you have written the characters is totally wrong, you are writing the character representation of the int. Internally this is written as a char. You need to write your integers like so.

for(int i = 0; i < a.length; i++)
{
   String value = String.valueOf(a[i]);
   f1.println(value);
}

This should be compatible with how you are now reading your integers.

Your Answer

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