Results 1 to 3 of 3
  1. #1
    computerboyo is offline Member
    Join Date
    Feb 2009
    Posts
    1
    Rep Power
    0

    Unhappy Java, output string, getting correct output? HELP!

    Hi, I have this piece of code;

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

  2. #2
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    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:
    Java Code:
    myClassA mca = new myClassA();
    then use use the the following:
    Java 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.
    Last edited by CJSLMAN; 02-26-2009 at 12:45 AM. Reason: corrected the termonology
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  3. #3
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

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

Similar Threads

  1. Replies: 9
    Last Post: 01-23-2014, 08:17 PM
  2. the explanation of output of simple java program
    By amol84 in forum New To Java
    Replies: 1
    Last Post: 11-06-2008, 06:06 PM
  3. What will be output and why
    By huma in forum Threads and Synchronization
    Replies: 4
    Last Post: 06-26-2008, 11:14 PM
  4. JSP to output Java String Array
    By irenavassilia in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 03-31-2008, 05:11 PM
  5. Java Swing class capturing output to the console
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-12-2008, 12:24 PM

Posting Permissions

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