Results 1 to 6 of 6

Thread: Problem with IO

  1. #1
    Venas is offline Member
    Join Date
    Feb 2009
    Posts
    3
    Rep Power
    0

    Default Problem with IO

    Hi,

    I need your help to solve a problem I have.

    I have an array list, using the program I add values to this arraylist. When I exit the program I save all the data to a file like this :

    3
    1;rui;true;true;true;true
    2;teste;true;true;true;true

    The problem is reading from this file.

    I have this peace of code :

    Java Code:
    public void readDataMotorista() {
    		 Motorista funcMotorista=new Motorista();	 
    		 String Data;
    		 
    		 try {
    			 BufferedReader line = new BufferedReader(new FileReader("Data/Motoristas.txt"));
    			 Data=line.readLine();
    			 ID=Integer.parseInt(Data);
    			 for (int i=0;Data!=null;i++) {
    				 Data=line.readLine();
    				 funcMotorista.readData(Data);
    				 motoristas.add(i,funcMotorista);
                                     // motoristas.add(i,funcMotorista);
    				 System.out.println("i: "+i);
    				 
    			 }	 
    		 }
    		 
    		 catch (Exception e) {
    				 
    		 }
    	 }
    That will call this code :

    Java Code:
    public void readData(String line) {
    
    	System.out.println("data: " + line);
    	StringTokenizer token;
    	token = new StringTokenizer(line, ";");
    	
    	ID=Integer.parseInt(token.nextToken());
    	nome=token.nextToken();
    	cMota=Boolean.parseBoolean(token.nextToken());
    	cLigeiros=Boolean.parseBoolean(token.nextToken());
    	cPMercadorias=Boolean.parseBoolean(token.nextToken());
    	cPPassageiros=Boolean.parseBoolean(token.nextToken());
    	
    	System.out.println("nome: " + nome);
    }
    The problem is that when I list all the data in the arraylist the sizer are 2 and both are populated with this line 2;teste;true;true;true;true

    What is wrong with this ? Could you help me please ?

  2. #2
    Venas is offline Member
    Join Date
    Feb 2009
    Posts
    3
    Rep Power
    0

    Default

    After 3 hours thinking on this I found the problem and is so simple.

    The line : Motorista funcMotorista=new Motorista();

    should be inside the for.

    Problem solved.

  3. #3
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    10

    Default

    Good job, and welcome to the wonderful world of programming!

    Please mark this post as solved. Thnx

  4. #4
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

    Default

    Problem solved.
    Not yet ))) I think you shall come back soon for the loop isn't right as for file reading (

  5. #5
    Venas is offline Member
    Join Date
    Feb 2009
    Posts
    3
    Rep Power
    0

    Default

    Webuser,

    It is not right ? Why ?

    I have tested and don't found anything wrong.

    Ok, I have replaced this line :

    motoristas.add(i,funcMotorista);

    with :

    motoristas.add(funcMotorista);

    It is your point ?

    Thanks a lot.

  6. #6
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •