Thursday, April 14, 2011

How to assign a String from a text file into integer array for Java?

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!

From stackoverflow
  • 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(",")

    ng : Currently he is not writing integers, he is writing characters.
    kgiannakakis : That is what I've suggested. Reformat the way the data are being written.
    ng : Its got nothing to do with format, he is not even writing the valid data.
  • 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.

  • 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|
    
    dhiller : This is a List instance holding Integer objects. Please see the collections framework in java for further information.
    dhiller : Small hint: A List implements the Iterable interface so you can use it in a new for loop.

0 comments:

Post a Comment

Note: Only a member of this blog may post a comment.