Results 1 to 6 of 6
  1. #1
    Cruz182 is offline Member
    Join Date
    Jun 2011
    Posts
    3
    Rep Power
    0

    Default Trying to read a csv file!

    hi ! here's my problem, I am trying to read a csv file, but unfortunatly my program only read the first line of the file, here's my code, I tried everything dont know what to do.

    The point is ready line by line and each line got two diferent numbers and its to store then bot in a object, and then each line will correspond to an object and save each one in a linkedList.

    Java Code:
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.ArrayList;
    import java.util.LinkedList;
    import java.util.Scanner;
    
    
    public class CSVLeitor {
        public static LinkedList<Aluno> Leitor(File f) throws FileNotFoundException {
    
            LinkedList<Aluno> L = new LinkedList<Aluno>();
            // do ficheiro f argumento do metodo
            Scanner lineScan = new Scanner(f);
            // 
            Scanner s = new Scanner(lineScan.nextLine());
            s.useDelimiter(";");
            
            while (s.hasNextLine()) {
                String stringa1 = s.next();
                String stringa2 = s.next();
                
                int a = Integer.parseInt(stringa1);
                int b = Integer.parseInt(stringa2);
                Aluno pupil = new Aluno(a,b);
                L.add(pupil);
            }
            return L;
        }
    }
    my main

    Java Code:
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.LinkedList;
    
    
    class LeitorCSV {
    
        public static void main(String[] args) throws FileNotFoundException {
        try {    
            
            LinkedList<Aluno> L = CSVLeitor.Leitor(new File("C:\\csv_file.csv"));
           
            for( Aluno e : L) {
                System.out.print(e.toString());
            }
        }
        catch ( Exception e) {}
        }
    }

    the final result is that only reads the first line , only store in the LinkedList the object containing the first 2 numbers of the first line.
    i runned the debugged mode and only do 1 iteration.
    thank you.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,387
    Rep Power
    25

    Default

    Java Code:
        while (s.hasNextLine()) {
                String stringa1 = s.next();
                String stringa2 = s.next();
    One problem here is there is only one call to hasNextLine() for two calls to next().

    How many lines in the input file?

    Have you tried debugging the code by printing out EVERYTHING you read before using it?

    Where in your code do you expect to read the second line from the file?
    Last edited by Norm; 06-05-2011 at 06:56 PM.

  3. #3
    Cruz182 is offline Member
    Join Date
    Jun 2011
    Posts
    3
    Rep Power
    0

    Default

    The input file got about 50 lines each one like this
    2132312 ; 232
    2353434 ; 553
    .... and so on ...

    it only gets me the first.
    when I try to println in the while it only prints the first line, then breaks the while.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,387
    Rep Power
    25

    Default

    Where in your code do you expect to read the second line from the file?

  5. #5
    Cruz182 is offline Member
    Join Date
    Jun 2011
    Posts
    3
    Rep Power
    0

    Default

    this cicle do that right " while (s.hasNextLine()) " ?

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,387
    Rep Power
    25

    Default

    Look at your code. What data does the Scanner object s scan?

Similar Threads

  1. Read a file and converting this file into a string
    By kostinio in forum New To Java
    Replies: 7
    Last Post: 12-26-2009, 03:54 PM
  2. Replies: 2
    Last Post: 05-11-2009, 10:07 AM
  3. Replies: 0
    Last Post: 02-11-2009, 09:53 AM
  4. Replies: 5
    Last Post: 02-05-2009, 10:28 AM
  5. How to read a text file from a Java Archive File
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-08-2008, 09:13 AM

Posting Permissions

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