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.
- Is there a way to check that the string is an actual string?
- 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?
- 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;
}
}