0

I've got this basic program working but it's spitting out a weird answer. When I run the program it gives me:

{\rtf1\ansi\ansicpg1252\cocoartf1038\cocoasubrtf350
{\fonttbl\f0\fswiss\fcharset0
Helvetica;}
{\colortbl;\red255\green255\blue255;}
\margl1440\margr1440\vieww9000\viewh8400\viewkind0
\pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\ql\qnatural\pardirnatural
\f0\fs24
\cf0
CIS
260
is
cool.\
Let's
put
another
sentence
in
here.\
Programming
is
problem
driven.}

*

import java.util.Scanner;
import java.io.*;

public class FileIO
{

public static void main(String[] args)
{

    File Fred = new File(System.getProperty("user.home"), "mytext.txt");
    try
    {   

        Scanner input = new Scanner(Fred);
        while (input.hasNext())
        {
            System.out.println(input.next());
        }                  

    }

    catch(Exception e)
    {

        System.out.println(e);

    }

}


}

2 Answers 2

3

How did you create mytext.txt? It looks like you're reading in an RTF file rather than a flat text file. Try recreating mytext.txt with a simple text editor and try it again.

Sign up to request clarification or add additional context in comments.

2 Comments

Agreed. I tried this source code exactly as you wrote it on my mac, and it gives me the expected output. I even saved the file with different encodings (mac, unix, windows), but it was read properly. The problem must be that mytext.txt is saved as RTF.
Thank you. I didn't realize the even though I made it read as a .txt file, it didn't convert it to one.
1

It looks like your mytext.txt file was saved as rich text, instead of plain text, so it contains formatting information as well as the actual content.

Save the text file again as plain text and you should get the result you expect.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.