• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Problem with Treemaps

 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying to make a program that reads input from the user that can be redircted from a file that contains a drivers name rego number start place end place starting kilometres and ending kilometres and then displays an output of the total distance travled. I am having trouble with trying to put something in. Below is my code



I am not too sure even how to use treemaps any help as to how to fix this problem would be great
 
Ranch Hand
Posts: 823
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Peter,

You really need to state what your problem is, i.e. compilation error "java.util.Map cannot be applied to (java.lang.String, int)". I had to take the trouble to compile your code to find out.

Anyway, the solution is:

The reason for this is that TreeMap.put() takes two Objects as parameters. As you probably know, int is a primitive type, not an Object. So you need to use the wrapper class, Integer.

Hope that helps.

Jules

P.S. You can't have 2 public classes in the same file either (if they are).
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Julian Kennedy:
The reason for this is that TreeMap.put() takes two Objects as parameters. As you probably know, int is a primitive type, not an Object. So you need to use the wrapper class, Integer.



Unless you are using Tiger (Java 5), whichs autoboxing feature would create the Integer object automagically for you.
 
Peter Shipway
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
whoops thats embrassing of course maps can only store objects not values or primative types and yes they are in differnt files i just put them like that to save time i was in a bit of a hurry ty for the help guys
 
Peter Shipway
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok i have just hit that brick wall i was afraid of i am up to processing the treemap and printing it, first let me explain how i am atempting to print it, i only want the drivers name and the total distance they have travled (k) e.g endk - startk. Eg instead of

Peter 5km
Peter 10km

Total 15km

I want

Peter 15km

Total 15km

Here is my new code with some minor changes





The problem is this i havent got a donkeys clue how to go through the treemap let alone add up all of the differnt distances travled since this is the first time i have used a treemap and still havent been able to find a simple example i have found one but it had more than 5 classes and enough commenting to sink a java battleship.

Any help, hints, code, examples would be greatly apreicated (please excuse bad spelling i am half alseep)
 
JulianInactive KennedyInactive
Ranch Hand
Posts: 823
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's an idea - why don't you create a second Map (TreeMap if you want it sorted) and sum the distances as you iterate through your array? The Map would be keyed on name. If the name is not contained in the Map you add a new member; if it is then you add the distance to the running total.

One way to iterate through a Map is as follows:

Jules
 
Peter Shipway
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have asked my teacher for help and he basiclly butcheded my code till it didnt work. Cant i just modify how i create my treemap so that it only has one of each name (key) then just adds the values? how would i do this?
 
author
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The most important clue you're missing is that in your final for loop, you will want to test whether you have already seen the car and add the distance to the existing value if you have:As Julian already indicated, you can iterate over all the entries in the map using an iterator. You could print the results usingThis will print the total for each, and the total for all drivers.

I realise that you're only just starting on your Java journey, so what follows may be overkill. Just check out what is meaningful to you and ignore what isn't (yet).[list]You are declaring all your variables way too early, give them a scope that is far too large, and you also initialise them where you don't need to. Instead ofYou could writeBecause you never intend to use the variables without first assigning to them (the same can be said about your Car class). This way, you make that very clear, and as an added bonus the compiler will warn you if under any circumstances you might use the variables without assigning first. But even better would be:This reduces the scope of the variables to the absolute minimum necessary. Reducing the scope makes the code much easier to read and maintain, because you're explicit about where you will be using these variables and where you won't.
  • Along similar lines, the declaration of the "tm" variable should be moved to right before the for loop.
  • [list]Staying on the subject of scope, turning your first while loop into a for loop allows you to restrict the scope of "line" to the loop:Not everyone will like this idiom, though.[/list]
  • Using a fixed-length array for your cars means that you're building arbitrary limitations in your application (100 cars in this case). A List, for example an ArrayList, is usually a better idea.
  • The fields in your Car class don't want to be initialised, as mentioned above. They also should be private. Finally, seeing that you are assigning them in the constructor and never changing them, you can just as well make that explicit by making them final. That way, everyone can immediately see that the Car class is immutable.
  • In JDK 1.4, use of String.split is encouraged over using the legacy StringTokenizer class. In this case, it does make things a little simpler.
  • Your run() method does three different things: capture the list of cars, sum the distances traveled, and print the results. Correspondingly, these operations should be implemented in three different methods.
  • With these modificatons, the code starts to look like this:There's more that could be said, but I'm afraid I've gone on for long enough now Hope this helps,

    - Peter
    [ September 01, 2004: Message edited by: Peter den Haan ]
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    reply
      Bookmark Topic Watch Topic
    • New Topic