Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

problem writing program

 
Christie Apps
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am fairly new to Java, and am in the process of writing a program that will engage a user in conversation and i seem to keep getting stuck and find myself looking at it too much which makes me even more confused. My issues at the moment are :
1. It tells me the variable name might not have been initialized
I am sure I had this working ok before so am not sure what I've done.

and 2. Once it reads the first question and the user types in the letter A, B or C and it gives the response I can't get it to then ask the next question!

If anyone understands what I'm trying to do and can help give me a big push in the right direction please do, I would really appreciate any input.
Thankyou
 
Thomas Paul Bigbee
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Name isn't initialized, its local to the methods, method variables need to be initialized before they can be used, where as instance (class) variables are initialized to their default values

Furthermore, the case statements are taking literals so 'A' does note equal 'a'

I don't really know what KeyboardReader is doing (Its probably some sort of System.in) however it's not listed in my version of 1.5, so I can't help you there.

Hope this helps
Tom
 
Craig Wood
Ranch Hand
Pie
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't have a KeyboardReader so I used an old fashoned reader that should work
in most versions of java
 
Christie Apps
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thankyou both so much, I think I am now getting somewhere!
I took out the try catch throw bit as I have not learnt that yet, and was a little confused by it
Only problem now is that when i execute it i get the following error
Exception in thread "main" java.lang.NoClassDefFoundError: Assignment1

I have added text (another 3 sets of questions and answers) and changed back to keyboardreader (as that is what we are to use)

Here is my KeyboardReader Code


And here is my program code (I hope it is not too long to put in a post, if it is I will edit it immediately)
 
Marilyn de Queiroz
Sheriff
Posts: 9059
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Christie,

Welcome to JavaRanch.

First a bit of business. Please adjust your display name to meet the JavaRanch Naming Policy.
You can change it here. Thanks.

Now for your question. When I run your program, it seems to work just fine. Therefore, I think the problem is not with your code. Additionally, the error that you mention "Exception in thread "main" java.lang.NoClassDefFoundError: Assignment1" is pretty specific to CLASSPATH issues.

There are a couple of ways to handle classpath. One is to set it globally in your environment. The other is to use the -cp switch on the java command.

If you go into the Java in General (beginner) forum, you will probably see several threads on how to set the classpath (without even doing a search).
 
Christie Apps
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thankyou very much
Yes I think the software that has been supplied may have a few glitches, might have to have a look around for something different!
About my name Christie is my first name and I thought that was what I was supposed to use I didn't realise I had to use my first AND last name, sorry, I have now changed it.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic