2

There is definitely a line but I don't understand why cant the Scanner see it ..

Here is the beginning of the file :

256
<Y 1874>
<A T. HARDY‡&lt;T Madding Crowd(Peuœ‚978)”C i”P 51‡DESCRIPTION OF FMERÅAK -- AÄINCIºNT

Below is my code for getting it:

File file = new File ("calgary/book1_enc");
Scanner first_line = new Scanner(file);
int size_st;
size_st = Integer.valueOf(first_line.nextLine());

But I am getting the error:

Exception in thread "main" java.util.NoSuchElementException: No line found
    at java.util.Scanner.nextLine(Unknown Source)
    at LZWDecoder.main(LZWDecoder.java:26)

The file book1_enc is an output of my LZW encoding algorithm.When I am passing the file to my Decoder I would like the decoder to know the size of dictionary which is 256 in this case...Thanks for reading...

4

2 回答 2

2

The problem is with the encoding of the input file. Use the other constructor for Scanner that specifies a character set:

Scanner first_line = new Scanner(file, "UTF-8");

Using other constructors results in the default character set being used which typically cannot correctly read unicode characters.

Workaround:

BufferedReader br = new BufferedReader(new FileReader("book1_enc.dat"));
int sizeSt = Integer.parseInt(br.readLine());
于 2013-06-26T10:15:56.793 回答
0

It means that your file doesn't have next line. You should always check hasNextLine() before calling nextLine. You should modify your code like this

if (first_line.hasNextLine()){
    size_st = Integer.valueOf(first_line.nextLine());
}

java.util.NoSuchElementException is thrown by the nextElement method of an Enumeration to indicate that there are no more elements in the enumeration.

于 2013-06-26T10:13:16.030 回答