Andorius
Andorius

Reputation: 105

JOptionPane input validation

I'm looking for a good way to check the user's input via JOptionPane showConfirmDialog, making sure it contains a string and a credible age. The idea is to use these input and add them to an object, that then gets added to an ArrayList.

The problem is in the "NyLis" class below". Name = Namn, and Land = country. Ålder = age. Age should be between 18 and 100.

  1. Is there a way to check that the string is an actual string?
  2. is there a way to return the window if input is invalid, and keep the previous input, so the use can pick up where it went wrong?
  3. Are try and catch blocks a good option here, and how would I implement them?

I've been playing around with while loops and try catch block, but I can't wrap my silly head around it all.

Any help is immensely appreciated.

// JOptionPane window

        Form(){

        setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));

        JPanel rad0 = new JPanel();
        rad0.add(new JLabel("StartNr: "+ list.size()+1+""));
        add(rad0);

        JPanel rad1 = new JPanel();
        rad1.add(new JLabel("Namn: "));
        namnFält = new JTextField(15);
        rad1.add(namnFält);
        add(rad1);

        JPanel rad2 = new JPanel();
        rad2.add(new JLabel("Land: "));
        landFält = new JTextField(15);
        rad2.add(landFält);
        add(rad2);

        JPanel rad3 = new JPanel();
        ålderFält = new JTextField(5);
        rad3.add(ålderFält);
        rad3.add(new JLabel("Ålder: "));

        rad3.add(ålderFält);
        add(rad3);


    }

}

// Listener

class NyLis implements ActionListener{
    public void actionPerformed(ActionEvent ave){

        Form f = new Form();            

        int svar = JOptionPane.showConfirmDialog(null, f);
        if (svar != JOptionPane.OK_OPTION)

            return;

            String namn = f.namnFält.getText();
            String land = f.landFält.getText();
            int ålder = Integer.parseInt(f.ålderFält.getText());

            boolean success=false;

            while(!success){
            JOptionPane.showMessageDialog(null, "Fel. Försök igen.");
            int svar2 = JOptionPane.showConfirmDialog(null, f);
            if (svar2 != JOptionPane.OK_OPTION)

            return;

            if(!namn.isEmpty() && !land.isEmpty()&&!(ålder<18 || ålder>100)){           
            success=true;

             int startNr = list.size()+1;


           Tävlande tv = new Tävlande (namn,land,ålder,startNr,Double.MAX_VALUE);
            list.add(tv);
            visa.setEnabled(true);

                    }

                        }



                }
        }

// Object

public class Tävlande implements Comparable<Tävlande>{



    private String namn;
    private String land;
    private int ålder;
    private int startNr;
    private double tid;

    public Tävlande (String namn, String land,int ålder,int startNr, double tid){
        this.namn = namn;
        this.land = land;
        this.ålder = ålder;
        this.startNr = startNr;
        this.tid = tid;
    }

    public String getNamn(){
        return namn;
    }


    public String getLand(){
        return land;
    }

    public int getÅlder(){
        return ålder;
    }

    public int getStartNr(){
        return startNr;
    }

    public double getTid(){
        return tid;
    }

    public void setTid(Double tid) {
        this.tid = tid;
    }

    public String toString(){
        String str =  namn +" "  + ", Land: "+land + ", Ålder: " +ålder+ ", Startnummer: " + " "+startNr;
        return str;

    }
    public String toString2(){
        String str =  namn +" "  + ", Land: "+land + ", Ålder: " +ålder+ ", Startnummer: " + " "+startNr+  ", Tid: "+tid;
        return str;

    }
    public boolean equals(Object other){
        if (other instanceof Tävlande){
            Tävlande t = (Tävlande) other;
            if (startNr == t.startNr)
                return true;

            else
                return false;

        }
        else{
            return false;
        }
    }

    @Override

    public int compareTo(Tävlande other) {
        if(startNr < other.startNr)
            return -1;  
        else if (startNr > other.startNr)
            return 1;
        else 
            return 0;

    }

}

enter image description here

Upvotes: 0

Views: 468

Answers (2)

Andrew Thompson
Andrew Thompson

Reputation: 168815

SpinnerNumberModel ageModel = new SpinnerNumberModel(25, 18, 100, 1);
JSpinner ageSpinner = new JSpinner(ageModel);
JOptionPane.showMessageDialog(
    frame, ageSpinner, "Age?", JOptionPane.QUESTION_MESSAGE);
System.out.println(ageSpinner.getValue());

Upvotes: 2

Ejesalva
Ejesalva

Reputation: 123

Well there is several ways you could go about doing it. For the date you could try using some methods like String Split to break up the dates by '/' and see if the dates are within a relevant range.

String string = "4/10/2014";
String[] parts = string.split("/");
String part1 = parts[0]; // 4
String part2 = parts[1]; // 10
String part3 = parts[2]; // 2014

Then do a while loop and loop the user back to the beginning for new input if the values aren't Int's or are to high or low.

Heres a link from a previous question about Int verification: Link!

Upvotes: -1

Related Questions