Link to home
Start Free TrialLog in
Avatar of usmbay
usmbayFlag for United States of America

asked on

Pls, I need your help

the text file:
ABC54102 T FTFTFTTTFTTFTTF TF
DEF56278 TTFTFTTTFTFTFFTTFTTF
ABC42366 TTFTFTTTFTFTFFTTF
ABC42586 TTTTFTTT TFTFFFTF

I need to read from the above text file and store it in array as following
ABC54102 as student id
T FTFTFTTTFTTFTTF TF as student result each letter is separate and consider the blank(if there is) as no answer

Thanks
Avatar of sciuriware
sciuriware

Create a class Data:   { String id; char[] answers }
And then:
    Data[] information.

;JOOP!
Avatar of usmbay

ASKER

I'm new to Java and I need more explanation

The problem I want to new how to read the answer (20char) char by char inclodind the spaces
T FTFTFTTTFTTFTTF TF
TTFTFTTTFTFTFFTTF
TTTTFTTT TFTFFFTF
ASKER CERTIFIED SOLUTION
Avatar of sciuriware
sciuriware

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Mayank S
Or you could just do: char[] answers = line.substring ( 8 ).toCharArray () ;
(it will handle the length of the array for you automatically)
This migh help

http://www.idevelopment.info/data/Programming/java/collections/ArrayListExample.java

The first part is similar to the above and second part will show you how store in an array list and third part will show you how to retrieve them from array list


import java.io.FileReader;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.FileNotFoundException;

import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.ListIterator;
import java.util.Collections;
import java.util.Random;

 
public class FileReadStudentData1
{
  public static void main( String [] args )
  {
//  declare ArrayList to hold student data
                    
        ArrayList alist1 = new ArrayList();


    try
    {
          
   
       FileReader fro = new FileReader( "c:\\Javatest.txt" );
       BufferedReader bro = new BufferedReader( fro );
 
       // declare String variable and read
       String stringRead = bro.readLine( );
 
       while(  stringRead != null ) // end of the file
       {
         
          String ID = stringRead.substring(0,8);
          char[] answers = new char[20];
          // First Part will allow you to read answer by answer
          System.out.println(ID);
          for (int i = 9 ; i< answers.length ; i++)
          {
                answers[i] =  stringRead.charAt(i);
                System.out.println(answers[i]);
                
                
          }
         
          //Second Part will show you to store ID and answers
          //in an ArrayList
          alist1.add(stringRead);
          stringRead = bro.readLine( );  // read next line
       }
       
       //Third Part will allow you retrieve the
       //stored values in  ArrayList
       
       System.out.println("Storing in Array List   " );
       System.out.println("StudentID" + "        " + "Answers" );
       Iterator i = alist1.iterator();
       while (i.hasNext()) {
          System.out.println( i.next());
       }
       
       bro.close( );
    }
 
    catch( FileNotFoundException filenotfoundexxption )
    {
      System.out.println( "Javatest.txt, does not exist" );
    }
 
    catch( IOException ioexception )
    {
      ioexception.printStackTrace( );
    }
  }
}
Kanti, this is perhaps a homework question. Posting full-code for it is actually against the site-rules. That's why sciuriware or I also did not post full-working code.
A grade of C means: answer was scarcely of use.


'JOOP!
The Asker could ask a question about grades (haha).