Home > Software design >  Can't read integer file
Can't read integer file

Time:12-24

I'm trying to read data from a file that contains integers, but the Scanner doesn't read anything from that file.

I've tried to read the file from the Scanner :

// switch() blablabla 
case POPULATION:
            try {
                while (sc.hasNextInt()) {
                    this.listePops.add(sc.nextInt());
                }
            } catch (Exception e) {
                System.err.println("~ERREUR~ : "   e.getMessage());
            }
            break;

And if I try to print each sc.nextInt() to the console, it just prints a blank line and then stops. Now when I read the same file as a String:

?652432
531345
335975
164308
141220
1094283
328278
270582
// (Rest of the data)

So, I guess it can't read the file as a list of integers since there's a question mark at the beginning, but the problem is that this question mark doesn't appear anywhere in my file, so I can't remove it. What am I supposed to do?

CodePudding user response:

If the first character in the file is a question mark (?) and its original origin is unknown then it is usually the UTF-8 Byte Order Mark (BOM). This means the file was saved as UTF-8. The Microsoft Notepad application will add a BOM to the saved text file if that file was saved in UTF-8 instead of ANSI. There are also other BOM characters for UTF-16, UTF-32, etc.

Reading a text file as String doesn't look like a bad idea now. Changing the save format of the file can work to but that BOM may have actual intended purpose for another application, so, that may not be a viable option. Let's read the file as String lines (read comments in code):

// Variable to hold the value of the UTF-8 BOM:
final String UTF8_BOM = "\uFEFF";

// List to hold the Integer numbers in file.
List<Integer> listePops = new ArrayList<>();
    
// 'Try With Resources' used to to auto-close file and free resources.
try (Scanner reader = new Scanner(new File("data.txt"))) {
    String line;
    int lineCount = 0;
    while (reader.hasNextLine()) {
        line = reader.nextLine();
        line = line.trim();
        // Skip blank lines (if any):
        if (line.isEmpty()) {
            continue; 
        }
        lineCount  ;
        /* Is this the first line and is there a BOM at the 
           start of this line? If so, then remove it.    */
        if (lineCount == 1 && line.startsWith(UTF8_BOM)) {
            line = line.substring(1);
        }
            
        // Validate Line Data:
        // Is the line a String representation of an Integer Number?
        if (line.matches("\\d ")) {
            // Yes... then convert that line to Integer and add it to the List.
            listePops.add(Integer.parseInt(line));
        }
        // Move onto next file line...
    }
}
catch (FileNotFoundException ex) {
    // Do what you want with this exception (but don't ignore it):
    System.err.println(ex.getMessage());
}
    
// Display the gathered List contents:
for (Integer ints : listePops) {
    System.out.println(ints);
}
  • Related