简体   繁体   中英

Is there a CSV Scanning with No line found in.nextLine();

I have to work with CSV file but i dont need the first line, i tried to use: be.nextLine() it worked with other project but not with this one. Scanner be = new Scanner(new File("autok.csv"))

There is a CSV file i working with looks like this:

First line: | Departure | Destination | Number | Tel | room | Second line:| Budapest | Moscow | PQA-209| 30/5555555 | 5 | Third line | Budapest | Berlin | ASD-444| 30/4444855 | 3 |

This is what i tried and worked perfectly with other projects:

static ArrayList<autok> kocsi = new ArrayList<>();

public static void main(String[] args) {
  try (Scanner be = new Scanner(new File("autok.csv"))){
  be.nextLine();
  while(be.hasNextLine()){
      String[] s = be.nextLine().split(";");
      autok d;
      d = new autok(s[0],s[1],s[2],
                    Integer.parseInt(s[3]),
                    Integer.parseInt(s[4]));
      kocsi.add(d);
  }
  }catch (IOException ex) {
      System.out.println("Hiba");
  }

int  asd = kocsi.size();
for(int i =0; i<asd;i++){
    System.out.println("hirdetők száma: " + kocsi.size());
}

}```

POJO:

        this.indulas = indulas;
        this.cel = cel;
        this.rendszam = rendszam;
        this.telefonszam = telefonszam;
        this.ferohely = ferohely;
    }
    private String indulas;
    private String cel;
    private String rendszam;
    private int telefonszam;
    private int ferohely;

and the GETTERS.

As it looks i want to work with this CSV but it gives back the error: java.util.NoSuchElementException: No line found for the : be.nextLine(); Any help would be appreciated.

Working fine be.nextLine(); but your code need some upgrade

static ArrayList<autok> kocsi = new ArrayList<>();

public static void main(String[] args) {
    try (Scanner be = new Scanner(new File("src/com/test/autok.csv"))) {
        be.nextLine();
        while (be.hasNextLine()) {
            String[] s = be.nextLine().split("\\|");

            autok d;
            d = new autok(s[1], s[2], s[3], s[4], Integer.parseInt(s[5].trim()));
            kocsi.add(d);
        }
    } catch (IOException ex) {
        System.out.println("Hiba");
    }

    int asd = kocsi.size();
    for (int i = 0; i < asd; i++) {
        System.out.println("hirdetk szma: " + kocsi.size());
    }

}

Class Autok

public class Autok {

private String indulas;
private String cel;
private String rendszam;
private String telefonszam;
private int ferohely;

public Autok(String indulas, String cel, String rendszam, String s, int ferohely) {
    this.indulas = indulas;
    this.cel = cel;
    this.rendszam = rendszam;
    this.telefonszam = s;
    this.ferohely = ferohely;
}

// getter & setter
}

CSV File

| Departure | Destination | Number | Tel | room |
| Budapest | Moscow | PQA-209| 30/5555555 | 5 |
| Budapest | Berlin | ASD-444| 30/4444855 | 3 |

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM