Numbers not working outside Random number method

  • (2 Pages)
  • +
  • 1
  • 2

20 Replies - 569 Views - Last Post: 26 March 2012 - 10:29 PM Rate Topic: -----

#1 robgw3  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 138
  • Joined: 23-February 12

Numbers not working outside Random number method

Posted 26 March 2012 - 07:40 PM

I have tried to make a method that will produce 3 random numbers which i can then use to compare 3 numbers entered by the user against. However for some reason i cant seem to be able to use the numbers outside of the method, they work fine and generate properly, but i cant access the variables and trying to use them just returns null.

I know i probably wrote it wrong, but i cant see what i did wrong so could someone please take a look at this and explain where i made the mistake?

public void randomNum() {
        randOne = Integer.toString(random.nextInt(9));
        randTwo = Integer.toString(random.nextInt(9));
        randThree = Integer.toString(random.nextInt(9));
        System.out.println("One: " + randOne + " Two: " + randTwo + " Three: " + randThree);
    }


I call it when i click a button by using classname.randomNum();

Is This A Good Question/Topic? 0
  • +

Replies To: Numbers not working outside Random number method

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8316
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Numbers not working outside Random number method

Posted 26 March 2012 - 07:45 PM

As this statement does not generate an error

randOne = Integer.toString(random.nextInt(9));

it means that the String randOne, randTwo and randThree are declared outside the method
so they should be visible to other methods

Post your code and your errors
Was This Post Helpful? 0
  • +
  • -

#3 robgw3  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 138
  • Joined: 23-February 12

Re: Numbers not working outside Random number method

Posted 26 March 2012 - 07:49 PM

Well the thing is that it doesnt give me any errors, well not any major ones anyway. All it does is give me null when i try and use them. I guess they would give me errors if i tried to put them back into code, but when they werent working all i did was put them into a System.out.println to show what was happening with them and thats what shows them as null.

As for my code, this is the code from the class that the random method is in:

import java.util.*;

/**
 *
 * @author Robert Wright
 */
public class Fermi {
    
    private String guessOne;
    private String guessTwo;
    private String guessThree;
    public String randOne;
    public String randTwo;
    public String randThree;
    private ArrayList guesses = new ArrayList();
    private Random random;
    public String num1;
    public String num2;
    public String num3;
    
    public Fermi() {
        guesses.clear();
        random = new Random(); 
        num1 = Integer.toString(2);
        num2 = Integer.toString(2);
        num3 = Integer.toString(6);
    }
    
    
    public void randomNum() {
        randOne = Integer.toString(random.nextInt(9));
        randTwo = Integer.toString(random.nextInt(9));
        randThree = Integer.toString(random.nextInt(9));
        System.out.println("One: " + randOne + " Two: " + randTwo + " Three: " + randThree);
    }
    
    
    public void setGuess1 (String guess1) {
        guessOne = guess1;
    }
    
    public String getGuess1 () {
        return guessOne;
    }
    
    public void setGuess2 (String guess2) {
        guessTwo = guess2;
    }
    
    public String getGuess2 () {
        return guessTwo;
    }
    
    public void setGuess3 (String guess3) {
        guessThree = guess3;
    }
    
    public String getGuess3 () {
        return guessThree;
    }
    
    public void checkResults() {
        
        if (guessOne.equals(num1)) {
            guesses.add("Fermi");
        } else 
        if ((guessOne.equals(num2) && !guessTwo.equals(num2)) || (guessOne.equals(num3) && !guessThree.equals(num3))){ 

                
                // 
            guesses.add("Pico");
        } else {
            guesses.add("Nano");
        }
        
        if (guessTwo.equals(num2)) {
            guesses.add("Fermi");
        } else 
        if ((guessTwo.equals(num1) && !guessOne.equals(num1)) || (guessTwo.equals(num3)&& !guessThree.equals(num3))){
            guesses.add("Pico");
        } else {
            guesses.add("Nano");
        }
        
        if (guessThree.equals(num3)) {
            guesses.add("Fermi");
        } else 
        if ((guessThree.equals(num2) && !guessOne.equals(num1)) || (guessThree.equals(num3) && !guessTwo.equals(num2))){
            guesses.add("Pico");
        } else {
            guesses.add("Nano");
        }
        
        System.out.println("Guess1: " + guessOne + " Guess2: " + guessTwo + " Guess3: " + guessThree);
        System.out.println("ROne: " + num1 + " RTwo: " + num2 + " RThree: " + num3);
        //System.out.println("rOne: " + randOne + " rTwo: " + randTwo + " rThree: " + randThree);
        System.out.println(guesses.get(0) + " " + guesses.get(1) + " " + guesses.get(2));
        //System.out.println(guesses.get(1) + " " + guesses.get(2) + " " + guesses.get(3));
    }
    
        
        
}


Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8316
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Numbers not working outside Random number method

