Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading degree symbol from .txt file with scanner

I'm writing a master program for my simulator flying. I get briefings in a .txt file, and I am attempting to read the file with a scanner. The .txt file contains a degree symbol, and this causes the scanner to not to read the entire file.

 public static String[] ConvertFile(String FileName){
    ArrayList<String> FileArray = new ArrayList<String>();
    int count = 0; 

    try{
        Scanner file =  new Scanner( new File ("C:\ <File Location>" + FileName));
        while(file.hasNextLine()){
            count++;
            String Line = file.nextLine());
            System.out.printf("%3d: %s %n", count, Line );
            System.out.println(count);

        }
    }
    catch(FileNotFoundException fnfe){
        System.out.println("File Not Found.");
    }
    return null;
}

Ive put the line in queston below (Line 23)

COND:    140475 LB // RWY DRY // +14°C  Q1021  270/09  // LMT: OBS(B)

Note that the output gives the first 16 lines of the txt file when there are 726 lines. I know its the degree symbol because when I edit the txt and remove the degree symbols, the program outputs all the lines.

like image 967
TheSpaceWolf Avatar asked Oct 30 '22 13:10

TheSpaceWolf


1 Answers

This is interesting behavior. I tried to run your example and I got the same results, except for me, if I had a degree symbol on line 5 of my file, the program did not even want to display the first 4 lines. The exact reason behind this has something to do with character encoding and it would be interesting to find a detailed explanation with someone who has an insight into this.

After some digging around, it turns out that Java's Scanner uses the underlying platform's default character set. You can find out what your default character set is by using the following code:

System.out.println(java.nio.charset.Charset.defaultCharset());

On my system, the message that was displayed was "UTF-8". I am currently using a Windows 10 system, and the text file that I created had a default character encoding of "ANSI". I would advise to make sure that the character encoding that your Scanner is using and the character encoding of your text file are the same.

Using Window's notepad application, I was able to change the character encoding from ANSI to UTF-8. Just click on "File" then "Save As..." and when the dialog box pops up, make sure that you choose "UTF-8" on the bottom in the "Encoding" box.

enter image description here

When I re-ran the example. The application was able to read every line successfully. Hope this helps. Cheers.

like image 149
Adam Bak Avatar answered Nov 15 '22 04:11

Adam Bak