Question & Answer: Write a Java application to accomplish the following task:…..

Write a Java application to accomplish the following task:

Ask users to input the amount of tax they paid for the past 3 years.

Write this information to a text file.

Expert Answer

 

There is two ways you can go for this, either take one input to store the total amount of tax for the past 3 years or take an array for each of the year. It really doesn’t matter what steps do you take the process remains the same. You can take input and write them to the file. If you store it into one value you have to write only one data to the file otherwise you have to iterate through the array and write individual value to the file. Here is the code for the same:

import java.util.*;

import java.lang.*;

import java.io.*;

class tax

{

public static void main (String[] args) throws java.lang.Exception

{

double value;

System.out.println(“Enter amount of tax paid for the last three years: “);

Scanner in = new Scanner(System.in); //to take input

value = in.nextInt(); //store input in the variable

try{

PrintWriter writer = new PrintWriter(“the-file-name.txt”, “UTF-8”); //open a file

writer.println(value); //write to it

writer.close(); //close the file

} catch (IOException e) {

// can include messages here not needed though

}

}

}

Edit :

import javax.swing.JOptionPane and then replace the part of the code above try with the following:

String amount;
double value;
amount = JOptionPane.showInputDialog(“Enter amount of tax paid for the last three years: “);
value=Double.parseDouble(amount);

This pops up an input dialog box and takes the value, after than it converts it to double type.

Still stressed from student homework?
Get quality assistance from academic writers!