0
package javaapplication1;

import javax.swing.JOptionPane;

public class Room{

    public static void main(String[] args) {
        dialog();
        System.out.println(sample);
    }

    public static String dialog() {
        String sample = JOptionPane.showInputDialog(null, "Insert Value", "Enter amount ", JOptionPane.QUESTION_MESSAGE); 
        if (sample.isEmpty()) {
            JOptionPane.showMessageDialog(null, "Error!", "No Value Detected", JOptionPane.ERROR_MESSAGE);
            dialog();
        }
        System.out.println(sample+" from the bottom line.");
        return sample;
    }

Hey guys,

it seems that I'm currently facing an issue with java at the moment in terms of calling one variable from another object. Following the code above, it's a sample code that provides the user input using java swing. I have 2 objects one being the main and one more being dialog(), dialog has declared a variable called sample that I want to bring it over to main, however I can't seem to use the variable as it always comes out an error.

Would appreciate some advice thanks!

2
  • Dialog is not an object. it is a function. Commented Feb 15, 2015 at 8:36
  • Thanks for pointing that out! I'm still new in Java and this is kind of my first programming language so I might be a little confused in certain areas. Commented Feb 18, 2015 at 8:40

4 Answers 4

1

Use inside main method as String sample = dialog(); or whole class

package javaapplication1;

import javax.swing.JOptionPane;

public class Room{

    public static void main(String[] args) {
        String sample = dialog();
        System.out.println(sample);
    }

    public static String dialog() {
        String sample = JOptionPane.showInputDialog(null, "Insert Value", "Enter amount ", JOptionPane.QUESTION_MESSAGE); 
        if (sample.isEmpty()) {
            JOptionPane.showMessageDialog(null, "Error!", "No Value Detected", JOptionPane.ERROR_MESSAGE);
            dialog();
        }
        System.out.println(sample+" from the bottom line.");
        return sample;
    }
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the help! This works perfectly, sorry for the late reply got busy with celebrations here. Cheers! This works amazingly!
0

If a method returns a value, you can capture it when you call the method like this:

public static void main(String[] args) {
    // Assign the return value of dialog() to a variable
    String sample = dialog();
    System.out.println(sample);
}

Comments

0

Just:

String sample = dialog();
System.out.println(sample);

You have to assign the return value to a variable.

1 Comment

Ah, i did not know how it would be actually, I've tried using return sample; but did not place the String sample = dialog(); this makes sense now. Thank you very much!
0

Use,

System.out.println(dialog());

You are returning a string from your method.So you can directly print the value using System.out.println

If you want to use the value later in your program, you can store it in a variable.

One thing to note in your approach is that you cannot access sample variable of dialog method from main method.

Another logical mistake you are doing,

if (sample.isEmpty()) {
        JOptionPane.showMessageDialog(null, "Error!", "No Value Detected", JOptionPane.ERROR_MESSAGE);
        dialog();
}

Here you are trying to invoke the dialog method again if the value is empty for the sample variable. But you haven't assigned the returning value to the sample variable. There for event tho you invoke the dialog method your sample value will still be empty. You can correct it by,

if (sample.isEmpty()) {
        JOptionPane.showMessageDialog(null, "Error!", "No Value Detected", JOptionPane.ERROR_MESSAGE);
        sample = dialog();
}

2 Comments

I see, thanks for the clarification! this is a great detailed answer on my misunderstanding. However, in this case, I've tried that if the answer is empty, and emptied for a few times, then followed by placing a string, it comes out in 4 lines then (depending on how many times i give it an empty value). How would I be able to fix that? (Sorry for moving towards the original question)
I'm sorry I didn't get what you said. Can you provide a code snippet?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.