0

Hi i am totally a beginner and trying a simple input/output program for practice, the whole programs run in my Eclipse compiler but the result is something like

Enter maintenance expense : 87
Enter School fees: 67
Enter Utility Bills: 98
Enter Grocery expense : 88
Total expense : 88 ( here is the problem it give the last value i enter)

import java.util.Scanner;

public class budgetSystem {
    Scanner userInput = new Scanner(System.in);
    private static Scanner userInput2;
    public static void main(String[] args){

         int utilityBills = 0;
         int maintainceExp = 0;
         int schoolExp = 0;
         int groceryExp = 0;
         int totalExp = 0;
         userInput2 = new Scanner(System.in);

             System.out.println("Enter Maintaince Expance:");
             utilityBills = userInput2.nextInt();

             System.out.println("Enter School Fees:");
             utilityBills = userInput2.nextInt();

             System.out.println("Enter Utility Bills:");
             utilityBills = userInput2.nextInt();

             System.out.println("Enter Grocery Expance:");
             utilityBills = userInput2.nextInt();

            totalExp= utilityBills + maintainceExp + schoolExp + groceryExp;

             System.out.println("Total Expance: " + totalExp);



    }

} // END OF BUDGET SYSTEM

4 Answers 4

2

For every step you're reassigning utilityBills variable, change your code from:

System.out.println("Enter Maintaince Expance:");
utilityBills = userInput2.nextInt();

To

System.out.println("Enter Maintaince Expance:");
utilityBills += userInput2.nextInt();

In every input you take, this would print correct output.

Sign up to request clarification or add additional context in comments.

Comments

1

You overwrite the variable utilityBills the whole time.

Change your code to:

System.out.println("Enter Maintaince Expance:");
 maintainceExp = userInput2.nextInt();

System.out.println("Enter School Fees:");
schoolExp = userInput2.nextInt();

 System.out.println("Enter Utility Bills:");
 utilityBills = userInput2.nextInt();

System.out.println("Enter Grocery Expance:");
 groceryExp = userInput2.nextInt();

Comments

1

Everytime you scan a value and assigning it back to {utilityBills}, instead set the respective variables in your program. Your last entered value is 88 which is set for {utilityBills} and rest all variables are still zero never set to any value. Change your code as below.

 System.out.println("Enter Maintaince Expance:");
             maintainceExp = userInput2.nextInt();

             System.out.println("Enter School Fees:");
             schoolExp = userInput2.nextInt();

             System.out.println("Enter Utility Bills:");
             utilityBills = userInput2.nextInt();

             System.out.println("Enter Grocery Expance:");
             groceryExp = userInput2.nextInt();

            totalExp= utilityBills + maintainceExp + schoolExp + groceryExp;

Comments

0

You're saving every value in utilityBills instead of the correct variables. That's why every other value is 0 and the total is then set to the last entered value.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.