I have a file with a syntax that looks like this:
! <Group>, Column1, column2, column3
Group, 10, 20, 30
Group, 11, 12, 13
...
! <AnotherGroup>, Column1, column2, column3, column4
AnotherGroup, 10, 20, 30, A
AnotherGroup, 11, 12, 13, B
...
When I try to manipulate the strings through the Scanner object, some strange things are happening such as:
- end of file is found
- sometimes does not recognize the character “!” (see code snippet)
-
sometimes does not recognize space characters
sc = new Scanner(new File("files/myFile")); while (sc.hasNextLine()) { String tempLine = sc.nextLine(); if (tempLine.contains("!")) { System.out.println(tempLine);
Anyway, I think I have to read the entire file and stores it in a specific format in order to manipulate.
Any advices?
Edited after first answer:
This is the case wich sometimes does not recognize the character “!” (see code snippet)
My Sysout:
! <Group1>, Column1, column2, column3
! <Group2>, Column1, column2, column3
! <Group5>, Column1, column2, column3
Didnt read the lines with groups 3 and 4
I dont think that there is a problem with the Scanner. At least the part of the code that you have provided should work fine.
May be verify if you are doing some silly mistake or wait until some gurus come to your help 😉