Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 02-25-2009, 09:57 PM
Member
 
Join Date: Feb 2009
Posts: 1
Rep Power: 0
computerboyo is on a distinguished road
Unhappy Java, output string, getting correct output? HELP!
Hi, I have this piece of code;

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;
And I want to display the output entered from this class;
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);
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!".

Any help appreciated. Thanks.
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 02-25-2009, 10:35 PM
CJSLMAN's Avatar
Moderator
 
Join Date: Oct 2008
Location: Mexico
Posts: 1,159
Rep Power: 3
CJSLMAN is on a distinguished road
Default
Since I don't see all of your code, not sure if this will work:
instantiate the class at the beginning at classB ... example:
Code:
myClassA mca = new myClassA();
then use use the the following:
Code:
mca.guessNumber
Not sure if your variable has to be declared global or not.

CJSL

EDIT: this code is not a very good example.... the one in the my below post is a little better.
__________________
Chris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

Last edited by CJSLMAN; 02-26-2009 at 12:45 AM. Reason: corrected the termonology
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 02-26-2009, 12:44 AM
CJSLMAN's Avatar
Moderator
 
Join Date: Oct 2008
Location: Mexico
Posts: 1,159
Rep Power: 3
CJSLMAN is on a distinguished road
Default
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:
Code:
String varToSend ="Hello !!!";
ClassB cb = new ClassB(); //instantiate class B in class A
cb.heresTheVariable(varToSend);//call the class B method
in Class B:
Code:
public void heresTheVariable(String varFromClassA)
{
  System.out.println("The variable form class A: " + varFromClassA);
}
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).

Luck,
CJSL
__________________
Chris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
the explanation of output of simple java program amol84 New To Java 1 11-06-2008 06:06 PM
What will be output and why huma Threads and Synchronization 4 06-26-2008 11:14 PM
JSP to output Java String Array irenavassilia JavaServer Pages (JSP) and JSTL 0 03-31-2008 05:11 PM
Java Swing class capturing output to the console Java Tip Java Tips 0 03-12-2008 12:24 PM
Calling a Jython script from a Java Servlet returns incomplete output bthakur Java Servlet 0 11-13-2007 01:41 PM


All times are GMT +2. The time now is 04:40 PM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org