0

The code:

public static void main(String[] args) throws Exception{
    Scanner scn = new Scanner(new File ("kektura.csv"));
    int kezd = 0;
    List <String> indul = new ArrayList<>();
    List <String> veg = new ArrayList<>();
    List <Double> hossz = new ArrayList<>();
    List <Integer> emel = new ArrayList<>();
    List <Integer> lejt = new ArrayList<>();
    List <Boolean> pecset = new ArrayList<>();
    kezd=scn.nextInt(); 
    scn.nextLine();
    while(scn.hasNextLine())
    {
        scn.useDelimiter(";");
        indul.add(scn.next());           
        veg.add(scn.next());            
        hossz.add(scn.nextDouble());           
        emel.add(scn.nextInt());            
        lejt.add(scn.nextInt());            
        if(scn.next()=="n")
        {
            pecset.add(Boolean.TRUE);             
        }
        else
        {
            pecset.add(Boolean.FALSE);             
        }
        scn.nextLine();
    }
    for(Object x : pecset)
    {
        System.out.println(x);
    }
    scn.close();
}

And the file (It's a csv file, it has more lines, but they are the same pattern):

192
Sumeg, vasutallomas;Sumeg, buszpalyaudvar;1,208;16;6;n
Sumeg, buszpalyaudvar;Mogyorosi-domb, geologiai bemutatohely;1,512;24;8;n
Mogyorosi-domb, geologiai bemutatohely;Sumegi bazaltbanya vasutallomas;1,576;13;43;n
Sumegi bazaltbanya vasutallomas;Sarvaly erdeszhaz, pecsetelohely;2,101;69;18;i

I have a problem with reading this. Read the first line, it's okay. But the second line isn't good, because (i think) the delimeter reads until the ( ; ) character, but in the end of the line, there's not a ( ; ) . The last line of the while, I wrote scn.nextLine to read the \n character, but it doesn't work. I know thats a possible way, to read the line, and split it, but it's a school project, and the teacher told us to find out another solution. Is there a way, to solve this?

  • Don't use `Scanner` to read that file. Use a `BufferedReader` and the `readLine()` method to read lines of input. You can then use a `Scanner` to read values from the line, if you want. The primary read is by line, though, so that's what you should do. Better yet, use a CSV parser to read such file, e.g. [Apache Commons CSV](https://commons.apache.org/proper/commons-csv/). – Andreas Jan 13 '18 at 17:33
  • https://stackoverflow.com/a/4898295/3179169 – clinomaniac Jan 13 '18 at 17:34
  • On a side note, don't store the data in parallel lists. Java is an Object-Oriented language. Use it! Create a class with fields to store those values, then have a single list of objects. – Andreas Jan 13 '18 at 17:34
  • We learnt only Scanner, that's why I used that. I will try this with BufferedReader. if I try this multiply delimeter, I caught this: Exception in thread "main" java.util.InputMismatchException. Andreas, I will store this in a class, but I wanted to see whether I read this. (Spoiler: I couldn't :D ) But thanks, I'll take your advice. – JoskaStack Jan 13 '18 at 17:44

1 Answers1

0

As your teacher wants you to find another solution, I propose to use a BufferedReader to read the file in single lines. For each line you can then use String#split() and finally convert the respective parts to the required type (Integer#parseInt() etc).

However, as this is stated as a homework question, I will not provide a full example.

Izruo
  • 2,246
  • 1
  • 11
  • 23
  • He told us to don't read this line-by-line.. If we read with scanner, random access file, or anything, but line-by-line, then it isn't good for him. – JoskaStack Jan 13 '18 at 17:46
  • Well, it is a line-based input. How if not line-by-line could you possibly parse it? In fact, with that restriction, I don't think I could solve this myself ... sure, I could come up with some solutions, that don't *look* line-by-line, but according to the input they simply have to be and therefore always will be. – Izruo Jan 13 '18 at 17:50
  • I don't speak english very well, so my expression wasn't too good. In my code, I read this line-by-line. But the way to read the whole line to e.g. a String, and split into parts is not acceptable. I try to read this step by step. so, read a string, and another string, a double, etc. – JoskaStack Jan 13 '18 at 17:58