Jump to content

JavaFx random number to word problems

Go to solution Solved by bomberblyat,
17 minutes ago, DtrollMC said:

Bro IDK how hard it can be to setText of a Label. I don't think any of us can really help you.

it's not hard it just doesnt work coz my code doesnt kinda make anysense, ill solve how to do it other way,

myMessage.setText(Integer.toString(whateverishere));

i know that  take's that shit from the code it just doesn't work, since my switch doesn't have id or someshit ive done now some string stuff, but thanks for trying to help i guess.

Hey! Im stuck on trying to get my random number to display text on label, but it display that random number it generates, ive tried many times to solve it out, done some research haven't found answer to this yet, so maybe you guys know what im doing wrong on this. Code below

  public void generateRandom(ActionEvent event) {
        Random rand = new Random();
        int myrand = rand.nextInt(50) + 1;
        switch (myrand) {
            case 1:
                System.out.println("test1");
                break;
            case 2:
                System.out.println("test2");
                break;
            case 3:
                System.out.println("test3");
                break;
            case 4:
                System.out.println("test4");
                break;
            case 5:
                System.out.println("test5");
                break;
            case 6:
                System.out.println("test6");
                break;
            default:
                System.out.println("test7");
                break;
        }
                myMessage.setText(Integer.toString(myrand));
                System.out.println(Integer.toString(myrand));

        }
    }

 

Link to comment
Share on other sites

Link to post
Share on other sites

From what I understand, you're generating a random number between 1-50 (or 0 and 50, not too sure..), and then running a switch statement from that random number. It looks to me like you want to write "test#" (where # is the generated number) on a label, but a switch statement is not the right thing to use here. You can simply do this:

System.out.println("test" + myRand);

Overall this code doesn't really make any sense. The ActionEvent parameter is never used in the function either, which leads me to believe you're going about handling listeners incorrectly.

Link to comment
Share on other sites

Link to post
Share on other sites

Well, if you parse the number 6 to string then the text will be “6” so it is doing the right thing. You want it to be “six” or something? 

 

 

Edited by wasab
Seems like you want to default everything to test 7

Sudo make me a sandwich 

Link to comment
Share on other sites

Link to post
Share on other sites

38 minutes ago, DtrollMC said:

From what I understand, you're generating a random number between 1-50 (or 0 and 50, not too sure..), and then running a switch statement from that random number. It looks to me like you want to write "test#" (where # is the generated number) on a label, but a switch statement is not the right thing to use here. You can simply do this:


System.out.println("test" + myRand);

Overall this code doesn't really make any sense. The ActionEvent parameter is never used in the function either, which leads me to believe you're going about handling listeners incorrectly.

What im trying to do is kinda like, you click button, it generates number and that number is like id for each test, so it picks that test, im sure there's much simpler ways to do this but im quite new on javafx, do you have any suggestions or any help to fix this, i tried yours it didnt work it always prints (test text and number on netbeans console) but it doesnt print it on label it only prints the random number generated to label not text, thanks.

Link to comment
Share on other sites

Link to post
Share on other sites

31 minutes ago, wasab said:

Well, if you parse the number 6 to string then the text will be “6” so it is doing the right thing. You want it to be “six” or something? 

 

 

what do you mean? im dont really understand what do you mean.

Link to comment
Share on other sites

Link to post
Share on other sites

I can't really understand what you're saying, sorry dude. To set the text of a label, call the function setText(String text) on your button object.

Link to comment
Share on other sites

Link to post
Share on other sites

The button only has the number on it, because thats what you told it to contain.

 

myMessage.setText(Integer.toString(myrand));

 

If myRand is say, 4, then the myMessage component's text will say "4"

Link to comment
Share on other sites

Link to post
Share on other sites

6 minutes ago, DtrollMC said:

The button only has the number on it, because thats what you told it to contain.

 


myMessage.setText(Integer.toString(myrand));

 

If myRand is say, 4, then the myMessage component's text will say "4"

i think im doing my stuff on way more complicated way, im sure there's easier way so ill explain you idea of this thing so, User opens the software and then he clicks button and it generates one of my souts randomly, i tought doing it with random number was the best way.

Link to comment
Share on other sites

Link to post
Share on other sites

17 minutes ago, bomberblyat said:

i think im doing my stuff on way more complicated way, im sure there's easier way so ill explain you idea of this thing so, User opens the software and then he clicks button and it generates one of my souts randomly, i tought doing it with random number was the best way.

Can’t understand what you want to do. What is souts? Writing coherent sentence is a vital forum posting skill. 

Sudo make me a sandwich 

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, wasab said:

Can’t understand what you want to do. What is souts? Writing coherent sentence is a vital forum posting skill. 

Sorry, sout = System.out.println

 

Link to comment
Share on other sites

Link to post
Share on other sites

2 hours ago, bomberblyat said:

What im trying to do is kinda like, you click button, it generates number and that number is like id for each test, so it picks that test, im sure there's much simpler ways to do this but im quite new on javafx, do you have any suggestions or any help to fix this, i tried yours it didnt work it always prints (test text and number on netbeans console) but it doesnt print it on label it only prints the random number generated to label not text, thanks.

Place label.setText inside the switch loop right under each cases. 

Sudo make me a sandwich 

Link to comment
Share on other sites

Link to post
Share on other sites

8 hours ago, wasab said:

Place label.setText inside the switch loop right under each cases. 

didnt work :/

Link to comment
Share on other sites

Link to post
Share on other sites

Bro IDK how hard it can be to setText of a Label. I don't think any of us can really help you.

Link to comment
Share on other sites

Link to post
Share on other sites

17 minutes ago, DtrollMC said:

Bro IDK how hard it can be to setText of a Label. I don't think any of us can really help you.

it's not hard it just doesnt work coz my code doesnt kinda make anysense, ill solve how to do it other way,

myMessage.setText(Integer.toString(whateverishere));

i know that  take's that shit from the code it just doesn't work, since my switch doesn't have id or someshit ive done now some string stuff, but thanks for trying to help i guess.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×