Hoa Ngoc Quynh Le
Hoa Ngoc Quynh Le

Reputation: 15

Catching results from a void method into an output file

I am new at Java so thank you for helping!

    public static int convert (String value) {
      int temp_convert = 0;
      // Setting up new Scanner to read the User-input string
      Scanner token = new Scanner(value);
      // Take out the word "CONVERT"
      String fnc = token.next();
      // Get the temperature that needs to be converted
      int temp = token.nextInt();
      // Current unit of temperature
      String type = token.next().toLowerCase();
      if (type.equals("f")) {
         temp_convert = (int) Math.round((temp - 32)/1.8); 
         System.out.println(temp_convert + "C");
      } else if (type.equals("c")) {
         temp_convert = (int) Math.round(1.8 * temp + 32);
         System.out.println(temp_convert + "F");
      }
      return temp_convert;
   }

I am trying to get the print result from this method into an output file using PrintStream. I need whatever is lines printed in this method to be print out into the output file. How can I do this? This is the code I have so far but it doesn't produce anything in the .txt file yet.

public static void readFile (Scanner console, String file_input) throws FileNotFoundException {
      // Setting up new Scanner to scan the file
      Scanner input = new Scanner (file_input);
       // Prompt user for output file's name
      System.out.print("Output file name: ");
      String name_output = console.next();
      PrintStream file_output = new PrintStream(new File(name_output));
      System.out.println("YazLang program interpreted and output to .txt file!");
      System.out.println();
      while (input.hasNext()) {
         String value = input.nextLine().toLowerCase();
         if (value.startsWith("convert")) {
            int concert_temp = convert(value);
            file_output.println(concert_temp);
         } else if (value.startsWith("range")) {
            range(value);
         } else if (value.startsWith("repeat")) {
            repeat(value);
         }
      }
   } 

Upvotes: 0

Views: 1832

Answers (3)

Mạnh Quyết Nguyễn
Mạnh Quyết Nguyễn

Reputation: 18235

There are two options:

  1. Define the PrintStream in the outer most function, then pass it to whichever function you need. Proceed to write to file using PrintStream::print... method.

  2. Define a wrapper class that will write to both stream:

public class DuplexPrinter {

    private final PrintStream printStream;

    public DuplexPrinter(PrintStream printStream) {
        this.printStream = printStream;
    }

    public void println(String line) {
        System.out.println(line);
        printStream.println(line);
    }

    public void close() {
        printStream.close();;
    }
}

Init printer:

DuplexPrinter printer = new DuplexPrinter(file_output);

Now replace every call to System.out.println with:

printer.println()
  1. Using third party library. For example TeeOutputStream

Upvotes: 0

bb1950328
bb1950328

Reputation: 1599

you can use System.setOut() (https://www.tutorialspoint.com/java/lang/system_setout.htm) Insert this line after line 7 in your readFile method:

System.setOut(file_output);

Upvotes: 0

Piotr Niewinski
Piotr Niewinski

Reputation: 1347

Why don't you change signature of convert method to return int?

public static int convert (String value) {
    Scanner token = new Scanner(value);
    // ...
    return value;
}

And then write result to file in readFile method like this:

public static void readFile (Scanner console, String file_input) throws FileNotFoundException {
  // omitted..
  while (input.hasNext()) {
     String value = input.nextLine().toLowerCase();
     if (value.startsWith("convert")) {
        int concert_temp = convert(value);
        file_output.println(concert_temp);
     } else if (value.startsWith("range")) {
        // here
        int concert_temp = range(value);
        file_output.println(concert_temp);
     } else if (value.startsWith("repeat")) {
        repeat(value);
     }
  }
} 

Upvotes: 0

Related Questions