The Artima Developer Community
Sponsored Link

Java Answers Forum
Need some INsight

1 reply on 1 page. Most recent reply: Nov 21, 2002 8:57 AM by Ryan Shriver

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Trevor

Posts: 3
Nickname: paintcheck
Registered: Nov, 2002

Need some INsight Posted: Nov 20, 2002 4:44 PM
Reply to this message Reply
Advertisement
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);


Ryan Shriver

Posts: 9
Nickname: rshriver
Registered: Oct, 2002

Re: Need some INsight Posted: Nov 21, 2002 8:57 AM
Reply to this message Reply
OK, I'll take a stab at this.

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:

private boolean isValidYear(String year) {
    return emp.isNumeric(byear, 1930, 1984) && byear.length() == 4;
}
 
private String displayYearMessage() {
    return JOptionPane.showInputDialog(null, "Enter Year of Birth");
}
 
private void displayInvalidYearMessage() {
    JOptionPane.showMessageDialog(null, "Invalid Year Value");
}


So then lets try this:

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.

Hope this helps.

-ryan

Flat View: This topic has 1 reply on 1 page
Topic: Fat Client Previous Topic   Next Topic Topic: Plz Help me ! everybody

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use