aspose file tools*
The moose likes Java in General and the fly likes ArrayList error on Java 1.4 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "ArrayList error on Java 1.4" Watch "ArrayList error on Java 1.4" New topic
Author

ArrayList error on Java 1.4

Salma Youssef
Greenhorn

Joined: Sep 01, 2013
Posts: 20

I'm having the worst day! I sent in an assignment for a program that I created using Java 1.7 and my teacher sends it back saying, "re-do it for version 1.4". I spent so long just writing the first code! Having to re-write it for 1.4 (where I can't use generics) is going to be impossible. It's an assignment about reading and writing from files. I tried reading about it, but I've resolved to come ask here. I just need help changing my code so that it works it 1.4 (so, I can't use ArrayList). I'm not asking you to do it for me, I just need guidance and advice on what to change (please try to be thorough ). Any help would be appreciated!

My "Grades" class:



And here's my main code:



Thanks again in advance!
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14144
    
  18

As you already know, Java 1.4 does not have generics, so you should remove all use of generics, and you'll most likely need to add some casts to your code at the places where you get objects out of the ArrayList.

Remove the generics in line 6.

Add casts in lines 41, 42, 43, 52, 53, 57, 58; everywhere where you call get() on the ArrayList, you need to cast the result to Student.

Why do you need to use Java 1.4? That's an ancient version for which there has not been support from Oracle anymore for years.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Salma Youssef
Greenhorn

Joined: Sep 01, 2013
Posts: 20

Jesper de Jong wrote:As you already know, Java 1.4 does not have generics, so you should remove all use of generics, and you'll most likely need to add some casts to your code at the places where you get objects out of the ArrayList.

Remove the generics in line 6.

Add casts in lines 41, 42, 43, 52, 53, 57, 58; everywhere where you call get() on the ArrayList, you need to cast the result to Student.

Why do you need to use Java 1.4? That's an ancient version for which there has not been support from Oracle anymore for years.


Yes, I remember reading about this! Could you please give me an example on how I'd add a cast?
I hate it! It's because of my teacher. I'm completely against using 1.4 because it's missing a lot of features and every code I write is so limited on what I can use! I can't even use the Scanner class! But he's insistent on it.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14144
    
  18

For example, instead of:

write:

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38765
    
  23
And you will need a buffered reader wrapped round a file reader to read a text file, instead of a scanner. Remember those readers don't do their own exception handling, but a scanner handles most of its own exceptions.
Salma Youssef
Greenhorn

Joined: Sep 01, 2013
Posts: 20

Jesper de Jong wrote:For example, instead of:

write:



The casting isn't working at all. I declared the "Student" object in the class and tried the casting, but my IDE was giving me errors.

I wrote:



And the IDE said "cannot find symbol, symbol method get(int), location variable students of type Student". What's the problem now?!
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4387
    
    8

When you say "I declared the 'Student' object in the class", what do you mean? You shouldn't have to change any declarations. That error message looks like you've declared students as a Student instead of a List.

There's also a crucial difference between what you've done there and what Jesper wrote. The parentheses are different, and that actually completely changes the meaning. Which will cause an error, but I don't think that's the error you're getting right now.
Salma Youssef
Greenhorn

Joined: Sep 01, 2013
Posts: 20

Well, I removed the generics, so I needed to add something to declare it, correct?

I realized what I did, so I changed it to



But how could I incorporate the "Student" object? It's still giving me the "cannot find symbol" error.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4387
    
    8

Salma Youssef wrote:But how could I incorporate the "Student" object? It's still giving me the "cannot find symbol" error.

You've got the right declaration now. Is it giving you exactly the same error? Or is it complaining about number now? That would be the second point I made. Have a look at Jesper's code again compared to yours.
Salma Youssef
Greenhorn

Joined: Sep 01, 2013
Posts: 20

I looked at Jesper's code and realized that I was missing a set of brackets. (Oh, the frustrations of coding...)

My code works perfectly now! Thank you!
Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2850
    
  11

It works until tomorrow when the teacher says, "Now do it in Fortran!"

Seriously though, Java 1.4 is over a decade old. Your teacher needs to update his/her assignments.
Salma Youssef
Greenhorn

Joined: Sep 01, 2013
Posts: 20

That's what I've been saying. I sent in another assignment a few days ago, but I used 1.7. I just hope he doesn't tell me to fix it to 1.4. It's so ancient. What's worse is that he has 1.7 installed on his computer, but he had us download 1.4 and insists on us using it.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38765
    
  23
Although I agree that Java1.4 is very much out of date, there are millions of lines of Java1.4 and even Java1.3 code out there, which have to be maintained. You do unfortunately have to learn old Java some time or other.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ArrayList error on Java 1.4