How to hashMap an array in Java?

Array in java, somebody help me, please?

  • create a java program (using either the scanner and console or the joptionpane class with input and message dialog boxes) that will make an array of five items for sale in a grocery store and a second, parallel array holding their prices. when the user inputs the index number of an element in the array, the item and price should be output in a complete sentence, such as "The price of milk is $1.79." Note that the price is formatted as currency and there's a period at the end of the sentence. Note also that java will create both arrays with their first elements having index number of 0. When we initialize the arrays (using "initializer lists"), if we want our value to match "human numbering" as shown in the flowchart, we should assign null values to the first element of each array, that is 0 for the numeric array and "" (an empty string) for the String array. this is what it is in visual logic flowchart, how to write it on java, help me please. Begin | Make Array(item, 5) | Make Array(price, 5) | item(1) = "bread" | item(2) = "milk" | item(3) = "juice" | item(4) = "eggs" | item(5) = "coffee" | price(1) = 3.29 | price(2) = 1.79 | price(3) = 2.19 | price(4) = 1.99 | price(5) = 3.89 | input: index | output: "The price of" & item(index) & "is" & FormatCurrency(price(index)) & "." when user input a number from 1 to 5, it will show up a price, for example if user input 4, the output should be " the price of eggs is $1.99". if user input 2, output should be "the price of milk is $1.79", it corresponding to both item and price. the beginning may be like this, but i am not sure: String [] item = {"bread", "milk", "juice", "eggs", "coffee"} double [] price = {3.29, 1.79, 2.19, 1.99, 3.89} i dont know what is the following, help me out, thank you very much.

  • Answer:

    http://ideone.com/5mHb8 <-JOptionPanes don't work on ideone so copy paste it to run it on your machine. reduce index by one, because the first entry in an array is at 0. Any questions? Your error is likely because you're reading in a string, it needs to be converted to an integer http://ideone.com/KwD5S <- no JOptionPanes, using scanner to read an int

Xing Lin at Yahoo! Answers Visit the source

Was this solution helpful to you?

Other answers

Your arrays look good, but you need to make the first item in each of the lists a null value. For example, index 1 would point to 'milk', not 'bread'. To fix this problem, just put in null values for the first list element. so your strings should be {null, "bread", "milk", "juice", "eggs", "coffee"} and the prices would be double [] price = {0, 3.29, 1.79, 2.19, 1.99, 3.89}. Then ask for user input, store this in a variable. Then all you have to do is print out the corresponding index, like System.out.print(item[inputVariable] + "costs " + price[inputVariable]).

reidesol

OK so you didn't like my last answer... first thing is first, you are not following your instructions. The items and prices must be input by the user. When you initialize your array you need to use String[] items = new String[6]; items[0] = ""; You must do this because you want to store 5 items and your instructions say that item 0 must be an empty string. When strings are created in java they are already empty strings but we can explicitly set it here anyway. for the doubles use: double[] prices = new double[6]; prices[0] = 0.0; the next step is asking the user five times (use a loop like I used in my other answer) and store the answers in items[i] and prices[i] assuming your loop uses i as it's variable. This way they will match up for sure. after that's done you ask the user the index they wish to display and you can pull it from items[index] and prices[index]...

Jeff

Just Added Q & A:

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.