Results 1 to 3 of 3
- 02-25-2009, 08:57 PM #1
Member
- Join Date
- Feb 2009
- Posts
- 1
- Rep Power
- 0
Java, output string, getting correct output? HELP!
Hi, I have this piece of code;
And I want to display the output entered from this class;Java Code:resultText.setText("Sorry the number is " + WHAT GOES HERE HELP!); finishGame(); break; case 1: //won resultText.setText("Well done it is " + playerNumber); finishGame(); break; case 2: resultText.setText("higher"); break; case 3: resultText.setText("lower"); break;
I know the user enters the number into "guessNumber" but please can someone tell me how I can display that number in another class, the one shown above? The bit that says "WHAT GOES HERE HELP!".Java Code:public TestGame(int maxValue, int maxNumberOfGuesses) { super(maxValue, maxNumberOfGuesses); //ask user for guess number String numberString = JOptionPane.showInputDialog(null, "Please input guess number:"); //save it guessNumber = Integer.parseInt(numberString);
Any help appreciated. Thanks.
- 02-25-2009, 09:35 PM #2
Since I don't see all of your code, not sure if this will work:
instantiate the class at the beginning at classB ... example:
then use use the the following:Java Code:myClassA mca = new myClassA();
Not sure if your variable has to be declared global or not.Java Code:mca.guessNumber
CJSL
EDIT: this code is not a very good example.... the one in the my below post is a little better.Last edited by CJSLMAN; 02-25-2009 at 11:45 PM. Reason: corrected the termonology
Chris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 02-25-2009, 11:44 PM #3
A couple comments...
- You didn't fare very well over at the Sun forums (lesson learned: don't cross post)
- what you want to do is not as easy as it seems...
I'll try to explain....
you have two classes: Class A and Class B.
I think what you want to do is pass a variable value from class A so it can be used in class B... correct? Please make sure that this is what you want to do: can this be done in Class A with a mehtod?
You can't first run classA and then run class B for this to work. What has to happen is that you have to call the method that's in class B (from Class A) that needs that variable from Class A (sounds like "Who's on first?").
in Class A:
in Class B:Java Code:String varToSend ="Hello !!!"; ClassB cb = new ClassB(); //instantiate class B in class A cb.heresTheVariable(varToSend);//call the class B method
Now in this example, I'm not returning anything from the class B method, so the example is not useful... it just shows how things flow (if i didn't goof anything up).Java Code:public void heresTheVariable(String varFromClassA) { System.out.println("The variable form class A: " + varFromClassA); }
Luck,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
Similar Threads
-
the explanation of output of simple java program
By amol84 in forum New To JavaReplies: 1Last Post: 11-06-2008, 05:06 PM -
What will be output and why
By huma in forum Threads and SynchronizationReplies: 4Last Post: 06-26-2008, 10:14 PM -
JSP to output Java String Array
By irenavassilia in forum JavaServer Pages (JSP) and JSTLReplies: 0Last Post: 03-31-2008, 04:11 PM -
Java Swing class capturing output to the console
By Java Tip in forum Java TipReplies: 0Last Post: 03-12-2008, 11:24 AM -
Calling a Jython script from a Java Servlet returns incomplete output
By bthakur in forum Java ServletReplies: 0Last Post: 11-13-2007, 12:41 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks