Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Apr 2007
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Need to use an int in a different class

    EDIT: Problem solved.....feel free to delete this thread

    I have a very basic question:

    I have 2 classes; GUI.java & learner.java. The GUI contains a method that is called when it runs that shows an inputDialog , and the user chooses a number between 2 & 10.

    Code:
    public void setNumChars()throws Exception{
       int tmp;
       do
           tmp = Integer.parseInt(JOptionPane.showInputDialog("Write 
           num of chars\nbetween 2 and 10:"));
            
       while(tmp>10 || tmp<2);
         
       numChars = tmp;
    }
    And I need the number that the user inputs to be available to me in the learner.java class. So I tried creating a public method that retrieves the number (it is also located in the GUI class):

    Code:
    public int findNumChars(){
       return numChars;
    }
    Then I tried using that method in the learner class.

    Code:
    public static void learn(String tekst, int orden)
            {
            ....................
            for(int i=0; i<=tekst.length()-(findNumChars() -1); i++)
                {
               ...........................
                if
                    ....................
                else
                    ..................
                
                }
                
            }
    Now, I know that this doesn't work. My question is; what changes do I have to make to be able to use the user-inputted int from the GUI, in the learner class?

    Really appreciate your help, thanks
    Last edited by BKevinT; 04-22-2007 at 01:23 AM.

  • #2
    Regular Coder Aradon's Avatar
    Join Date
    Jun 2005
    Location
    USA
    Posts
    734
    Thanks
    0
    Thanked 20 Times in 19 Posts
    Can you please post your solution for future reference?

    I know it either involves setting a global variable to using a set method in the learner class, but let us know which you used.
    "To iterate is human, to recurse divine." -L. Peter Deutsch


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •