I hade some poblems with this Block of code, Thought, I would just repost being the noob that em.
//Get Birth Year
String byear = JOptionPane.showInputDialog(null, "Enter Year of Birth:");
//Validate B Year
while(!(emp.isNumeric(byear, 1930, 1984) && byear.length() == 4 )){
if (emp.isNumeric(byear, 1930, 1984) && byear.length() == 4) {
byearValue = Integer.parseInt(byear);
}
else { JOptionPane.showMessageDialog(null, "Invalid Year Value");
byear = JOptionPane.showInputDialog(null, "Enter Year of Birth");
}
}
aEmp.setBYear(byearValue);
so the problem I believe was that byearValue was being destroyed before it gets outside the if statement., I solved the porblem by assing in parseint outside of if and while, this really doesn't solve my problem correctly (I think). Could use some insigt, Here is what I ahve now.
//Get Birth Year
String byear = JOptionPane.showInputDialog(null, "Enter Year of Birth:");
//Validate B Year
while(!(emp.isNumeric(byear, 1930, 1984) && byear.length() == 4 )){
if (emp.isNumeric(byear, 1930, 1984) && byear.length() == 4) {
byearValue = Integer.parseInt(byear);
}
else { JOptionPane.showMessageDialog(null, "Invalid Year Value");
byear = JOptionPane.showInputDialog(null, "Enter Year of Birth");
}
}
byearValue = Integer.parseInt(byear);
aEmp.setBYear(byearValue);
First off, what are you trying to accomplish with this code? I'm guessing validate the year entered on a form, but I'm not sure. But I'll go on this assumption.
I believe your logic is off here, a little refactoring might help. Create these methods:
String year = displayYearMessage();
while (!isValidYear(year)) {
displayInvalidYearMessage();
year = displayYearMessage();
}
aEmp.setBYear(Integer.parseInt(year));
Give this a try.
The main point I want to make is to understand how to break down the problem with the help of little helper methods. These methods may only have one line (or maybe a few), but if you give them descriptive names (important!), it'll help simplify the main logic you are trying to program. It'll help you find logic mistakes easier and later on when you come back and have to maintain this code it'll be easier to read.