#My game doesnt work and i dont know why
1 messages · Page 1 of 1 (latest)
While you are waiting for getting help, here are some tips to improve your experience:
If your code is long, or you have multiple files to share, consider posting it on sites like https://pastebin.com/ and share the link instead, that is easier to browse for helpers.
If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.
Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (won = false) {
try {
String TextFromTextfield = jtextfield.getText();
Integer Anumber = Integer.parseInt(TextFromTextfield);
Guesses++;
NumberTest(Anumber);
} catch (Exception error) {
text1.setBounds(130, 10, 200, 30);
text2.setBounds(25, 25, 300, 30);
text1.setText("Halt!");
text2.setText("Bitte gib eine Zahl zwischen 1 und 100 ein.");
}
} else {
}
}
});
this is what the button used to guess does. if you dont put a number into the textfield it tells you to "Please insert a number between 1 and 100."
now heres my problem: after restarting the game using the button after winning, it always tells you to insert a number between 1 and 100. ive tried many things, googled and asked friends, still no clue. so, what have i done wrong?
start with a few things
find out the actual exeption and then place a debug point on either NumberTest( or Guesses++
from the small section u posted and the behavior, its most likely NumberTest that is messing up
your code cant trigger on won condition being false or null ( yes null , u used a Boolean, not a boolean ) so its making an exepetion on one of those 4 lines. Debug mode will find it in minutes
can you tell me what to do exactly? i actually just started with java like 3 days ago, i used ✨ scratch ✨ before. i havent used debug mode yet
print out the exeption see what it is
your basicly telling, Hey , if somethign goes wrong, dont care what it is, just print out the text
in your code are 3 main places where it can go wrong fast so need to isolate them or debug them
print out the error
how do i do that?
will tell u what is wrong, where and the type
syso the 'error' variable should usually do it
syso?
system out
there u go, that's why its failing , its cant translate from the field
when u win the game, are u resetting the fields with "" ?
yeaa it was from another thing i had in the game
that's the reason
i know the code and the trick. Alter your error handling or your flow to avoid that
it works, removed the
jtextfield.setText("");
but is there a way to start with the textfield being empty?
humanly, that's the way
u just need to do a little trick for your backend to also know
well if u read the (humanly visual empty field) , it wont be empty, but have a value
we see it as empty, but as value, its ""
for example as in your case, u need to grab a number, but your also with a use case where it can be ""
so u need to tell your code, grab a number , and watch out because it can be empty or ""
ok, let me try
also change your Boolean to boolean if u dont need the third condition from Boolean ( boolean=true/false Boolean=true/false/null )
how do i do it?
either u handle the exeption and ask again, of u just add some code to validate the field ( which already should of been there )
why does
String TextFromTextfield = jtextfield.getText();
Integer Anumber = Integer.parseInt(TextFromTextfield);
ignore ""?
it doesnt
the first line reads that , the second tries to convert it to an Integer, and as its no number, it fails
so it gets taken out because the input is ""50 or any number, right?
"" means emtpy field
after your win, your setting it to "" and then trying to read it as an int
or is there an action u forgot to mention ?
i type smth into the textfield before i confirm
print out the actual textfield then to see what it reads
so
System.out.println(jtextfield);
right?
and when u get time learn on what debug does , that will save u alot of time and adding prints
jtextfield.getText();
is there like a yt vid or anything you can recommend?
else your just printing the object
the manual :p
blank
simply disappears
and
doesnt print anything
which mean its probably not reading the number
is the code u pasted earlier your entire code ?
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import java.util.Scanner;
import java.util.Set;
import java.util.concurrent.ThreadLocalRandom;
public class Main {
I think this is unnecessary, but I changed a lot and also had made this game out of another ones code i did a day earlier, at the end i was unsure which ones i can keep and which ones are useless so i simply kept them all
i think Scanner is useless by now
most of the code u have is kinda useless, 😉
yeaa i know lmao
3 days into it you know
your still new and your repeating yourselft multuple unneeded times. That will change with exp
its not the java code, u said u coded earlier so that's why its wierd. Unless it was just small things u made
also if your using a good Ide, it will tell u what's unneeded as imports etc
nah i deleted everything but the import stuff at the end, i just didnt know which imports were from this game and which were from the earlier one. i just used the old one as help
to like
remember how i did certain stuff
yk
IntelliJ
i mean it does but
use that, dont manual delete. Save time and effort 🙂
there's an icon that shows on the unused ones, use that menu to clear them all
but i do actually need it
it was just bugging around i guess, got colors right after i clicked on the line
okk, removed them
one thing tho
jtextfield.setText("") at the bottom
every time you guess wrong
why does this not affect the program?
there alot of things that are currently not working in your program, one by one 😉
like u never get a new number, the win condition does not show etc
im from belgium, the german is no problem
so here its 85 ( alter the text2 )
enter 85, no thing happens
how did you get to 100.85 lol i never got that
just added the variable to print
1st rule of working with GUI, be able to overwrite it so work on its flow
wdym
same way with any user input. Make it work once, block it after
your the programmer, make the program jump to the part where its going wrong, without actaully having to interact with it
tell me how lol im new
my friend and this video
https://youtu.be/HuYs2Vp3GtM
are my whole experience right now lol
Kostenloses Beratungsgespräch: https://weiterbildung.developerakademie.com/y
In diesem Anfänger-Tutorial lernst du die Programmiersprache Java. Java ist eine sehr verbreitete Programmiersprache mit einer großen Community.
In diesem Kurs lernst du alles, was du als kompletter Anfänger wissen musst um deine eigenen Programme mit Java zu schreiben...