-2

Right now my program is outputing the data I would like it to (I checked it with some system.out.println statements) but I want to convert the two arrays into a hashmap that includes both. I do not know how to do that. Please help!

Here is the code I have:

import java.util.*;
import java.io.File;
import java.io.FileNotFoundException;

public class Vending2 
{
  public static void main(String[] args) throws FileNotFoundException
  {
      System.out.print("Enter your food selection file: ");      // User inputs file
      Scanner input = new Scanner(System.in);                       // Keyboard input from user
      String filename = input.nextLine();
      Scanner fs = new Scanner(new File(filename));         // Scans in the file that was inputed

        String line;
        double price;
        int num = 0;
        while(fs.hasNextLine()){
             line = fs.nextLine();
             price = Double.parseDouble(line.split(":")[0]);
             num++;
        }

        fs.close();
        fs = new Scanner(new File(filename));

        double[] value = new double[num];                        //Array for prices
        int i = 0;
        while(fs.hasNextLine()){
             line = fs.nextLine();
             price = Double.parseDouble(line.split(":")[0]);
             value[i] = price;
             i++;
        }
        System.out.println(Arrays.toString(value));

        fs.close();
        fs = new Scanner(new File(filename));

         ArrayList<String> foods = new ArrayList<String>();  
        while( fs.hasNext()){

            String str = fs.nextLine();
            String words[] = str.split(":");
            if (words.length > 0){

                for(int j=1; j < words.length; j++){
                    foods.add(words[j]);
                }
            }
        }
        System.out.println("foods="+foods);


    }
  }

And here is what it is outputing so far (this is what I want to put into a hashmap) :

1.0, 1.5, 1.5, 2.0, 1.0, 1.0, 1.25, 0.75, 1.0, 1.0, 1.0, 1.5, 1.25, 0.5, 0.5, 0.5, 3.5, 1.75, 0.25, 1.5]
foods=[Honey roasted peanuts, Cheetos, Bugles, Synder’s Pretzels, Snickers, Twix, M n Ms, Life savers, Twizzlers, Nutter Butters, Butter Fingers, King Size Kit Kats, Carrot sticks, Juicy Fruit, Spearmint Gum, Five gum, Pepperoni, Cheez-Its, Slim Jim, Lays Barbeque Chips]
6
  • 1
    So you want to map food names into respective prices? Oct 19, 2015 at 19:03
  • 3
    Your current history of questions gives the (strong) impression that you are using stackoverflow as a "homework-making-machine". However, a question like this one may be legitimate, and if you tried to phrase it in a form that is more appropriate for a Q/A site, then they would likely be percieved better. This refers to details of the wording ("I want to" vs. "How to"), but also to focussing on the core of the question: No Scanners, no files, just a main method with two hard-coded arrays. Also, you should be precise about which array contains keys and which one contains values
    – Marco13
    Oct 19, 2015 at 19:08
  • It would be best if you mapped them straight out of the file. Can you add a sample from the file to your question? Oct 19, 2015 at 19:09
  • @RealSkeptic See stackoverflow.com/questions/33219776/… ... ... ...
    – Marco13
    Oct 19, 2015 at 19:11
  • @Marco13 Interesting. Then I wonder how split(":") would work at all... I suppose your previous comment makes sense. Oct 19, 2015 at 19:14

2 Answers 2

2

assuming both foods list and value array are always of the same length, so you can loop over them together.

Map<String, Double> map = new HashMap<String, Double>();

int i = 0;
while (i < foods.size() && i < value.length) { //for safety i will check on both sizes
    map.put(foods.get(i), value[i]);
    i++;
}
1
  • 1
    Could you please add some explanation to your answer? Code-only answers are frowned upon on SO.
    – honk
    Oct 19, 2015 at 20:28
1

If your two arrays/list have same length, just do :

Map<String, Double> map = new HashMap<>();
for (int i = 0; i < value.length; i++) {
    map.put(foods[i], value[i]);
    // or map.put(foods.get(i), value[i]) if foods is a List
}

Assuming there is only one price for one food.

6
  • How do I implement this into my given code? where do I put it without saying that "i" has already been defined. (Sorry I am VERY new to java)
    – Cliff
    Oct 19, 2015 at 19:38
  • 1
    Just rename the variable into j. Also, you probably need a `Map<String, Double> if your prices are not integers. Oct 19, 2015 at 19:52
  • Ok I did that and it says "array required".
    – Cliff
    Oct 19, 2015 at 19:58
  • You need foods.get(j) because it's a list, not an array. Oct 19, 2015 at 20:04
  • Thank you, but then it says "cannot find method "foods(int)""
    – Cliff
    Oct 19, 2015 at 20:20

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.