Scasey94
Scasey94

Reputation: 13

Copying user input text to output text file (java)

This is an assigment for my java class. I'm having to create user input and put that into an input file(InputFile.txt), then have this text be submitted through an output file(OutPutFile.txt). The question and sample text is below:

Write a program that opens a text file (InputFile.txt) and reads the input. The file contains lines of string that look like this:

this is line one.

this is line two.

this is line three.

this is line four.

Change the input string to upper case ("THIS IS LINE ONE.") Write the upper case line to a OutputFile.txt. Be sure to read all lines from the input file.

I've gotten pretty much everything down, except on going about putting the text into the output and it showing up. My code is below along with my result I keep getting. I know I'm messing up somewhere, I just don't know where.

Any help/recommendations would help tremendously. Thank you!!

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

public class Main {
    public static void main(String[] args) throws IOException {
        FileInputStream fileByteStream = null;
        Scaner inFS = null;
        String InputFile;

        fileByteStream = new FileInputStream("InputFile.txt");
        inFS = new Scanner(fileByteStream);

        while (inFS.hasNextLine()) {
            System.out.println(inFS.nextLine());
        }
        fileByteStream.close();

        FileOutputStream fileStream = new FileOutputStream("OutputFile.txt");
        PrintWriter outFS = new PrintWriter(fileStream);

        outFS.print(inFS);
        outFS.close();
    }
}

Result: java.util.Scanner[delimiters=\p{javaWhitespace}+][position=77][match valid=false][need input=false][source closed=true][skipped=false][group separator=\,][decimal separator=\.][positive prefix=][negative prefix=\Q-\E][positive suffix=][negative suffix=][NaN string=\QNaN\E][infinity string=\Q∞\E]

Upvotes: 0

Views: 2023

Answers (1)

user11760346
user11760346

Reputation:

Alright, first thing is that there is not library in Java called "java.util.io". What you are probably looking for is "java.io".

Another thing about your code is that you misspelled the Scanner object name, not sure if this was by accident when you posted your question but the correct syntax is:

Scanner inFS = null;

With regards to why you are receiving:

java.util.Scanner[delimiters=\p{javaWhitespace}+][position=77][match valid=false][need input=false][source closed=true][skipped=false][group separator=\,][decimal separator=.][positive prefix=][negative prefix=\Q-\E][positive suffix=][negative suffix=][NaN string=\QNaN\E][infinity string=\Q∞\E]

This is because you are passing the Scanner object to the PrintWriter. You are not actually printing the lines that the Scanner read in, you are printing the string contents of the Scanner returned from its toString() method.

Anyways, in order to put the text into the output, you're going to need a File object that points to the output file and a PrintWriter which points to the File object of the output file. The overall idea to get this to work is to have your PrintWriter object write to the OutputFile.txt while the program is reading the lines from InputFile.txt through the Scanner. Therefore, your code should look something like this:

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

public class Main 
{
    public static void main(String[] args) throws IOException 
    {
        File inputFile = new File("InputFile.txt");
        File outputFile = new File("OutputFile.txt");
        Scanner inFS = new Scanner(inputFile);
        PrintWriter outFS = new PrintWriter(outputFile);

        String line;
        while (inFS.hasNextLine()) 
        {   
            // Read in the line and convert it to upper case
            line = inFS.nextLine().toUpperCase();

            // Print the result to the console
            System.out.println(line);

            // Write the read in line to the output file with a new line character
            outFS.print(line + "\n");
        }

        // Close any resources used
        outFS.close();
        inFS.close();
    }
}

Upvotes: 1

Related Questions