Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Switch Statement Assistance  RSS feed

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am making a Text Adventure Game with a GUI as a self study project. In my game, I want users to be able to submit an int or string from the JTextField and when button is pressed, the player choice will display on the JTextArea and also the method that follows from the switch statement case will execute.

I have created a test code here to replicate the issue (excuse the ugly GUI)


This is the main class


And this is the Event:


With this test code you can see that when I select int 1 and submit from JTextField to JTextArea, "1" is displayed to the JTextArea but not the append text that follows case 1.

I have a feeling this is due to lack of knowledge of how to use switch statements with listeners. Happy for advise, links to knowledge, examples, anything that will allow me to not only get my code working, but to truly understand what I am doing. Thanks, Mark.D
 
Bartender
Posts: 2180
46
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the value of playerChoice at the beginning of testMethod()? Have you tried displaying the value?
I can't see any assign statement.
 
Mark Andrew Davis
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paweł Baczyński wrote:What is the value of playerChoice at the beginning of testMethod()? Have you tried displaying the value?
I can't see any assign statement.



So I want the value of playerChoice to be equal to whatever I put in the JTextField. For example if I put 1 in JTextField it will play case 1 , if I put 2 it will play case 2 and if i put not a stated case I will do an output of something like "not valid'.

I believe I need to some how link it back to my Listener but I am not to sure how I do this. I know if this was a console I can use scanner which then i would do playerChoice - userInput.nextInt(); (where userChoice = new scanner).

Hope that makes sense.

Thanks,
Mark.D
 
Master Rancher
Posts: 3625
39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your call to testMethod is not part of the textInputListener, which is the lambda on lines 94 and 95.
So it never gets called (well, not as part of the button push anyway).  Indeed, you see a "1" because line 95 is appending the value the user has entered.

Since you want the user input to drive the output to the test area create a method called something like getDisplayText which takes a char (since it seems like you are limiting the user to a single character), or a String.
That method will do what testMethod does, except rather than doing the append to the text area it will return the correct text String.
The switch will be then based on the String/char passed in.  No point converting to an int.

Then your listener can do on line 95:


Obviously if this becomes more complex you would want to handle this flow differently, but you should be able to limit the code changes to the getDisplayText method.
 
Mark Andrew Davis
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Your call to testMethod is not part of the textInputListener, which is the lambda on lines 94 and 95.
So it never gets called (well, not as part of the button push anyway).  Indeed, you see a "1" because line 95 is appending the value the user has entered.



Yep, this I realized. The issue is I don't know the correct way to do this...

Since you want the user input to drive the output to the test area create a method called something like getDisplayText which takes a char (since it seems like you are limiting the user to a single character), or a String.
That method will do what testMethod does, except rather than doing the append to the text area it will return the correct text String.
The switch will be then based on the String/char passed in.  No point converting to an int.

Then your listener can do on line 95:


Obviously if this becomes more complex you would want to handle this flow differently, but you should be able to limit the code changes to the getDisplayText method.



So, I actually cut some of my code off on purpose for this issue. I have limited the code because on this particulate text adventure games I will gave three types of scenarios for user input.
1. I have no text inside the player input field and when enter is pressed, it will display next appended text.
2. Only accepts ints
3. Only accepts strings

As followed:




So, In particular instances I would want the user to enter a string when for example a it prompts to enter his name. Then I would require a switch choice of 1 - 4 to choose where the player wants to go, etc.
I can set the state by calling buttonState in my code and set it when I need to change the function of my buttons, which confirmed working. The issue is, I need to connect my switch statements so they listen for user input which I can't get working...

I do thank you for your time to assist me, I am still learning so I get confused every now and then on different components due to lack of experience to link the knowledge back too.
 
Mark Andrew Davis
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thought I would include a screen shot so you know what I am trying to achieve.

As you can see default append text shows and displays some text for input. At this stage I trigger buttonState = 1; for int

At this point I also need the listener to go back to the switch statement so the switch knows what case to play.

Hope this makes sense.

Thanks,
Mark.D
Game.PNG
[Thumbnail for Game.PNG]
 
Mark Andrew Davis
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Dave Tolls wrote:Your call to testMethod is not part of the textInputListener, which is the lambda on lines 94 and 95.
So it never gets called (well, not as part of the button push anyway).  Indeed, you see a "1" because line 95 is appending the value the user has entered.

Since you want the user input to drive the output to the test area create a method called something like getDisplayText which takes a char (since it seems like you are limiting the user to a single character), or a String.
That method will do what testMethod does, except rather than doing the append to the text area it will return the correct text String.
The switch will be then based on the String/char passed in.  No point converting to an int.

Then your listener can do on line 95:


Obviously if this becomes more complex you would want to handle this flow differently, but you should be able to limit the code changes to the getDisplayText method.




Sorry, are you able to show me an example? I am a little confused. I greatly appreciate your assistance
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!