• Post Reply Bookmark Topic Watch Topic
  • New Topic

trouble wtih calling methods, executing main, and instance/static variables  RSS feed

 
Christopher Sheridan
Ranch Hand
Posts: 50
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am a beginning Comp Sci student taking my first semester of a Java course. I had an assignment where I had to construct a simple Magic 8 Ball application. I got it to work as required with no errors. A problem arose when I wanted to keep my code and expand on the game for future personal use (fun for the kids perhaps). I will show you my original code, and then my new code, because I am having trouble figuring out where I am going wrong. The expanded version of the game will include a requirement where the program can only answer yes or no questions. If the input string does not include those key words, it will output an error message and allow them to try again.
I am having trouble wrapping my head around method calls, static/instance variables, and parameters. I am leaving the second version as is so everyone can see where I might be going wrong. I seemed to have hit an impasse when I got to methods, instance/static, constructors, etc. I simply don't understand it fully.

Any help would be greatly appreciated. I feel like I'm so close. Even if my language method is wrong (getting the string requirements to work), I can correct that later. I don't understand yet why it worked before, and crashed and burned when I started using parameters, and an additional method.

Here is the first (working version):



Here is the second. The problem is that I'm not getting ANYthing in the console. It's just blank, even though I am calling the methods in main. Also, I don't know how to get the "language" method to execute, so I've done nothing in main to invoke it:



 
Stevens Miller
Bartender
Posts: 1445
30
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

What difference(s) do you see between Line 12 of your first program, and Line 7 of your second?
 
Christopher Sheridan
Ranch Hand
Posts: 50
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see that I initialized my boolean playAgain to true on the first, and did not on the second. I changed that, and now it asks the first question, I answer, and it skips the random answer and goes straight to "Would you like to ask another question?" part. The language method still does not work either. I can type anything, and it will still output the random answer, even though the conditions were not met.
 
Christopher Sheridan
Ranch Hand
Posts: 50
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, I missed a little line of code in my new version. I forgot to type "randomAnswer(random)" in my playAgain method.

Like this:



I'm glad I found that. I just can't seem to get the language method to work. For example, if I type "Can I have a dog?", that qualifies as a yes or no question. However, if I type "I has dog?", I want it to print the error message, and loop to the askAnother. I feel like I'm so close, and hopefully my code gives you an idea of what I'm wanting to do.
 
Knute Snortum
Sheriff
Posts: 4276
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where are you calling the language method? Could you post your latest code?
 
Knute Snortum
Sheriff
Posts: 4276
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One thing you're doing that's not necessary:

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!