Question: Write a program that asks the user to enter the amount that he or she has budgeted for a month. A loop should then prompt

Write a program that asks the user to enter the amount that he or she has budgeted for a month. A loop should then prompt the user to enter each of his or her expenses for the month, and keep a running total. When the loop finishes, the program should display the amount that the user is over or under budget.

I wrote the following code and all of it works except for when I exit the while loop at the bottom of the code. I get an IOException everytime I leave that loop and I'm not sure how to fix it.

import java.util.Scanner;

import java.io.*;

public class homework2

{

public static void main(String[] args) throws IOException

{

//1.

System.out.println("1) Write a program that asks for the number of checks written for the month. The program should then calculate "

+ "and display the banks service fees for the month. ");

Scanner keyboard = new Scanner(System.in);

System.out.println("How many checks have you written this month? Checks Written:");

float checkAmt = keyboard.nextInt();

float baseFee = 10.0f; //base amount

float addFee = 0.0f;

if (checkAmt < 20)//if-else statements that determines the fee cost

addFee = 0.10f;

else if (checkAmt >= 20 && checkAmt <= 39)

addFee = 0.08f;

else if (checkAmt >= 40 && checkAmt <= 59)

addFee = 0.06f;

else if (checkAmt >= 60)

addFee = 0.04f;

float sum = (baseFee + (checkAmt * addFee));//calculates the amount owed

//System.out.println(addFee);

System.out.println("Your service fee for this month is: $" + sum + " ");

//2.

//3.

System.out.println("3) Write a program that asks the user to enter the amount "

+ "that he or she has budgeted for a month. A loop should then "

+ " prompt the user to enter each of his or her expenses for the month,"

+ "and keep a running total. When the loop finishes, the program "

+ "should display the amount that the user is over or under budget. ");

System.out.println("How much do you plan to budget for this month?");

float budgetAmt = keyboard.nextFloat();

System.out.println(budgetAmt);

String in = keyboard.nextLine();

System.out.println(in);

while (in != "q")

{

if(budgetAmt > 0)

{

System.out.println(" You're currently under-budget");

System.out.println("What are your expenses? (Enter \"q\" to end program");

float expense = keyboard.nextFloat();

float leftOver = budgetAmt - expense;

System.out.println(" You have $" + leftOver + " left in your monthly budget.");

budgetAmt = leftOver;

}

else if(budgetAmt <= 0)

{

System.out.println(" You're over-budget");

System.out.println("What are your expenses? (Enter \"q\" to end program");

float expense = keyboard.nextFloat();

float overbudget = budgetAmt - expense;

float debt = overbudget * -1;

System.out.println(" You are $" + debt + " in-debt.");

budgetAmt = overbudget;

}

}

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!