• Post Reply Bookmark Topic Watch Topic
  • New Topic

Trying to call another class  RSS feed

 
Michael Scalzetti
Greenhorn
Posts: 20
Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys!
I'm trying to make a sort of multi-purpose program. I decided I would use multiple classes to do this but I forgot how to call an entire class. Whenever I run this in eclipse it just stops running after the user responds (line 26). I'm not sure if the while statements are even running at all... HELP!!

(Test is the 'hub' class
RPS is the class trying to be called on)
*Sidenote: sorry if my code is a bit odd, I am teaching myself Java until I can get a proper tutor (or something)...



 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't call a class. You create an object from it and call its methods. You are doing so with Scanner. Then you go and use the == operator on Strings, which you can be sure won't work.
 
Michael Scalzetti
Greenhorn
Posts: 20
Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:You don't call a class. You create an object from it and call its methods. You are doing so with Scanner. Then you go and use the == operator on Strings, which you can be sure won't work.

So if this is the case, how should I go about calling the entire class. I keep getting completely lost trying to find something online.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't call a class, as I have already told you.
You create an object and call a method on a reference to that object, as you are doing with your Scanner instance. In exceptional circumstances, you may need methods which are not related to objects and you might make them static, in which case you call them by ClassName.methodName(...)
But you don't call classes. Tell us what you are trying to do, then we can help better.
 
Michael Scalzetti
Greenhorn
Posts: 20
Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am simply trying to play the rock paper scissors game inside of the Test class. I'm not sure if calling a method would even work... Or how I would go about doing that. As I said earlier I am a self taught n00b.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
new RockPaperScissors().start();
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
...but make sure it has a method of that name.
 
Michael Scalzetti
Greenhorn
Posts: 20
Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure what you mean by that. Please keep in mind I haven't done this before and that I have no idea what I'm doing. If you can give me examples in fake lines of code that would be much appreciated.
 
Michael Scalzetti
Greenhorn
Posts: 20
Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe I should explain where my code is right now. All the code that I am planning to use is functional independent of one another. The problem is whenever I try to make a method that runs the RPS game the code self-terminates. If there is a reason for this, I don't know what it is.
 
Junilu Lacar
Sheriff
Posts: 11481
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The reason your program stops is that you're comparing the String variable action to a possible value using the == operator. When comparing String values, use equals(), not ==.  The reasons for doing so are explained here: https://coderanch.com/t/674031/difference-equals

Also, using while loop like what you're doing on lines 28-38 will trap you in never-ending loops.  Once you enter one of these loops, nothing is done to change the value of the action variable so you'll be stuck in there forever playing the same game over and over. You'll have to manually stop your program via your IDE or by CTRL-C if you're running the program from the command prompt.  Instead of a while loop, you can use either a switch statement or an if-else statement:

Avoid calling System.exit(), especially if you just want to end your program normally. Reserve the use of System.exit() for situations where you know other programs will be checking the OS exit code after your program runs to check for abnormal termination. When this is the case, you'll want to call System.exit() with a non-zero argument to tell other programs that are checking the OS exit code that your program was terminated due to an error or abnormal condition.
      
 
Michael Scalzetti
Greenhorn
Posts: 20
Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
YES!! Thank you so much. The if statement was the problem. As for the while loop thing you mentioned. I am planning to make the program loop until the user decides to quit.
 
Michael Scalzetti
Greenhorn
Posts: 20
Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For those who are wondering what the final code looks like, here it is.

main(hub) class



rock paper scissors class




magic 8 ball class

 
Junilu Lacar
Sheriff
Posts: 11481
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you try running it to see if you can actually stop playing one game, go back to your main menu and choose to play another game?  It doesn't look like your latest version will let you do that. 

By the way, I'd never call any version of a program "final" until I actually walk away from it forever. A program is never done; sooner or later you're going to find something that needs to be changed.
 
Michael Scalzetti
Greenhorn
Posts: 20
Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, it doesn't. This is the final version of this specific update to the code. Sorry if I didn't say that. I am thinking of adding more to the current code(more games, settings, gui, and as you mentioned, the ability to switch games).
 
Michael Scalzetti
Greenhorn
Posts: 20
Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anyhow, I am going to mark this thread as closed, there is no reason to keep it open at this point in time.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!