Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that asks the user to enter the amount he or she has budgeted fo

ID: 3531069 • Letter: W

Question

Write a program that asks the user to enter the amount he or she has budgeted for a month. Your program should then prompt the user to enter the name of an input file where his or her monthly expenses are stored. Once the user enters a valid input file name, a loop should be used to sum the expenses in the file by keeping a running total (using an accumulator variable). When the loop finishes, the program should display the amount that the user is over or under budget. If the user enters an invalid input filename, then your program should display the message, Invalid input file name. Please try again., and then terminate. input file=expenseData.txt-: 150.00 200.00 75.00 50.00 75.00 200.00 150.00

Explanation / Answer

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;


public class BudgetTest {

public static void main(String[] args) {
Scanner inputScanner = new Scanner(System.in);
System.out.println("Enter the budget for this month: ");
double budget= inputScanner.nextDouble();
System.out.println("Enter the expenses file name ");
String fileName = inputScanner.next();
Scanner fileScanner = null;
try {
fileScanner = new Scanner(new File(fileName));
} catch (FileNotFoundException e) {
System.out.println("File Not Found " + e.getMessage() + " Please try again ");
}
double totalExpenses = 0;
while(fileScanner.hasNextDouble()) {
totalExpenses += fileScanner.nextDouble();
}

if(totalExpenses > budget) {
System.out.println("Expenses over the budget by " + (totalExpenses - budget));
}
else {
System.out.println("Expenses under the budget by " + (budget - totalExpenses));
}


}

}


output:


Enter the budget for this month:
1000
Enter the expenses file name
expenses.txt
Expenses under the budget by 100.0

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote