Answer:

Yes. It is a little awkward, however, because the user prompts as well as the computed answer will be sent to the output text file.

All Output gets sent to a File

Here is an example of redirecting the output of this file. Notice that the user does not see any prompts because they have been sent to the output file:

C:\users\default\JavaLessons>java Discount > discount.out

100
20

C:\users\default\JavaLessons>type discount.out
Enter list price in cents:
Enter discount in percent:
Discount Price: 80

C:\users\default\JavaLessons>

Of course the output file does not contain any of the characters that the user typed because they were sent to the program. To deal with this problem, programs are sometimes written to echo all user input:

import java.util.Scanner;
class Discount
{
  public static void main ( String[] args ) 
  {
    int listPrice;
    int discount;
    int discountPrice;

    Scanner scan = new Scanner( System.in );

    System.out.print("Enter list price in cents: ");
    listPrice = scan.nextInt();
    System.out.println("List price:" + listPrice );    // echo input

    System.out.print("Enter discount in percent: ");
    discount = scan.nextInt();
    System.out.println("Discount:" + discount );      // echo input

    discountPrice = listPrice - (listPrice*discount)/100 ;
    System.out.println( "Discount Price: " + discountPrice );
  }
}

QUESTION 12:

Can the user pick any file name for the redirected output?