1

I am beginner to Java, so I am learning through experimentation. I wanted to create a class where it would take a type of ride as a String and number of riders as an Integer. I decided to use Map because it would allow me to keep together a ride type with its pertinent # of individuals.

//How many attendants in total
System.out.println("How many attendents today?: ");
int numberOfAttendents = Integer.parseInt(bf.readLine());
System.out.println("Total number of attendents: " + numberOfAttendents);

Map<String, Integer> typeOfRide = new HashMap<String, Integer>();
boolean run = true;
final String counterEnd = "stop";
while(run)
{
   System.out.println("What type of ride?: ");
   String nameOfType = bf.readLine();
   if(nameOfType.equalsIgnoreCase(counterEnd))
   {
      run = false;
   }
   else
   {
     System.out.println("Number of riders?: ");
     Integer numberPerType =    Integer.parseInt(bf.readLine());                
     typeOfRide.put(nameOfType, numberPerType);
    }   
 }

So the above part compiles with no problem. However when I try to add the number of riders per ride type, I am having some difficulty try to come up with a way. I understand the enhanced for loop I used is for arrays and not for map.

 //Adding individual entries of same ride
   for(Integer i: typeOfRide)
   {
      int sum = 0;
      System.out.println("Which ride type?: ");
      String typeNumber = bf.readLine();
      sum = sum + typeOfRide.get(typeNumber);
      System.out.println("Total number of riders per" + typeNumber + ":" + sum);
    }

So if y'all can suggest a method to get the sum, I would appreciate. Thanks!

6
  • Why do you ask for input from the user within your loop? What is it that you are trying to get the sum of? Commented Aug 1, 2013 at 21:13
  • @SimonAndréForsberg: I am using UI b/c when I wrote the code, it was under the assumption I don't know why type of rides will be offered. Since user knows what ride types they entered, I am asking them to re-enter the ride type they wish to know the total # of riders. Hope this clears your doubt. Commented Aug 1, 2013 at 21:26
  • Then be aware that the user will be asked about a ride type the same number of times as there are number of ride types, which is a bit strange design. Commented Aug 1, 2013 at 21:30
  • 2
    @Ram Do you realize there is actually nothing to sum then? You can only add to the map one entry for each type of ride and therefore one 'number of riders' so nothing to sum. Commented Aug 1, 2013 at 21:30
  • @JavaDevil, +1 for pointing out the concept of the map. Commented Aug 1, 2013 at 21:30

2 Answers 2

2

You are initializing sum inside the loop which causes the value of sum to be typeOfRide.get(typeNumber). Move the line int sum = 0; above the for loop.

Sign up to request clarification or add additional context in comments.

Comments

1

typeOfRide is a Map, which does not implement iterable, which should give you a compilation error above.

For the loop, you will probably want to use either typeOfRide.entrySet(), typeOfRide.keySet() or typeOfRide.values()

In addition to vinodadhikary's answer about moving the int sum = 0; to above the for-loop, looping on this should solve it.

4 Comments

The .values() method worked when I tried, but the compiler shows all the calculations. Example: water, 30; water, 40; balloon, 50. If I run the compiler for water, I want to just see 70. But the compiler prints out: 30, 70. Do you know where the problem is occurring?
@Ram that's perhaps because your line System.out.println("Total number of riders per" + typeNumber + ":" + sum); is inside the loop. I'm not sure how you want it to behave so I can't help you much there, but I'm glad I solved the compiler error for you.
@Ram you might want to read the comment by Java Devil above, and understand the concept of a HashMap. A single key can't have multiple values in a HashMap
Thanks guys for your help, I know have something else to experiment with!

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.