Advanced Search

Results 1 to 5 of 5

Thread: Need to access an int from another class

  1. #1
    Join Date
    Apr 2007
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Need to access an int from another class

    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

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,878
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    There's no reason that method won't work, but you need to access it as myGUI.findNumChars() (where myGUI is a GUI instance) rather than just findNumChars().

    Java convention says you should call the method getNumChars() (getter methods should be formed of "get" and then the property name with the first letter uppercased), and call the "learner" class "Learner" (class names should start with a capital letter). Stylistic points like this are very important in Java; in fact, NetBeans relies on the former convention to work.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  3. #3
    Join Date
    Apr 2007
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thanks, I got it all fixed now

    So now I have another question....The application now stores the number of occurences of different character combinations in a text string in a hashMap.

    So if a string contains the text: "Hello"; He, el, ll and lo are stored as keys in the hashMap; all with the value 1; since they only occur once each. I want to use those statistics to generate a new text. So if the language in the text(s) it analyzed was german/swedish or whatever; the application should be able to generate a text that resembles that language. Obviously I need to analyze longer texts; but you get the point....

    Any thoughts as to how I could accomplish this?

    Thanks...

  4. #4
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,878
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    With great difficulty.

    I don't think you'll have much luck doing it that way. You really need to be able to distinguish between syllables and mix and match those, rather than combinations of two characters. Unfortunately, you'd need to know which language it is to determine syllables.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  5. #5
    Join Date
    Apr 2007
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    It's really not that important that the generated text looks a LOT like the analyzed language, I'm just making this for myself; school related. So I just want to be able to generate a new text, based on the likelihood that a specific combination of characters would appear. Also the user is now able to choose the number of characters that are stored in the combo; between 2 and 10. If we forget about using syllables and such; and just base the text-generation on the hashMap I have; do you have any idea how I could do something like this?
    Last edited by BKevinT; 04-22-2007 at 02:43 PM.

Bookmarks

Posting Permissions

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