Posted 26 March 2012 - 08:03 PM

and where is the main() that drives all that ?
Was This Post Helpful? 0
  • +
  • -

#5 robgw3  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 138
  • Joined: 23-February 12

Re: Numbers not working outside Random number method

Posted 26 March 2012 - 08:07 PM

    public MainForm() {
        initComponents();
        
        Fermi fermi;
        fermi = new Fermi();
        
        fermi.randomNum();
    }


Thats the only part of the main form that actually deals with the random number, i can post the entire code for it if needed though.
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8316
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Numbers not working outside Random number method

Posted 26 March 2012 - 08:13 PM

that only calls randomNum() that prints the 3 random numbers as String
Was This Post Helpful? 0
  • +
  • -

#7 robgw3  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 138
  • Joined: 23-February 12

Re: Numbers not working outside Random number method

Posted 26 March 2012 - 08:30 PM

How would i go about fixing that?
Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8316
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Numbers not working outside Random number method

Posted 26 March 2012 - 08:46 PM

Depends what you want to do :)
There is nothing in your code but:
- who is supposed to call checkResults() and under which condition(s) ?
- same thing for all your getter() and setter() who is supposed to call them ? If they are not used, just remove them, it will confuse the next guy, like me, who try to understand your code
Was This Post Helpful? 0
  • +
  • -

#9 robgw3  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 138
  • Joined: 23-February 12

Re: Numbers not working outside Random number method

Posted 26 March 2012 - 08:53 PM

They are used, i just didnt show them because i didnt see how they affected the random numbers. I have a key released event triggered to the text fields so when someone types into the text fields it checks to see if it's a number between 0-9 and if not it tells them to enter a number. It's also supposed to check if the other two boxes are filled and if they are then it's supposed to enable a button but that doesnt work so i removed that for now. As for the check results, thats done through the button i just mentioned. When the button is clicked it gathers the values from the text fields and uses the setter to send the players guesses. The gets are used in the check results, they work at least. So far the only problems i have left are the random numbers not working outside that and not being able to get the text field checker to work right.

Here is the code for the buttons and text fields as well:

