0

so I have been writing a program that takes user input for employee data and adds it to an ArrayList for display. The program is working fine except for two points. The first is that I am trying to validate that the user entered an hourly rate between $6 and $160. Here is the code for that:

    try{
        double r = Double.parseDouble(rate.getText());

        if (r >= 6 && r <= 150){
            test.setRate(r);
        }
    } catch(NumberFormatException e){
        JOptionPane.showMessageDialog(null, "Sorry, hourly rate must be between $6 and $150.");
        return;
    }

It does validate that it is between those values, but if it is not it simply sets it to 0 rather than display the message to the user and return out of the method.

My second problem is a little more complex, and to be honest I am completely lost. When I run the program to test or to debug it opens two windows. The primary window is completely blank but controls the EXIT_ON_CLOSE command while the second has everything that I coded into it. I have tried searching for a second JFrame that might have been initialized but cannot find one. I have a larger amount of code and am not sure what might be needed but here is the code that I believe is causing the problem based off of the behavior:

    face = new JFrame();
    face.setSize(300,300);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    GroupLayout design = new GroupLayout(face.getContentPane());
    face.setLayout(design);
    face.setVisible(true);

    design.setAutoCreateGaps(true);
    design.setAutoCreateContainerGaps(true);
    design.setVerticalGroup
    (
        design.createSequentialGroup()
            .addGroup(design.createParallelGroup(GroupLayout.Alignment.LEADING)
                .addComponent(fName)
                .addComponent(first)
                .addComponent(list))
            .addGroup(design.createParallelGroup(GroupLayout.Alignment.LEADING)                    
                .addComponent(lName)
                .addComponent(last))
            .addGroup(design.createParallelGroup(GroupLayout.Alignment.LEADING)                    
                .addComponent(hWork)
                .addComponent(hours))
            .addGroup(design.createParallelGroup(GroupLayout.Alignment.LEADING)                    
                .addComponent(hRate)
                .addComponent(rate))
            .addComponent(admin)
            .addComponent(market)
            .addComponent(account)
            .addComponent(prod)
            .addComponent(sales)
            .addGroup(design.createParallelGroup(GroupLayout.Alignment.LEADING)                    
                .addComponent(submit)
                .addComponent(exit))
    );


    design.setHorizontalGroup
    (
        design.createSequentialGroup()
            .addGroup(design.createParallelGroup(GroupLayout.Alignment.LEADING)
                .addComponent(fName)
                .addComponent(lName)
                .addComponent(hWork)
                .addComponent(hRate)
                .addComponent(admin)
                .addComponent(market)
                .addComponent(account)
                .addComponent(prod)
                .addComponent(sales)
                .addComponent(submit))
            .addGroup(design.createParallelGroup(GroupLayout.Alignment.LEADING)
                .addComponent(first)
                .addComponent(last)
                .addComponent(hours)
                .addComponent(rate)
                .addComponent(exit))
            .addComponent(list)
    );
}

Other than creating the individual controls this is everything that has to do with the GUI design. Any insight would be very welcome here! If I forgot to post something I would be happy to do so.

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
Cameron Briggs
  • 71
  • 1
  • 1
  • 8
  • *"Validating a double to be between two values.."* `JSpinner` with a `SpinnerNumberModel` *"..and GUI opens two JFrames"* See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) ..What was the question? – Andrew Thompson Oct 28 '14 at 04:53
  • @AndrewThompson I don't think the OP wants the second frame, but without a runnable example, it's impossible to know where it's coming from... – MadProgrammer Oct 28 '14 at 04:57
  • While I really like the idea of using a JSpinner (it would be a fun feature) I have to validate the user's input. I had read that post while I was researching my problem about the second window. It is actually hard to explain. When I run the program, two windows automatically when I only intended one. The one I didn't mean to open is blank and I can't see where I told it to open at. – Cameron Briggs Oct 28 '14 at 05:02
  • I would be more than happy to post the full code, it is just rather long and I didn't want to have too much unnecessary code to search through. – Cameron Briggs Oct 28 '14 at 05:04

2 Answers2

1

Try reading this allowed...

if (r >= 6 && r <= 150){
    test.setRate(r);
}

If you're like me you to then end and went "now what?"...what happens when the values parses correctly to a double, but is outside the range...?

You will need to add a else condition to trap all the conditions out side of your specified range...

if (r >= 6 && r <= 150){
    test.setRate(r);
} else {
    // Show error message here
}

As for your second problem, consider providing a runnable example which demonstrates your problem. This will result in less confusion and better responses.

In the mean time, you might also like to take a look at How to Use Spinners, which will allow you to define a component which will pretty much do what you code snippet is trying to do...

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
1

Your first problem occurs because if r is not between 6 and 150 the try block doesn't throw a NumberFormatException so your catch isn't execute. You need to add an else statement to display that message.

boolean showError = false;
try{
    double r = Double.parseDouble(rate.getText());

    if (r >= 6 && r <= 150){
        test.setRate(r);
    }else{
       showError = true;
    }
} catch(NumberFormatException e){
     showError = true;
}

if(showError){
    JOptionPane.showMessageDialog(null, "Sorry, hourly rate must be between $6 and     $150.");
}

Since you are use setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); directly I suppose you're executing this code in a class that extends JFrame which means that you have 2 JFrames the one that you create when you call the class that holds that code and face.

If you have a class that extends JFrame, don't create other JFrames, you should remove:

face = new JFrame();
Titus
  • 22,031
  • 1
  • 23
  • 33
  • Oh my goodness! I can't believe I didn't see that, thank you so much. It is now calculating the way that it should be. As far as my second issue, I will keep trying at it to see what I can do. If you want a runnable example I would be happy to post it. My main concern for now was the validation. – Cameron Briggs Oct 28 '14 at 05:09