Compile and test it and see what happens

Compile and test it and see what happens

I edited the code. It is now a lot more simple. I complied and ran it. Both variables now are set correctly. This is maybe a silly question but how to make the constructor exits? If I put System.exit(0) inside the constructor then it only set the value for "anumber" and it will not exits out. So to make the constructor exits, I need to put System.exit(0); in my main() method that runs and tests the the program. What I want is that when the user select an element from that list and hit OK then the "astring" would be set and the program will also exit.

import java.awt.*;
import java.awt.event.*;
import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;


//public class SampleGUI extends Frame implements ItemListener{
    public class SampleGUI{
    

    public String astring;
    public String anumber;


    public static void main(String args[]){
        SampleGUI sg = new SampleGUI();
        System.out.println("class variable astring: " + sg.getString());
        System.out.println("class variable anumber: " + sg.getNumber());
        System.exit(0);
        
    }
     
    public SampleGUI(){

/*    
    JFileChooser chooser = new JFileChooser();
    chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
    
    
    chooser.setFileFilter(new javax.swing.filechooser.FileFilter() {
        public boolean accept(File f) {
            return f.getName().toLowerCase().endsWith(".txt") || f.isDirectory();
        }

        public String getDescription() {
            return "TXT Files";
        }
    }
    );
        
        
    int r = chooser.showDialog(new JFrame(),"Select .TXT File");
    
    if (r == JFileChooser.APPROVE_OPTION) {
        chooser.getSelectedFile().getName();
    }
    
    if(r == JFileChooser.CANCEL_OPTION)  System.exit(0);
*/

    
    JFrame frame = new JFrame();
    Object result = JOptionPane.showInputDialog(frame, "Enter a number:");
    System.out.println("User put in: " + result);
    setNumber(result.toString());
    
    
    /*
     Code to read in data from the input file and add to an String array "str_array".
     Since this part already worked fine for me, so I will not put it here and just
     assume that the String array "str_array" was initialized like bellow.
     */
     
    String [] str_array = new String[] {"a","b","c","d","e"};
     
    Object selectedValue = JOptionPane.showInputDialog(null, "Select an Element", "List of Elements",JOptionPane.INFORMATION_MESSAGE, null,str_array, str_array[0]);
    
    if(selectedValue.toString() == null) {
        System.out.println("User chose to cancel. Program will be terminated.");
        System.exit(0);
    }
    
    else
    {
        setString (selectedValue.toString());
    }
    }
          
    public String getString() {
        return astring;
    }
    
    public String getNumber (){
        return anumber;                
    }
    
    
    public void setNumber(String anumber) {
        this.anumber = anumber ;
    }
    
    
    public void setString (String astring){
        this.astring = astring;                
    }
    
}

how to make the constructor exits?

A constructor executes just like a method. How do you make a method exit?
One way is to execute up to the ending }.

the "astring" would be set and the program will also exit.

If the program is going to immediately exit, what difference does setting a variable make?
When the program exits, EVERYTHING in the program goes away.

A constructor executes just like a method. How do you make a method exit?

I guess I can do System.exit(0);

Or

I can do return aValue; (if this method does return something)

Or

I can just do return; (if it is a void method)

Since constructors do not return any values so I think I would either do System.exit(0); or return; to exit it.

If the program is going to immediately exit, what difference does setting a variable make?
When the program exits, EVERYTHING in the program goes away.

So what you was trying to say is that just let the method that calls the constructors to exit the program so that all the class variables be set correctly?

let the method that calls the constructors to exit the program so that all the class variables be set correctly?

If the program exits, the class variables do not exist. Nothing in memory from the program exists.

If the program exits, the class variables do not exist. Nothing in memory from the program exists.

I have to go. I will talk with you when I get home. Thanks.

If the program exits, the class variables do not exist. Nothing in memory from the program exists.

I think we misunderstood each other. Please look at your previous comment:

If you want astring to have a value from the user when the constructor exits, then you will have to use the same technique you use to get anumber.
Use a modal dialog that presents a list and have the use select from the list. The modal dialog will not return until the user has made the selection.

What you meant when you said "the constructor exits" ? How I do this? Since I can not put System.exit(0); or return; command inside my constructor because when I do that the astring will not get set to what the user selected from that list.

If I put System.exit(0); or return; inside my constructor, then this will happen when I run the program:

- the anumber set to the number user put in.
- the astring NOT set to the string user selected.
- the program exited.

If I not put System.exit(0) or return; inside my constructor and my main() method, then this will happen when I run it:

- both variables set to what they are supposed to.
- the program did NOT exited.

- If I not put System.exit(0) or return; inside my constructor, but I DO put System.exit(0) in my main() method, then this will happen when I run it:

- both variable set to what they are supposed to
- the program exited.

I think all I have to do is that just let the method that calls my constructor to exit the program by doing the System.exit(0);. Is that correct?

By the "constructor exits", I meant when it is finished executing and returns to the caller.
I was no referring to the System.exit() method.

By the "constructor exits", I meant when it is finished executing and returns to the caller.
I was no referring to the System.exit() method.

Ok, I think I've got everything clear. Thank you very much for your help. Have a good night.

You're welcome, good night

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.