0

I am a beginner of java, and when I following the display 2.12 of Absolute Java. The console shows there is thread in 'main. I put the text file in the root directory of my program. And I also have double check for the code. I have no idea how to fix it. I am using eclipse to code in the macOS and java is the newest version. Thanks for your help The code shows like this:

import java.util.Scanner;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

public class TextFileDemo
{
    public static void main(String[] args)
    {
        Scanner fileIn = null;
        try
        {
            fileIn = new Scanner(new FileInputStream("player.rtf"));
        }
        catch (FileNotFoundException e)
        {
            System.out.println("file not found.");
            System.exit(0);
        }

        int highscore;
        String name;

        System.out.println("Text left to read? " +    fileIn.hasNextLine());
        highscore = fileIn.nextInt();
        fileIn.nextLine();
        name = fileIn.nextLine();

        System.out.println("Name: " + name);
        System.out.println("High score: " + highscore);
        System.out.println("Text left to read? " + fileIn.hasNextLine());
        fileIn.close();
    }

And this is my console display:

And this is my console display

Jerry
  • 11
  • 1
  • 6

2 Answers2

0

The error indicated that, when reading from your data file, the first thing you tried to read with nextInt() was not actually an integer.

Examine what is in the file. Specifically, in an editor which treats the file as unformatted data, like notepad.

Your file name suggests the file you are reading is an .rtf file, or "Rich Text Format". Scanner cannot read that.

Save the file as a plain text file.

AJNeufeld
  • 8,526
  • 1
  • 25
  • 44
  • Thanks, I try to use .txt file before, but it is the same problem – Jerry Apr 12 '16 at 13:47
  • Just renaming `player.rtf` to `player.txt` does not change the encoding of the file; make sure to **save** the file as plain text. If you are certain the file is plain text, as a temporary debugging measure, replace `highscore = fileIn.nextInt(); fileIn.nextLine();` with `highscore = 1234; System.out.println("Line = "+fileIn.nextLine());` and report to us what the output was for further debugging assistance. – AJNeufeld Apr 12 '16 at 15:28
  • I checked the file information which shows it is plain text. I changed the code as your instruction and there is no error appear. But the result is weird , like this: Text left to read? true Line = {\rtf1\ansi\ansicpg1252\cocoartf1404\cocoasubrtf460 Name: {\fonttbl\f0\fswiss\fcharset0 Helvetica;} High score: 1234 Text left to read? true – Jerry Apr 12 '16 at 23:39
  • Yup! That's an `RTF` encoded document; it begins: "`{\rtf1`". You'll need to open it with an RTF aware program, and save-as a regular plain text file. Reading just the text of an rtf document is way beyond the capabilities of your `Scanner` class. – AJNeufeld Apr 12 '16 at 23:57
  • On MacOS, the `TextEdit` will work for this. Open the document, then "Format -> Make Plain Text", and save to a new name. – AJNeufeld Apr 13 '16 at 00:11
  • Thank you so much! I got the result ! Your knowledge is amazing ! – Jerry Apr 14 '16 at 00:30
0

I am not sure if I am right but this error is throw because you might be trying to match a int to a string. Probably, due to the content of the file you might want to upload that to the question too. If you just going to print to console I suggest you convert all of them to string.

This might help also Input Mismatch Exception

Community
  • 1
  • 1
UserBlanko
  • 202
  • 2
  • 12