• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ArrayList error on Java 1.4

 
Salma Youssef
Greenhorn
Posts: 20
Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15480
43
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Salma Youssef
Greenhorn
Posts: 20
Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15480
43
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For example, instead of:

write:

 
Campbell Ritchie
Sheriff
Pie
Posts: 50168
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 20
Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 20
Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 20
Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2993
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 20
Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 50168
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic