1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Java: String and Double arrays

Discussion in 'Software Development' started by DYS, Apr 2, 2010.

Thread Status:
Not open for further replies.
  1. DYS

    DYS Thread Starter

    Joined:
    Dec 28, 2008
    Messages:
    462
    Hello people.

    I currently have an array of Strings, like so:
    Code:
    private String catalogue[] = {"Apples", "Bananas", "Oranges", "Bread", "Milk", "Cheese", "Pizza", "Yoghurt", "Carrots", "Cabbage"};
    I have to generate a "Shopping List", using a GUI, but I need to assign prices to these array elements also.
    I plan to set up and declare the price (such as: double price; ) in a new class. But once I've done this, how do I assign a double value to the Strings as well?

    Thanks in advance :)
     
  2. DYS

    DYS Thread Starter

    Joined:
    Dec 28, 2008
    Messages:
    462
    Okay...

    I'll rephrase a little as I've done some research.

    Is there anyway I can create a multi-dimensional array that holds two types of objects? Like what I ideally would need to do is create an array to hold Strings and doubles. But I've tried doing this in my JVM, to no avail:
    Code:
    private String double catalogue[][] ({"blah blah blah"}, {8.5});
    
    That's just an example
    Basically, I need to assign a price (a double) to an item for my shopping list (String).

    I'd appreciate any help ASAP, as I have to hand in this work by the end of the week.

    Thanks people.
     
  3. Akihon

    Akihon

    Joined:
    Dec 2, 2007
    Messages:
    6
    What seems to be the problem, personally I would use a map structure such as
    Map<String, Double> shoppingList = new HasMap<String, Double>();
    then you can use the collection framework methods which would make things a damn sight easier for you and your code faster and more robust.
    Best of luck!
     
As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/914265

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice