• Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem with my code or Problem with Eclipse Luna?  RSS feed

 
Jb Thomas
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys. I'm just learning Java. I decided to write a little interaction program for my kids who love dogs. This would be a good chance for me to work on my object creating and practice my setter and getter methods and use keyboard input. Im using Mac OS Yosemite, and running Eclipse Luna. I encountered a strange instance where Eclipse is not alerting me to anything wrong other then not closing the keyboard input. At one point my keyboard would not accept input so that further confused me. My program consists of two Java files, and they debug just fine and don't create any errors inside Eclipse. The program runs fine in the Eclipse console, up until this line of code:



At that point in the Console interaction, it doesn't wait for the user input to enter the dogs color. It skips right over it, and goes onto spitting out the next lines of text and not waiting for their input either. Due to Eclipse not generating any errors when I compiled the code, I decided to compile it from the MAC Terminal Window. My MakeDog.Java file listed two errors on line 34. Saying "Cannot Find Symbol" and referring to "Dog".
What is wrong with this line of code and why doesn't Eclipse have a problem with it?




 
Stefan Evans
Bartender
Posts: 1837
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My guess is that you are compiling it from the same directory as the MakeDog.java file.
Instead you should be compiling it one level above that - from the root of your packages.
ie javac dog\MakeDog.java

That way it can then find and compile the Dog class as well.


With regards to why things are rushing straight on without waiting for input is related to your nextInt() call in asking the dogs age, and the way the Scanner class work
If you ask the Scanner for the next thing, and it still has input to look at, it will grab it from there.

The interaction goes:

>How old is Sandy?
(user enters 5 and hits enter)

This adds two things to the list of things Scanner will process. The number 5, and a carriage return.

The call to nextInt() will read the number 5 - but no more.
The call to nextLine() finds input waiting to be read, and reads up to the end of the line - which probably results in blank

This was the first time in your program you used nextInt() so the first time you encountered the problem.
Putting in a call to nextLine() will clear that enter key after entering the dogs age, and your program should continue as expected.


 
Jb Thomas
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok. I think I follow that pretty well. I have one last question in regards to good practice in these situations.
Should the book I am reading have taught me to use nextLine() method call after using nextInt() for a situation such as this?

Looks like I just need to add this line of code after the nextInt() call.

 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ouch!
What long lines! I have broken most of them so you can see the correct way to do it.
Ouch!
What a long main method. That should be broken up into lots of separate methods, preferably instance methods of a Game class.
If you have called nextXXX on a Scanner instance you may have to call nextLine twice to get a sensible input. Look at this old post.
 
Jb Thomas
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply and reminder about my Main Method. I should have known that placing the user responses inside another class would have been smarter and cleaner.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should have some class which you run the Dogs from. That ought to have been in your planning from the very beginning.
 
Jb Thomas
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:You should have some class which you run the Dogs from. That ought to have been in your planning from the very beginning.


At this time, this was coding experiment to practice object creation and working with setter and getter methods. A learning exercise for me. Since the program has no other purpose to it's existence - the main method worked well. But yes, with more purpose to this tiny application experiment, I do agree running the dog building in an outside class would be a smart idea. Perhaps, if I decide to play some more and introduce some other pets like cats, fish and birds... then doing this would be much much easier and extendable.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry for delay in replying.

You should get into good habits from the very beginning.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!