Array in java, somebody help me, please?
-
can someone help me? 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)) & "."
-
Answer:
This isn't what you wanted, but I thought I'd have some fun, because in real life people use map for what you are doing. it maps a key to a value which is awesome for matching food to prices. Either way, you can still use this to figure out some java. import java.util.*; public class Grocery { Map<String, Double> pricing = new HashMap<String, Double>(); int numFoods = 5; Scanner scan = new Scanner(System.in); Grocery(){ for (int i = 0; i<numFoods; i++ ){ System.out.print("Enter a food : "); String key = scan.next(); System.out.print("Enter a price: "); Double value = scan.nextDouble(); System.out.println(); pricing.put(key, value); } System.out.println("\nSo what do you want to know the price of? :"); String key = scan.next(); if (pricing.containsKey(key)){ System.out.printf("The price of %s is $%.2f.\n", key, pricing.get(key)); } else System.out.println("Sorry, item not found!"); } public static void main(String[] args) { new Grocery(); } }
Xing Lin at Yahoo! Answers Visit the source
Related Q & A:
- How to hashMap an array in Java?Best solution by Stack Overflow
- Could somebody please help me come up with a really cool msn name?Best solution by Yahoo! Answers
- I have a problem with webcam help me please.Best solution by Yahoo! Answers
- Can somebody help me introduce myself in portuguese?Best solution by Yahoo! Answers
- Can somebody please explain to me what spatial perspective is?Best solution by answers.yahoo.com
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
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.