1

lOkay so I have gotten stumped on this one...

What i'm trying to achieve is to generate a series of numbers between 0-9. After The generation of the numbers, i want them to be typed into a text field on the screen. I have gotten all the essentials working except the changing a random number into a key code. Currently when i go to convert it to a key code, it turns into 0.

Here is my code:

public void random() {
    int rand = new Random().nextInt(10);
            System.out.println(""+rand);
    convert();
}

public void convert() {
    switch (rand) {
    case 0:
        rand = KeyEvent.VK_0;
                    System.out.println(""+0);
        break;
    case 1:
        rand = KeyEvent.VK_1;
                    System.out.println(""+1);
        break;
    case 2:
        rand = KeyEvent.VK_2;
                    System.out.println(""+2);
        break;
    case 3:
        rand = KeyEvent.VK_3;
                    System.out.println(""+3);
        break;
    case 4:
        rand = KeyEvent.VK_4;
                    System.out.println(""+4);
        break;
    case 5:
        rand = KeyEvent.VK_5;
                    System.out.println(""+5);
        break;
    case 6:
        rand = KeyEvent.VK_6;
                    System.out.println(""+6);
        break;
    case 7:
        rand = KeyEvent.VK_7;
                    System.out.println(""+7);
        break;
    case 8:
        rand = KeyEvent.VK_8;
                    System.out.println(""+8);
        break;
    case 9:
        rand = KeyEvent.VK_9;
                    System.out.println(""+9);
        break;
    }
}

The out put is this:

5
0

What it should be:

5
53

Any suggestions/solutions?

Thanks!!

  • 1
    `System.out.printLn`? I though it was `System.out.println`, are you sure this code compiles and run? – Luiggi Mendoza Oct 23 '12 at 06:07
  • And why are you prepending "" before each value? Just `System.out.println(5)` will work fine... It's not clear how you expect two digits to be printed out for the second line at all, btw... – Jon Skeet Oct 23 '12 at 06:08
  • 1
    The rand variable is not defined in the convert method. If it is a class member then the random method does not assign the value to the right variable. – mkhelif Oct 23 '12 at 06:09
  • Also, I wonder how your `rand` variable goes from the `random` method to the `convert` method w/o being passed as parameter and not being an attribute of your class. – Luiggi Mendoza Oct 23 '12 at 06:10
  • Where does `convert` get `rand` from? – Eric Oct 23 '12 at 06:10
  • Also, you declared your `rand` variable in your random method which is not passed to convert method. I think that should not even compile, unless you have declared an instance variable. – Rohit Jain Oct 23 '12 at 06:10
  • Okay, for all the people asking, i have the rand declared before the random() method as: int rand; – user1767369 Oct 23 '12 at 06:14
  • Your last comment shows that you don't even know how your code behaves when you run it. I recommend you to use a debugger and see what your actual code does. When you have a real question, come back and we gladly help you. – Luiggi Mendoza Oct 23 '12 at 06:15
  • I know what my code is SUPPOSED to do, but the reason i am asking for help is because it is not doing that the way i have set it up. All I'm asking for is some guidance in the right direction to know why my switch statement is changing all the numbers into 0 instead of the respected key code. If you want me to post all of my code i will gladly do that for you, that is if it will better aid you in understanding what i need help with. – user1767369 Oct 23 '12 at 06:19
  • Are you sure you're not [shadowing](http://stackoverflow.com/q/1092099/1065197) a `rand` attribute in your class with the `int rand` local variable inside the `random` method (because that's how it looks and that's what other people is saying to you in comments/answers). – Luiggi Mendoza Oct 23 '12 at 06:23

4 Answers4

2

I think you have a global rand variable intialized to 0.

Do following changes and it should work fine:

  1. Change signature of convert to following:

    public void convert(int rand) 
    
  2. While calling convert from random,call as

    convert(rand);
    

EDIT:

Based on your comment, that you've int rand declared above random() method. You need not declare rand again.

Change following line

int rand = new Random().nextInt(10); // a new rand variable is created local to method

to

rand = new Random().nextInt(10); // already declared rand variable is used.
Azodious
  • 13,752
  • 1
  • 36
  • 71
  • Convert method doesn't call random. where are you seeing recursion? – Azodious Oct 23 '12 at 06:30
  • Oh .. ya .. corrected. thanks. but it would be a compiler error if it was a method name. – Azodious Oct 23 '12 at 06:31
  • Okay i have changed to your edits and it converts them to the right codes. Now my new problem is the error: Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: Invalid key code at sun.awt.windows.WRobotPeer.keyPress(Native Method) at java.awt.Robot.keyPress(Unknown Source) and 55 (the one that was supposed to be the input) is a correct code... – user1767369 Oct 23 '12 at 06:35
1

What you do seems really strange. Is the text field in your own app? Anyway, the way you get 0 is because you must use an instance variable (rand) in the convert method and you assign the random number to a local variable (also called rand but still a different variable). Pass the random variable as a parameter to the convert method like this:

public void random() {
    int rand = new Random().nextInt(10);
    System.out.println(rand);
    convert(rand);
}

public void convert(int rand) {
    switch (rand) {
    ...

Then change the prints to System.out.println(rand);

Tobias Ritzau
  • 3,327
  • 2
  • 18
  • 29
0

Your rand is declared local to your random method

int rand = new Random().nextInt(10);

But you are referencing it from within your convert method

switch (rand) {

This suggests that rand has already being declared as an instance variable (otherwise it won't compile) and you are ignoring it...

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
0

Luiggi is right. That code does not compile.

Anyway, if I where to guess, this code might work for you:

public void random() {
    int rand = new java.util.Random().nextInt(10);
    convert(rand);
}

public void convert(int rand) {
    int vk = 0;
    switch (rand) {
    case 0:
        vk = java.awt.event.KeyEvent.VK_0;
        break;
    case 1:
        vk = java.awt.event.KeyEvent.VK_1;
        break;
    case 2:
        vk = java.awt.event.KeyEvent.VK_2;
        break;
    case 3:
        vk = java.awt.event.KeyEvent.VK_3;
        break;
    case 4:
        vk = java.awt.event.KeyEvent.VK_4;
        break;
    case 5:
        vk = java.awt.event.KeyEvent.VK_5;
        break;
    case 6:
        vk = java.awt.event.KeyEvent.VK_6;
        break;
    case 7:
        vk = java.awt.event.KeyEvent.VK_7;
        break;
    case 8:
        vk = java.awt.event.KeyEvent.VK_8;
        break;
    case 9:
        vk = java.awt.event.KeyEvent.VK_9;
        break;
    }
    System.out.println(rand+"->"+vk);
}

Note that since VK_0 through VK_9 has values 48 through 57 so you might as well do this:

public void randomAndConvert() {
    int rand = new java.util.Random().nextInt(10);
    int vk = rand+48;
    System.out.println(rand+"->"+vk);
}

Finally, a couple of things about the original code:

  • printLn should be println
  • rand is never declared in convert
  • rand is never passed to convert throught parameter
  • rand is never printed out in convert

Hope this will lead you down the right path :)

  • Thanks for this! I don't know why you would get compile errors (I haven't) but will change to this just in-case... The printLn(); was just a typo, was copying off my laptop to post on my desktop (sorrys!)... – user1767369 Oct 23 '12 at 06:41
  • ok. I guess you have `rand` declared somewhere as mentioned by others. Then the code should "work". I'm not a big fan of using class members for method communication, but ymmv :) – Thobias Bergqvist Oct 23 '12 at 07:06