I have a programming assignment for my Intro to Java Programming class and I am stuck with a couple of minor problems. Here is the assignment:
I am to write a program that accepts two inputs from the user: one will be the name of a .txt file, and the second input will be a whole number of type long. It will create a new File object with the file name inputted by the user, and then it will get a PrintWriter object by passing the File object to the PrintWriter constructor. If the file is found, it will read in the whole number.
It will then use a recursive method to reverse the number inputted by the user (for example, if the user entered a number 123456, it would reverse the number to 654321). It will then write that reversed number both to the .txt file and to the screen.
Here is my code:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.io.PrintWriter;
public class Prog2c
{
public static void main(String[] args)
{
File inputFile = null; // File object for user's file
Scanner fileReader = null; // reads user's file
PrintWriter fileWriter = null; // writes to user's file
Scanner scan = new Scanner(System.in);
String fileName = null;
long number = 0;
try
{
fileName = scan.nextLine();
number = scan.nextLong(); // reads the whole number from the user
inputFile = new File(fileName); // creates new File object with user's file
fileReader = new Scanner(inputFile); // reads contents of user's file
System.out.println("Program 2, Christopher Moussa, masc1754");
if (inputFile.exists())
{
System.out.println("The file " + fileName + " is ready for writing."); // throws FileNotFoundException if
fileWriter = new PrintWriter(inputFile);
number = reverseNumber(number);
fileWriter.println(number); // performs recursive method on number inputted by user
System.out.println("File " + inputFile + " exists? " + inputFile.exists()); // checks to see if file exists
while (fileReader.hasNextLong())
{
number = fileReader.nextLong();
System.out.println(number); // prints contents of the file
}
}
}
catch (FileNotFoundException exception) // in case the file is not found or does not exist
{
System.out.println("FileNotFoundException file was not found: " + exception.getMessage());
System.exit(0);
}
finally
{
if (null != fileReader)
{
scan.close(); // closes scanner
fileWriter.close();
}
}
}
public static long reverseNumber(long number) // Recursive Method
{
if (number < 10) // Base Case 1: In case the number
{ // is a single digit number, I will
System.out.println(number); // just return the number
return number;
}
else // Recursion: This will take the
{ // remainder of the original number
System.out.print(number % 10); // and print it. It will then divide
reverseNumber(number/10); // the number by 10 to truncate it by
return number; // one digit. It will then return the value.
}
}
}
Here are my problems:
When I run the program, the number that is written to the .txt file is the original number that I inputted, not the number reversed (the reversed number is what should be written to the .txt file). It is also not printing out the content of the .txt file (the reversed number) to the screen, even though I have a scanner which should read the contents of the file and print the number to the screen (which is the function of the while statement in my program, right?). My idea as to why the PrintWriter is not writing the reversed number into the .txt file is because you cannot assign the result of a method to a variable? I am not completely sure. Any advice/suggestions as to how to write the reversed number into the .txt file and then print the content of that .txt file (which only has the reversed number) to the screen would be much appreciated, thank you.
You're reading from the file, while editing it. First, finish writing the number to file, then close to writer. Next open the reader.