aspose file tools*
The moose likes Beginning Java and the fly likes compiling help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "compiling help" Watch "compiling help" New topic
Author

compiling help

charlie mills
Ranch Hand

Joined: Nov 01, 2008
Posts: 49
I have the following code but its not compiling. Its saying that there are incompatible types on line 63
The code is supposed to print out the highest temperature and its corresponding city name. This is my first attempt at writing the code so I'm not sure if it does what it's supposed to. However, I want to try and figure it out for myself, so if you could just help me with the compiling issue and none of the other mistakes(if there are any).
Thanks

I've cut out some of the code because its not relevant to my problem.

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39415
    
  28
Find the generics part of the Java Tutorials, and have a look at that. I think it will work of you give your temperatures List the actual type parameter nearest to what you are trying to put in it (<Integer>).
If you don't supply a parameter, then the compiler will not realise you are only putting numbers in the List, and won't like getting them back out.

Integer and int can be swapped with boxing and unboxing; generics and boxing are features added in Java5, so they won't work on older versions. I presume you mean the line labelled 63 here.
Omar Al Kababji
Ranch Hand

Joined: Jan 13, 2009
Posts: 357
whats the type of your variable "temperatures" which you use, and what version of java are you using ? java 4 or java 5 or more ?


Omar Al Kababji - Electrical & Computer Engineer
[SCJP - 90% - Story] [SCWCD - 94% - Story] [SCBCD - 80% - Story] | My Blog
charlie mills
Ranch Hand

Joined: Nov 01, 2008
Posts: 49
Campbell Ritchie wrote:Find the generics part of the Java Tutorials, and have a look at that. I think it will work of you give your temperatures List the actual type parameter nearest to what you are trying to put in it (<Integer>).
If you don't supply a parameter, then the compiler will not realise you are only putting numbers in the List, and won't like getting them back out.

Integer and int can be swapped with boxing and unboxing; generics and boxing are features added in Java5, so they won't work on older versions. I presume you mean the line labelled 63 here.


ah ok it all makes sense now. thank you
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39415
    
  28
charlie mills wrote:ah ok it all makes sense now. thank you
After one of my explanations?

You're welcome
charlie mills
Ranch Hand

Joined: Nov 01, 2008
Posts: 49
yep lol. we've covered it in lectures, but it didnt occur to me that that was what was wrong. I got it to compile but the output wasn't what I wanted so I've been messing around with it and have come up with:


but now it doesnt compile. I want to print the highest temperature and then its corresponding city. My thought process was that I sort the temperatures, get the value of the highest one, find the position of the value in the unsorted ArrayList and then the corresponding city would be in the same place in the city List so I could print that element in the city arrayList. I think that makes sense.

I'm getting errors compling it though. Its saying:

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 20, Size: 4
at java.util.ArrayList.RangeCheck(ArrayList.java:547)
at java.util.ArrayList.get(ArrayList.java:322)
at CitiesAndTemperatures.controlMenu(CitiesTempsAppApp.java:144)
at CitiesAndTemperatures.main(CitiesTempsAppApp.java:175)


This is my main method if its relevant at all:


Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39415
    
  28
That exception suggests you are trying to get no 20 (the 21st entry) out of a List which contains 4 entries!
charlie mills
Ranch Hand

Joined: Nov 01, 2008
Posts: 49
oops. the value in the arrayList is 20.
I want to search for the value 20 in the unsorted ArrayList and store its position. (ie. 1,2,3 or 4). How would i do this?
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2316
    
  49
ArrayList has an indexOf(..) method. Is this what you were after?
charlie mills
Ranch Hand

Joined: Nov 01, 2008
Posts: 49
yeah, i tried that and it's sort of working. it'll compile and run, but its not giving the output that i want. Its just displaying the name of the first city in the cityArrayList, and not the one that corresponds to the highest temperature. Not sure where I'm going wrong
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39415
    
  28
You are calling the first temperature. Have you sorted the List? How? Do you need to reverse it?
charlie mills
Ranch Hand

Joined: Nov 01, 2008
Posts: 49
yeah i'm calling the first temperature in the sorted list, which is ordered highest to lowest - so descending. i then want to note this value (which is 20 - from my tests data) and find what position 20 is, in the unsorted arrayList (temperatures) - say the 3rd one in the list. I then want to print out this same position (3) in the cities ArrayList - say london.
the code at the moment isnt getting the right city, its just printing the first one in the list and i dont know how to fix it
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2316
    
  49
It seems to me like you are using the wrong approach. If the temperatures and cities are linked data then they should either be stored together ie in an object or they should be stored in a map type data structure, they should not be stored in separate Lists.

For example:

1. Using Memento type Objects
Create data objects that just hold the temperature and city name for each data pair and store the data objects in an Arraylist. Sort the ArrayList using a Comparator object that compares each data objects temperature. The highest temperature will now be at one end of the list. This is a little more complex than using a Map but much more robust and flexible. For example you can sort the list in temperature order or alphabetical city order or a combination of the two depends on the comparator you use when sorting.

2. Using a Map
You could use TreeMap keyed on the temperature with the cities as the values then you would automatically have sorted data and could easily find which city any temperature belongs to. The big drawback to this approach is each key must be unique so you couldn't have two cities with the same temperature. The other option is to use the city names as keys but then sorting the map in temperatur order is more difficult.

Personally I'd go for option 1, the benefits are worth the extra effort.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: compiling help