Helpful Information
 
 
Category: Java
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.


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):


public int findNumChars(){
return numChars;
}

Then I tried using that method in the learner class.


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

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.

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...

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.

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?










privacy (GDPR)