• Post Reply Bookmark Topic Watch Topic
  • New Topic

anyone know how I could fix the errors in my code?  RSS feed

 
Lesly Alcantar
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Basically its simple java code that is suppose to print out the latest, earliest, and current dates however, its not compiling and I keep getting "incompatible error" i'll put an error by where the error keeps popping. I'm not really familiar with java nor am I not that good at java and programming therefore, I'm asking for help, if anybody could help me. Please & thank you









//YOU DO THIS
//Print the average year of the dates in the dates array
//You may want to call getYear to get the year from each date
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37462
537
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to CodeRanch!

I don't get an error on that line when I try. I do get an error on these lines:


You have a String that needs to be an Integer. Take a look at the parseInt method to convert.
 
Campbell Ritchie
Marshal
Posts: 56533
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome again

I am afraid there is a lot which I think should be changed. The most serious error, in my opinion, is that you are not using object‑orientation. Nowhere are you creating an object from that class. And why haven't you got a Date class? [Beware of confusion; there are already two classes in the standard API called Date.]
Why have you got the keyword static in so many places? Do you know what static actually means?
Have you read the documentation for StringTokenizer[←link]? You ought not to use it, and the documentation tells you what to use instead.
 
Piet Souris
Master Rancher
Posts: 2042
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Campbell

Give Lesly a break! Read what he/she says in the first paragraph, consider that this forum is called 'Beginning Java',
and you're criticizing the lack of OO!

And about the use of the word 'static': the methods are called from within main(), what else would you expect?

@Lesly: what input do you supply at line 13? And what is the exact error?

Greetings,
Piet
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Piet Souris wrote:And about the use of the word 'static': the methods are called from within main(), what else would you expect?

I'm with Campbell.
If you're going to learn an OO language, then you should at least use basic OO design from the start, so your main should create an instance of the enclosing class and then call a non-static method of that class.

The beginner may not understand why it is done that way yet, but it is always better to introduce good practice as early as possible.
They can learn the 'why' later.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Piet Souris wrote:@Campbell...Give Lesly a break! Read what he/she says in the first paragraph, consider that this forum is called 'Beginning Java',
and you're criticizing the lack of OO!

Piet, EaseUp. Campbell's been on these forums for a long time, and definitely knows what he's talking about.
Me, I've been in IT for 35 years, and my major regret was that somebody didn't (or couldn't) teach me about Object-Orientation from the get go.

@Lesly: You plainly know a bit about methods and modularization, so that you don't repeat logic and don't plough everything into your main() method. Learning to create classes and objects to do what you want is just the next step along that road (albeit a major one ).

Winston
 
Campbell Ritchie
Marshal
Posts: 56533
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you everybody.
If you are learning to drive a car, you learn to drive on the roads.
If you are going to use an object‑oriented language, you need to learn to use objects.
I know there are books which hardly show creation of objects, or use of their public interface, and even the Java Tutorials fall into that trap. I do not know whether there is teaching which follows similar paths, but I suspect there is. The repeated use of static is often the result of failure to create objects; Stuart A Burkett has shown the correct way to do call methods from the main method.
 
Piet Souris
Master Rancher
Posts: 2042
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All,

first of all: my remarks were certainly not meant to be offensive in any way, or to doubt anyone's expertise.
If it was nevertheless found to be offensive, then I sincerely apologize.

I was thinking of the classical question: if someone is hungry, do you give him a fish or do you start learning
him how to fish? In that respect, I thought that Jeanne's remarks were spot on.

Anyway: I hope we didn't scare off Lesly!

Greetings,
Piet
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Piet Souris wrote:If it was nevertheless found to be offensive, then I sincerely apologize.

No probs. For my part, it was the exclamation marks I objected to more than anything else.

I was thinking of the classical question: if someone is hungry, do you give him a fish or do you start learning him how to fish?

Right. But you don't teach someone how to fish without giving them a fishing rod, do you?

Lesly is plainly past the "Hello World" stage, so it doesn't seem unreasonable to introduce the concept of creating classes and making objects do the work for you.

Like I say, I wish somebody had done it for me.

Winston
 
Campbell Ritchie
Marshal
Posts: 56533
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Apologies accepted And anything I would have said, Winston has already said better than I would have.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!