#My game doesnt work and i dont know why

1 messages · Page 1 of 1 (latest)

feral shoreBOT
#

<@&987246399047479336> please have a look, thanks.

feral shoreBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

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.

dense summit
#
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?

fierce rivet
#

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

dense summit
fierce rivet
#

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

dense summit
#

what is the better choice?

#

or what would be easier right now? 😅

fierce rivet
#

print out the error

dense summit
#

how do i do that?

fierce rivet
#

will tell u what is wrong, where and the type

#

syso the 'error' variable should usually do it

dense summit
#

syso?

fierce rivet
#

system out

dense summit
#

alr one second

#

here it is

fierce rivet
#

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 "" ?

dense summit
#

yeaa it was from another thing i had in the game

fierce rivet
#

that's the reason

dense summit
#

it used to keep the number you had typed in to win

#

one sec

fierce rivet
#

i know the code and the trick. Alter your error handling or your flow to avoid that

dense summit
#

it works, removed the

jtextfield.setText("");
#

but is there a way to start with the textfield being empty?

fierce rivet
#

humanly, that's the way

#

u just need to do a little trick for your backend to also know

fierce rivet
#

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 ""

dense summit
#

ok, let me try

fierce rivet
#

also change your Boolean to boolean if u dont need the third condition from Boolean ( boolean=true/false Boolean=true/false/null )

dense summit
#

just did

#

yeah

#

nah

#

no clue

fierce rivet
#

either u handle the exeption and ask again, of u just add some code to validate the field ( which already should of been there )

dense summit
#

why does

String TextFromTextfield = jtextfield.getText();
                        Integer Anumber = Integer.parseInt(TextFromTextfield);

ignore ""?

fierce rivet
#

it doesnt

#

the first line reads that , the second tries to convert it to an Integer, and as its no number, it fails

dense summit
#

so it gets taken out because the input is ""50 or any number, right?

fierce rivet
#

"" 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 ?

dense summit
#

i type smth into the textfield before i confirm

fierce rivet
#

print out the actual textfield then to see what it reads

dense summit
#

so

System.out.println(jtextfield);

right?

fierce rivet
#

and when u get time learn on what debug does , that will save u alot of time and adding prints

#

jtextfield.getText();

dense summit
fierce rivet
#

else your just printing the object

fierce rivet
dense summit
#

got it

#

one sec

dense summit
#

simply disappears

#

and

#

doesnt print anything

fierce rivet
#

which mean its probably not reading the number

#

is the code u pasted earlier your entire code ?

dense summit
#
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

fierce rivet
#

most of the code u have is kinda useless, 😉

dense summit
#

3 days into it you know

fierce rivet
#

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

dense summit
#

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

dense summit
#

i mean it does but

fierce rivet
#

use that, dont manual delete. Save time and effort 🙂

dense summit
#

look at this tho

#

it says its unneeded

fierce rivet
#

there's an icon that shows on the unused ones, use that menu to clear them all

dense summit
#

but i do actually need it

dense summit
fierce rivet
#

its unused

#

tried erasing the importline ?

dense summit
#

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?

fierce rivet
#

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

dense summit
#

the win condition?

#

it does

#

just that its german

fierce rivet
#

im from belgium, the german is no problem

#

so here its 85 ( alter the text2 )

#

enter 85, no thing happens

dense summit
#

how did you get to 100.85 lol i never got that

fierce rivet
#

just added the variable to print

dense summit
#

after typing 85

fierce rivet
#

1st rule of working with GUI, be able to overwrite it so work on its flow

fierce rivet
#

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

dense summit
#

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...

▶ Play video
fierce rivet
#

nope, not going to. Learn the basic syntax and refactor your code first. Also correct name formatting . That's waay more important

#

those tricks u will realize on yourself when your enting a value 20-30 times over and over to get somewhere