How to make this program output sent it to the file. import java.util.Scanner; p
ID: 3737580 • Letter: H
Question
How to make this program output sent it to the file.
import java.util.Scanner;
public class llll6 {
static Scanner input = new Scanner(System.in); //input object of scanner class for take single input from the user
static int a,b,result; //variable a and b for take input from the user and result is for storing the result
static char ch; //ch for take an input from the user
public static void main(String[] args) {
boolean done = false; //done is for continue the while loop
while (done == false) {
System.out.print("SIMPLE CALCULTOR ");
System.out.println("Select an option for calculation operation");
System.out.print("+ Addition, - Subtraction, * Multiplication, / division, % Modulation, A. Average, X. Maximum, M. Minimum, S. Square, Q. Quit ");
System.out.print("Enter Option: ");
ch = input.next().charAt(0); // a single character input from the user and store in ch variable
//switch case for inputed operation
switch (ch) {
case '+':
input(); // take input from the user
add(); //add method called when user enter +
prints(); // prints the result
break;
case '-':
input(); // take input from the user
subtract(); //subtract method called when user enter -
prints(); // prints the result
break;
case '*':
input(); // take input from the user
multiply(); //mutiply method called when user enter *
prints(); // prints the result
break;
case '/':
input(); // take input from the user
divide(); //divide method called when user enter /
prints(); // prints the result
break;
case '%':
input(); // take input from the user
module(); //module method called when user enter %
prints(); // prints the result
break;
case 'A':
input(); // take input from the user
average(); //average method called when user enter A
prints(); // prints the result
break;
case 'X':
input(); // take input from the user
maximum(); //maximum method called when user enter X
prints(); // prints the result
break;
case 'M':
input(); // take input from the user
minimum(); //minimum method called when user enter M
prints(); // prints the result
break;
case 'S':
inputSquare();// take input from the user only one input
square(); //square method called when user enter S
prints(); // prints the result
break;
case 'Q':
System.exit(0); //quit the operation
}
}
}
public static void prints() {
if(ch=='+'){
System.out.println("Sample output:");
System.out.println("Operation:addition");
System.out.println("augend: "+a);
System.out.println("addend: "+b);
System.out.println("Sum: "+result);
}
if(ch=='-'){
System.out.println("Sample output:");
System.out.println("Operation: subtraction");
System.out.println("augend: "+a);
System.out.println("addend: "+b);
System.out.println("Subtraction: "+result);
}
if(ch=='*'){
System.out.println("Sample output:");
System.out.println("Operation: multiplication");
System.out.println("augend: "+a);
System.out.println("addend: "+b);
System.out.println("Multiplication: "+result);
}
if(ch=='/'){
System.out.println("Sample output:");
System.out.println("Operation: division");
System.out.println("augend: "+a);
System.out.println("addend: "+b);
System.out.println("Division: "+result);
}
if(ch=='%'){
System.out.println("Sample output:");
System.out.println("Operation: modulation");
System.out.println("augend: "+a);
System.out.println("addend: "+b);
System.out.println("Modulation: "+result);
}
if(ch=='A'){
System.out.println("Sample output:");
System.out.println("Operation:Average");
System.out.println("augend: "+a);
System.out.println("addend: "+b);;
System.out.println("Average: "+result);
}
if(ch=='X'){
System.out.println("Sample output:");
System.out.println("Operation: Maximum");
System.out.println("augend: "+a);
System.out.println("addend: "+b);
System.out.println("Maximum: "+result);
}
if(ch=='M'){
System.out.println("Sample output:");
System.out.println("Operation: Minimum");
System.out.println("augend: "+a);
System.out.println("addend: "+b);
System.out.println("Minimum: "+result);
}
if(ch=='S'){
System.out.println("Sample output:");
System.out.println("Operation: square");
System.out.println("first number: "+a);
System.out.println("Square: "+result);
}
// TODO Auto-generated method stub
}
public static void input() {
// TODO Auto-generated method stub
System.out.println("Please enter First number: ");
a = input.nextInt();
System.out.println("Please enter Second Number: ");
b = input.nextInt();
}
public static void inputSquare() {
System.out.println("Please enter number to get square of number: ");
a = input.nextInt();
}
public static void square() {
result = a*a;
}
public static void minimum() {
if(a<b)
result = a;
else
result = b;
}
public static void maximum() {
if(a>b)
result = a;
else
result = b;
}
public static void average() {
result=(a+b)/2;
}
public static void module() {
result=a%b;
}
public static void divide() {
result=a/b;
}
public static void multiply() {
result=a*b;
}
public static void subtract() {
result=a-b;
}
public static void add() {
result=a+b;
}
}
Explanation / Answer
Hi, I have modified your code so that it will print the output to a file (and to the screen also) called output.txt each time an operation is performed. For this, defined a File object and a PrintWriter instance to write into that file, and inside the prints() method, defined the code to append the result to the output file using this PrintWriter instance.
Comments are included wherever code has been modified. Thanks
EDIT: getting troubles while submitting the answer without losing the format. Shows character limit exceeded error. So I’m attaching this as a plain text. Sorry if the code formatting is messed up.
// llll6.java
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
public class llll6 {
/**
* input object of scanner class for taking input from the user
*/
static Scanner input = new Scanner(System.in);
/**
* variable a and b for taking input from the user and result is for storing
* the result
*/
static int a, b, result;
static char ch; /* ch for getting input choice */
/**
* Defining a File to append the output
*/
static File outFile = new File("output.txt");
/**
* Defining a PrintWriter object to write output to a file
*/
static PrintWriter writer;
public static void main(String[] args) throws IOException {
/**
* Initializing PrintWriter object for writing output to the outFile
*/
writer = new PrintWriter(outFile);
boolean done = false; /* for continuing the while loop */
while (done == false) {
System.out.print("SIMPLE CALCULTOR ");
System.out.println("Select an option for calculation operation");
System.out
.print("+ Addition, - Subtraction, * Multiplication, / division, % Modulation, A. Average, X. Maximum, M. Minimum, S. Square, Q. Quit ");
System.out.print("Enter Option: ");
ch = input.next().charAt(0); /*
* a single character input from the
* user and storing in ch variable
*/
switch (ch) {
case '+':
input(); // take input from the user
add(); // add method called when user enter +
prints(); // prints the result
break;
case '-':
input(); // take input from the user
subtract(); // subtract method called when user enter -
prints(); // prints the result
break;
case '*':
input(); // take input from the user
multiply(); // mutiply method called when user enter *
prints(); // prints the result
break;
case '/':
input(); // take input from the user
divide(); // divide method called when user enter /
prints(); // prints the result
break;
case '%':
input(); // take input from the user
module(); // module method called when user enter %
prints(); // prints the result
break;
case 'A':
input(); // take input from the user
average(); // average method called when user enter A
prints(); // prints the result
break;
case 'X':
input(); // take input from the user
maximum(); // maximum method called when user enter X
prints(); // prints the result
break;
case 'M':
input(); // take input from the user
minimum(); // minimum method called when user enter M
prints(); // prints the result
break;
case 'S':
inputSquare();// take input from the user only one input
square(); // square method called when user enter S
prints(); // prints the result
break;
case 'Q':
writer.close();// closing the file writer
System.exit(0); // quit the operation
}
}
}
public static void prints() {
if (ch == '+') {
/**
* making the output a single String
*/
String output = "Operation: addition ";
output += "augend: " + a + " ";
output += "addend: " + b + " ";
output += "Sum: " + result + " ";
/**
* displaying the output
*/
System.out.println(output);
/**
* saving the output to the file
*/
writer.append(output);
}
if (ch == '-') {
/**
* making the output a single String
*/
String output = "Operation: subtraction ";
output += "augend: " + a + " ";
output += "addend: " + b + " ";
output += "Subtraction: " + result + " ";
/**
* displaying the output
*/
System.out.println(output);
/**
* saving the output to the file
*/
writer.append(output);
}
if (ch == '*') {
String output = "Operation: multiplication ";
output += "augend: " + a + " ";
output += "addend: " + b + " ";
output += "Multiplication: " + result + " ";
System.out.println(output);
writer.append(output);
}
if (ch == '/') {
String output = "Operation: division ";
output += "augend: " + a + " ";
output += "addend: " + b + " ";
output += "Division: " + result + " ";
System.out.println(output);
writer.append(output);
}
if (ch == '%') {
String output = "Operation: modulation ";
output += "augend: " + a + " ";
output += "addend: " + b + " ";
output += "Modulation: " + result + " ";
System.out.println(output);
writer.append(output);
}
if (ch == 'A') {
String output = "Operation: average ";
output += "augend: " + a + " ";
output += "addend: " + b + " ";
output += "Average: " + result + " ";
System.out.println(output);
writer.append(output);
}
if (ch == 'X') {
String output = "Operation: maximum ";
output += "augend: " + a + " ";
output += "addend: " + b + " ";
output += "Maximum: " + result + " ";
System.out.println(output);
writer.append(output);
}
if (ch == 'M') {
String output = "Operation: minimum ";
output += "augend: " + a + " ";
output += "addend: " + b + " ";
output += "Minimum: " + result + " ";
System.out.println(output);
writer.append(output);
}
if (ch == 'S') {
String output = "Operation: square ";
output += "first number: " + a + " ";
output += "Square: " + result + " ";
System.out.println(output);
writer.append(output);
}
}
public static void input() {
// TODO Auto-generated method stub
System.out.println("Please enter First number: ");
a = input.nextInt();
System.out.println("Please enter Second Number: ");
b = input.nextInt();
}
public static void inputSquare() {
System.out.println("Please enter number to get square of number: ");
a = input.nextInt();
}
public static void square() {
result = a * a;
}
public static void minimum() {
if (a < b)
result = a;
else
result = b;
}
public static void maximum() {
if (a > b)
result = a;
else
result = b;
}
public static void average() {
result = (a + b) / 2;
}
public static void module() {
result = a % b;
}
public static void divide() {
result = a / b;
}
public static void multiply() {
result = a * b;
}
public static void subtract() {
result = a - b;
}
public static void add() {
result = a + b;
}
}
/*OUTPUT*/
SIMPLE CALCULTOR
Select an option for calculation operation
+ Addition, - Subtraction, * Multiplication, / division, % Modulation, A. Average, X. Maximum, M. Minimum, S. Square, Q. Quit
Enter Option:
+
Please enter First number:
3
Please enter Second Number:
6
Operation: addition
augend: 3
addend: 6
Sum: 9
SIMPLE CALCULTOR
Select an option for calculation operation
+ Addition, - Subtraction, * Multiplication, / division, % Modulation, A. Average, X. Maximum, M. Minimum, S. Square, Q. Quit
Enter Option:
*
Please enter First number:
4
Please enter Second Number:
7
Operation: multiplication
augend: 4
addend: 7
Multiplication: 28
SIMPLE CALCULTOR
Select an option for calculation operation
+ Addition, - Subtraction, * Multiplication, / division, % Modulation, A. Average, X. Maximum, M. Minimum, S. Square, Q. Quit
Enter Option:
S
Please enter number to get square of number:
6
Operation: square
first number: 6
Square: 36
SIMPLE CALCULTOR
Select an option for calculation operation
+ Addition, - Subtraction, * Multiplication, / division, % Modulation, A. Average, X. Maximum, M. Minimum, S. Square, Q. Quit
Enter Option:
Q
/*output.txt after running the program*/
Operation: addition
augend: 3
addend: 6
Sum: 9
Operation: multiplication
augend: 4
addend: 7
Multiplication: 28
Operation: square
first number: 6
Square: 36
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.