-1

I have to read a int number 8 from the file and then saves the output into the same file. Everything works good but somehow the output is not display into the file. I don't know what's wrong. Any help will be appreciated.

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

public  class RomanNumerals
{
    public static void main(String[] args)throws IOException
    {
        // Reading data from the file.
        Scanner inputFile = new Scanner(new FileReader("C:\\Users\\IRC115\\Documents\\NumberValue.txt"));

        // Declare the variable.
        int number;

        // Reading the value from the file.
        number = inputFile.nextInt();

        // Close the input file.
        inputFile.close();

        // Create the PrintWriter Object.
        FileWriter fwriter =
            newFileWriter("C:\\Users\\IRC115\\Documents\\NumberValue.txt",true);
        PrintWriter outputFile =
            new PrintWriter(fwriter);

The path of the file is correct. I am not getting error when I execute the program. Is the switch statment is correct? I don't see any error.

switch (number)
{
    case 1:
    outputFile.println("I");
    break;

    case 2:
    outputFile.println("II");
    break;

    case 3:
    outputFile.println("III");
    break;

    case 4:
    outputFile.println("IV");
    break;

    case 5:
    outputFile.println("V");
    break;

    case 6:
    outputFile.println("VI");
    break;

    case 7:
    outputFile.println("VII");
    break;

    case 8:
    outputFile.println("VIII");
    break;

    case 9:
    outputFile.println("IX");
    break;

    case 10:
    outputFile.println("X");
    break;

    default:
    outputFile.println("Invalid number.");

    // Close the output file.
    outputFile.close();
}
Tom
  • 16,842
  • 17
  • 45
  • 54
Smit Patel
  • 7
  • 2
  • 6
  • Why to vote negative? If have had made a mistake, than let me know. So I can correct it. – Smit Patel Mar 18 '15 at 20:37
  • try closing your `outputFile` at the end (`outputFile.close()`) also, use a try - final statement to make sure your streams are closed appropriately – morgano Mar 18 '15 at 20:39
  • possible duplicate of [Converting Integers to Roman Numerals - Java](http://stackoverflow.com/questions/12967896/converting-integers-to-roman-numerals-java) – Mordechai Mar 18 '15 at 20:48

1 Answers1

0

Take a look on what's Oracle advice us regarding files: http://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html

To answer your question, as mentioned before youll probably only have to close the file for your code to work, but it will be way better to use try-with-resources syntax (if you use Java 7+).

try (BufferedReader br = new BufferedReader(new FileReader(path))) {
    return br.readLine();
}

this syntax automatically closes the file.

Radek
  • 86
  • 6