So im creating a game of blackjack as part of an assignment for my college. I have to create some kind of method to allow the player to Hit or Fold.I decided to write this part of code which checks the input:
do {
System.out.println(playernames[j]+" (H/F)");
playerinput=input.next();
} while (!((playerinput.equals("H"))||(playerinput.equals("F"))));
Everything runs smoothly until it reaches this loop.The workspace im working(eclipse) doesnt show any syntax problems nor any java exceptions occur.I tried editing this to find what is happening by using a temp boolean set to false before the loop and after the loop it gets set to true.The output didnt change meaning it didnt get past the loop.
This code works, but only if the user types a capital H or F. Maybe that's the problem? If you want to allow both lower case and upper case letters, you can use playerinput.equalsIgnoreCase("H").
Also, input.next() will only return after the user has pressed enter. Just pressing the H or F key is not enough.
Related
I'm trying to use a while loop for email validation in JFrame (An email has to have "#" and "com" in it). So far I have:
while(!emailInput.getText().matches(".*[#com].*")) {
if (emailInput.getText().matches(".*[#com].*")) {
break;
} //from if
}
if((!emailInput.getText().matches(".*[#com].*"))) {
JOptionPane.showMessageDialog(null, "Your email does not seem to be valid. It should be in the form of myemail#myemail.com. Please try again!");
}
Of course, the while run will be an infinite one - once I tested the program by entering a random email (say, "test"), the condition will always be false. I dare not to put show the dialog (saying that the email doesn't seem right) in the while loop because it will also show the message infinite number of times.
But that is a little bit counterintuitive - since I want the user to keep entering the email until he gets it right (break in while loop).
I have tried to use only if statement but it seems that my if code only works once - so I'm counting on while loop. However, is there any way to fix this, even without using while loop? I am using this for password validation and I am having trouble as well. I am open to any suggestion.
As Sebastian suggested, the solution is to use a button, get rid of the while loop, and just use an if statement. The reason is that if the user clicks on it, the program would have checked the validation - and it checks the validation whenever the user clicks on the button. The process ends when the user enters a valid email and clicks on the button for the final time.
I'm building an application on Processing using NyARToolkit, but my question is not directly about NyArToolkit but about a key released() method.
The thing is, I show a card and then I can do a few things if I press different keys. I press "X" and it shows one thing, I press "Y" and it shows another thing. The problem is, it shows the info from the last key pressed all the time.
If I change my AR card, it will immediatly show the info from the last key pressed. I would like to do something to release the key, something like: just show while i'm pressing the key, or have an "ESC" to stop showing everything.
I've been reading about the keyreleased() method but I didn't figured it yet out to put it to work.
By the way my method is like this:
if(key == "c") then
else if(key =="d") then...
How about a boolean keeping track wether you've pushed something
private boolean buttonIsPressed = false;
and inside your keyPressed
buttonIsPressed = true;
and inside your keyReleased
buttonIsPressed = false;
This way you can keep track wether it's pressed or not, using a String you can also keep track which button is being held by initializing it in your keyPressed and making it null in your keyReleased.
If I missunderstood your question let me know.
EDIT:
Reading your edit, you're making quite a big mistake there, make sure to use .equals to compare strings so if(key.equals("c"))
Again, your question is not quite clear for me, so if I'm wrong, excuse me.
I'm a beginning computer science student and we've been asked to complete a project that animates random generated car objects moving to randomly generated parking spots on a city map grid. I've developed a GUI for the buttons, text fields, and text areas. Everything works as required except for at the end of the animation, when all cars have reached parking spots, I need to display analytics in my JTextArea. My buttons are set up properly and I will show you where the code takes place below:
else if (e.getSource() == start) {
setAnimate(true);
if(simulator.simulationFinished()) {
createAnalytics();
}
}
So here I have implemented an action listener on the button "start" that begins the animation. Currently it:
currently it begins the animation and all the cars travel to the parking spots as intended.
it displays analytics that are derived from before the animation began (Analytics include: car ID, number of moves, average number of spots tried, average distance travelled)
then the animation will conclude.
If I press the start button again it will display the proper analytics.
I know that in order for the program to display the right analytics the moment the program finishes I most likely need a while loop, however I haven't been able to dream anything up that will not create an infinite loop and require me to manually terminate the program via console.
while(!simulator.simulationFinished()) {
if(simulator.simulationFinished() == true)break; {
createAnalytics();
}
}
I've also tried this among several hundred other variations of all the loops in existence. Following from my logic, I need the while loop so that it will keep checking to see if the simulation is finished so that I can execute my method that generates analytics, but if don't give the while loop something to do it just goes on forever and crashes. I'm at a loss, any help would be appreciated. Thanks.
First: no, you don't need a while loop necessarily. You could use the observer pattern instead (for example) and your object would be notified as soon as the simulation had finished. As to your question: the if inside the while is obviously superfluous (as is the == true). The real problem seems to be that simulator.simulationFinished() never returns true. Could you post the code of that function and the code (and any code that directly influences the return value of it)?
Without going into details, there's a serious problem with your while loop.
See, it loops as long as simulator.simulationFinished() returns false.
However, within the loop, you check for the opposite - which will never happen.
I would recommend using listeners of some sort, although the requirement is not clear enough for me to advise any further.
Im currently trying to make a clone of the game "Lumines" in java. I only have a couple of months of experience in simple java, and previously tried as hard as possible to not bring my problems to the internet, but i have been working on this bug for hours to no prevail. If you're not familiar with Lumines, watch this video: lumines video . In the video, you will see that when a row of a block falls on top of an already laid block, the outer row continues falling. This is where my bug is. I have successfully got it so the outer column will continue falling in most positions, but if you spin one block once and drop it to the floor, and spin another block once and drop it so the right outer column is supposed to continue falling, it doesn't fall.
Since there is alot of source code/images, and it is so bad it's embarrassing (i was getting around to making it more efficient, i just wanted to get the major parts down first ), I will upload the files for you to download here: source/images
When you run it, press the up arrow once and wait for the block to fall. when the next block appears at the top, press the up arrow once and the right arrow once. wait for that block to fall and you will see the bug.
(dont laugh at my 1337 c0d3z )
I'm finally making the voyage back to IntelliJ via Eclipse. Currently my Eclipse is set up so that if I currently have a statement such as this (where ^ denotes where my cursor currently sits):
foo.bar("hello"^)
and I hit the semi-colon (;) key, it will automatically put the semi-colon at the end of the statement:
foo.bar("hello");
Currently IntelliJ does this:
foo.bar("hello";)
Meaning I will have to explicitly type the closing bracket before typing the semi-colon.
Not a huge problem obviously but I have found myself putting the semi-colon in the wrong place a few times today as I make the transition back to IntelliJ and thought that it may be more efficient (for both my fingers and brain) not to have to type the closing bracket.
For Windows or Linux users, Ctrl+Shift+Enter.
For macOS/OS X users, ⌘ Command+⇧ Shift+Enter.
That finishes the statement you're currently writing. Try it in a few different situations, like in if statements, for loops etc, and you'll see that it'll complete the line and open some curly braces for you if necessary.
You can add a keymap of your own. I added Ctrl+; to my Keymaps for the "Complete Current Statement" action. This saved me an extra key stroke and made it a little bit more intuitive.
Go to Settings > Keymap
In the search box, enter "Complete Current Statement".
Right-click on the entry and select "Add Keyboard shortcut"
With the First Stroke field hightlighted, enter your key combination. E.g., Ctrl+;, which will display as Ctrl+Semicolon
Click OK, and OK to save and exit.
You can opt to remove or keep the existing mapping. Should look something like this when you're done:
if you want always have it just do this