How to hashMap an array in Java?

Calculating User Input in an Array (Java)?

  • Hello, i am trying to gather user input and store inside an array, which then adds the points of the array together. With this total i then want to subtract the total from another int (budget). Any Suggestions? I keep getting the result budget - the array length, rather than array total. E.g. Budget (100) - how many bills (2) = 98 this is my code...thanks import javax.swing.JOptionPane; import java.util.Scanner; public class input { public static void main (String[] args) { Scanner input = new Scanner(System.in); System.out.println("total budget?"); int budget = input.nextInt(); //allow user input; System.out.println("How many bills?"); int num = input.nextInt(); int array[] = new int[num]; System.out.println("Enter the amount " + num + " one by."); for (int i = 0 ; i < array.length; i++ ) { array[i] = input.nextInt(); } { int sum = budget-num; for (int x: array) { num+=x; System.out.println("You Have spent a toal of " + num + " pounds"); } System.out.println("You Have spent a toal of " + sum + " pounds"); { } } } }

  • Answer:

    First off, this line is wrong: int sum = budget-num; That just means budget-number of bills. You're reusing "num" when you shouldn't be. Use a new variable name.

Paul at Yahoo! Answers Visit the source

Was this solution helpful to you?

Other answers

First off, this line is wrong: int sum = budget-num; That just means budget-number of bills. You're reusing "num" when you shouldn't be. Use a new variable name.

godfathe...

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.