private void btnInstructionsMouseReleased(java.awt.event.MouseEvent evt) {                                              
       Instructions instructions;
       instructions = new Instructions();
       instructions.setVisible(true);
        
    }                                             

    private void btnResetMouseReleased(java.awt.event.MouseEvent evt) {                                       
        Fermi fermi;
        fermi = new Fermi();
        
        txtGuess1.setText("");
        txtGuess1.setEnabled(true);
        txtGuess2.setText("");
        txtGuess2.setEnabled(true);
        txtGuess3.setText("");
        txtGuess3.setEnabled(true);
        txtResults.setText("");
        fermi.randomNum();
    }                                      

    // Checks to see what the user entered in textbox 1
    private void txtGuess1KeyReleased(java.awt.event.KeyEvent evt) {                                      
        String guess1 = txtGuess1.getText();
        String guess2 = txtGuess2.getText();
        String guess3 = txtGuess3.getText();
        TextFieldValidator textfield;
        textfield = new TextFieldValidator();
        //textfield.Validate(guess1);
        if (textfield.Validate(guess1)) {
            JOptionPane.showMessageDialog(null, "You must enter a number between 0-9");
            txtGuess1.setText("");
            txtGuess1.requestFocusInWindow();
        } 
    }                                     

    // Checks to see what the user entered in textbox 2
    private void txtGuess2KeyReleased(java.awt.event.KeyEvent evt) {                                      
        String guess1 = txtGuess1.getText();
        String guess2 = txtGuess2.getText();
        String guess3 = txtGuess3.getText();
        TextFieldValidator textfield;
        textfield = new TextFieldValidator();
        //textfield.Validate(guess1);
        if (textfield.Validate(guess2)) {
            JOptionPane.showMessageDialog(null, "You must enter a number between 0-9");
            txtGuess2.setText("");
            txtGuess2.requestFocusInWindow();
        } 
    }                                     

    
    // Checks to see what the user entered in textbox 3
    private void txtGuess3KeyReleased(java.awt.event.KeyEvent evt) {                                      
        String guess1 = txtGuess1.getText();
        String guess2 = txtGuess2.getText();
        String guess3 = txtGuess3.getText();
        TextFieldValidator textfield;
        textfield = new TextFieldValidator();
        //textfield.Validate(guess1);
        if (textfield.Validate(guess3)) {
            JOptionPane.showMessageDialog(null, "You must enter a number between 0-9");
            txtGuess3.setText("");
            txtGuess3.requestFocusInWindow();
        } 
    }                                     

    private void btnCheckGuessMouseClicked(java.awt.event.MouseEvent evt) {                                           
        if (btnCheckGuess.isEnabled()) {
        Fermi fermi;
        fermi = new Fermi();
        
        fermi.setGuess1(txtGuess1.getText());
        fermi.setGuess2(txtGuess2.getText());
        fermi.setGuess3(txtGuess3.getText());
        fermi.checkResults();
        }
    }    

Was This Post Helpful? 0
  • +
  • -

#10 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8316
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Numbers not working outside Random number method

Posted 26 March 2012 - 08:57 PM

Are you telling me you are creating a new Fermi object everytime the mouse is clicked ?
What for ? Your random numbers will always be different
Was This Post Helpful? 0
  • +
  • -

#11 robgw3  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 138
  • Joined: 23-February 12

Re: Numbers not working outside Random number method

Posted 26 March 2012 - 09:00 PM

View Postpbl, on 26 March 2012 - 08:57 PM, said:

Are you telling me you are creating a new Fermi object everytime the mouse is clicked ?
What for ? Your random numbers will always be different


Cause it was the only way i could get the data from the text field into the Fermi class. I've only been learning java since Jan.
Was This Post Helpful? 0
  • +
  • -

#12 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8316
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Numbers not working outside Random number method

Posted 26 March 2012 - 09:04 PM

The constructor of Fermi does not receive the JTextField data as parameter so how can building a new Fermi object would fix your problem ?
How do you transfer your JTextField data into your Fermi object ?
Was This Post Helpful? 0
  • +
  • -

#13 robgw3  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 138
  • Joined: 23-February 12

Re: Numbers not working outside Random number method

Posted 26 March 2012 - 09:10 PM

No but i used the setter to send the JTextField data to the Fermi class, if i didnt have that in there then they wont work either.

Fermi fermi;
        fermi = new Fermi();
        
        fermi.setGuess1(txtGuess1.getText());
        fermi.setGuess2(txtGuess2.getText());
        fermi.setGuess3(txtGuess3.getText());
        fermi.checkResults();


Without the fermi = new Fermi(); in there the fermi. in the following 4 lines says "cannot find symbol" and if i remove the fermi from them then setGuess1 says "cannot find symbol".
Was This Post Helpful? 0
  • +
  • -

#14 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8316
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Numbers not working outside Random number method

Posted 26 March 2012 - 09:14 PM

You should be able to create a Fermi object in your class that extends JFrame and use that object later on
Creating it in your actionEvent() method will make it vanish as soon as the method exits

class Foo extend JFrame {
   Fermi fermi;

   // constructor
   Foo() {
      fermi = new Fermi();


Was This Post Helpful? 0
  • +
  • -

#15 robgw3  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 138
  • Joined: 23-February 12

Re: Numbers not working outside Random number method

Posted 26 March 2012 - 09:23 PM

Sorry i dont really understand what you mean there. I get what your saying about it vanishing, but not the rest of it.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2