Announcement

Collapse
No announcement yet.

Programming question

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Just skimming, but BlackCat is right -- after the exception is caught, the code resumes execution immediately after the catch.


    Which is also what happens right after the if(true condition) statement;, Asher.

    Comment


    • Adding a return statement to the body of the if() would make it no longer behave the same as the code it was replacing.

      Comment


      • Well, I wasn't looking at it from the perspective of performing like the code it replaced...I looked at it from the perspective of doing what is good and doing what is right.
        "The issue is there are still many people out there that use religion as a crutch for bigotry and hate. Like Ben."
        Ben Kenobi: "That means I'm doing something right. "

        Comment


        • Originally posted by Kuciwalker View Post
          Adding a return statement to the body of the if() would make it no longer behave the same as the code it was replacing.
          Uhm, no, the code was altered by my suggestion in post #108

          And to Asher, I didn't wan't to change DriXnaK's program structure - just get it to work. I quite agree that function should be in the try, except in initialisation. Say, check parameter file, connect to database, get CORBA server etc. wich I would put in their own try-catches.

          EDIT: and now hopefully without loss of connection I was a bit hasty, I would group try-catches according to functionallity - especially if there may be rollback involved.
          Last edited by BlackCat; October 21, 2010, 22:01.
          With or without religion, you would have good people doing good things and evil people doing evil things. But for good people to do evil things, that takes religion.

          Steven Weinberg

          Comment


          • Ok, then for the sake of argument, how would you construct this if you were doing it? Here's the full code for the Enter button:


            /*gets the value from the two text fields and sets them equal to the
            variables*/
            double paymentValue = Double.parseDouble(payment.getText());

            double timeValue = Double.parseDouble(time.getText());

            //Creates new object fram of Jframe class
            JFrame frame = new JFrame("Error");

            //Creates new object of NumberFormatException
            NumberFormatException problem = new NumberFormatException("Invalid Payment amount.");

            /*Checks for negative numbers and throws exception if found. Pops up Jpanel with
            error reason.*/
            try {
            if (paymentValue <= 0)
            throw problem;
            }
            catch (NumberFormatException exception){
            JOptionPane.showMessageDialog(frame,"Invalid payment amount. Must be greater than zero.");
            return;
            }

            try {
            if (timeValue <= 0)
            throw problem;
            }
            catch (NumberFormatException exception){
            JOptionPane.showMessageDialog(frame,"Invalid time amount. Must be greater than zero.");
            return;
            }
            //Sets the variables to column 1 and colum 2.
            ptArray[index][0] = timeValue;
            ptArray[index][1] = paymentValue;
            index++;




            }

            Comment


            • double paymentValue = Double.parseDouble(payment.getText());

              double timeValue = Double.parseDouble(time.getText());

              //Creates new object fram of Jframe class
              JFrame frame = new JFrame("Error");
              if (paymentValue <= 0) {
              JOptionPane.showMessageDialog(frame,"Invalid payment amount. Must be greater than zero.");
              } else {
              if (timeValue <= 0) {
              JOptionPane.showMessageDialog(frame,"Invalid time amount. Must be greater than zero.");
              } else {
              ptArray[index][0] = timeValue;
              ptArray[index][0] = timeValue;
              index++;
              }
              }



              }

              This should be the simplest.

              Edit: that is the simplest in your case, but in more complicated cases you may want to use exceptions, though, it all depends on the actual case
              With or without religion, you would have good people doing good things and evil people doing evil things. But for good people to do evil things, that takes religion.

              Steven Weinberg

              Comment


              • Ok, looks good to me. Thanks.

                Comment


                • Does the assignment specify what you are supposed to do if the user enters text into one of the number inputs? BlackCat's code crashes, in that case. To fix it, just make this modification:

                  Code:
                  double paymentValue;
                  double timeValue;
                  
                  try {
                  	paymentValue = Double.parseDouble(payment.getText()):
                  } catch (NumberFormatException e) {
                  	JOptionPane.showMessageDialog(frame,"Invalid payment amount. Must be a number.");
                  	return;
                  }
                  
                  try {
                  	timeValue = Double.parseDouble(time.getText()):
                  } catch (NumberFormatException e) {
                  	JOptionPane.showMessageDialog(frame,"Invalid time amount. Must be a number.");
                  	return;
                  }
                  
                  //Creates new object fram of Jframe class
                  JFrame frame = new JFrame("Error");
                  if (paymentValue <= 0) {
                  	JOptionPane.showMessageDialog(frame,"Invalid payment amount. Must be greater than zero.");
                  } else {
                  	if (timeValue <= 0) {
                  		JOptionPane.showMessageDialog(frame,"Invalid time amount. Must be greater than zero.");
                  	} else {
                  		ptArray[index][0] = timeValue;
                  		ptArray[index][0] = timeValue;
                  		index++;
                  	}
                  }

                  Comment


                  • Originally posted by BlackCat View Post
                    double paymentValue = Double.parseDouble(payment.getText());

                    double timeValue = Double.parseDouble(time.getText());

                    //Creates new object fram of Jframe class
                    JFrame frame = new JFrame("Error");
                    if (paymentValue <= 0) {
                    JOptionPane.showMessageDialog(frame,"Invalid payment amount. Must be greater than zero.");
                    } else {
                    if (timeValue <= 0) {
                    JOptionPane.showMessageDialog(frame,"Invalid time amount. Must be greater than zero.");
                    } else {
                    ptArray[index][0] = timeValue;
                    ptArray[index][0] = timeValue;
                    index++;
                    }
                    }



                    }

                    This should be the simplest.

                    Edit: that is the simplest in your case, but in more complicated cases you may want to use exceptions, though, it all depends on the actual case
                    Why would you construct a JFrame if you may not need it? It's unnecessary -- slows things down, uses more memory, and potentially (hopefully) not needed. You don't need to keep a reference to it, either, so just anonymously instantiate it:

                    Code:
                    double paymentValue = Double.parseDouble(payment.getText());
                    
                            double timeValue = Double.parseDouble(time.getText());        
                    
                    	if (paymentValue <= 0) {
                    		JOptionPane.showMessageDialog(new JFrame("Error"),"Invalid payment amount. Must be greater than zero.");
                    	} else {
                    		if (timeValue <= 0) {
                    			JOptionPane.showMessageDialog(new JFrame("Error"),"Invalid time amount. Must be greater than zero.");
                    		} else {
                    			ptArray[index][0] = timeValue;
                    			ptArray[index][0] = timeValue;
                    			index++;
                    		}
                    	}
                        }
                    I don't like this because parseDouble throws exceptions. The code is not robust.

                    So:
                    Code:
                    try
                    {
                    	double paymentValue = Double.parseDouble(payment.getText());
                    	double timeValue = Double.parseDouble(time.getText());        
                    
                    	if (paymentValue <= 0) 
                    	{
                    		JOptionPane.showMessageDialog(new JFrame("Error"),"Invalid payment amount. Must be greater than zero.");
                    	} 
                    	else 
                    	{
                    		if (timeValue <= 0)
                    		 {
                    			JOptionPane.showMessageDialog(new JFrame("Error"),"Invalid time amount. Must be greater than zero.");
                    		}
                    		else 
                    		{
                    			ptArray[index][0] = timeValue;
                    			ptArray[index][0] = timeValue;
                    			index++;
                    		}
                    	}
                    }
                    catch (Exception e)
                    {
                    	JOptionPane.showMessageDialog(new JFrame("Error"),"Invalid input.");
                    }
                    There's other things I'd do to make this most robust (theoretically the JOptionPanes could throw an exception and should be caught, if they threw one now the user would get a misleading message about input). But that would definitely be overkill. I also had to resist the temptation to make an error() function:
                    Code:
                    public void showError(String errorText)
                    {
                    	JOptionPane.showMessageDialog(new JFrame("Error"),errorText);
                    }
                    And I'd then replace all JOptionPane calls with showError("blah");
                    "The issue is there are still many people out there that use religion as a crutch for bigotry and hate. Like Ben."
                    Ben Kenobi: "That means I'm doing something right. "

                    Comment


                    • All of Asher's suggestion are, of course, correct. Just remember that if you did have to do anything with the error dialog besides just popping it up, you couldn't create it anonymously and would have to bind it to a variable. But in general if you have the pattern:

                      x = something;
                      ...
                      foo(x);

                      and those are the only places you use x, you might as well just get rid of it and call foo(something) directly.

                      Comment


                      • Originally posted by Kuciwalker View Post
                        All of Asher's suggestion are, of course, correct. Just remember that if you did have to do anything with the error dialog besides just popping it up, you couldn't create it anonymously and would have to bind it to a variable.
                        Aye.

                        When possible, I use anonymous objects because it, in theory, also gets rid of that allocated memory faster. As I'm sure you know (but others may not), when you assign an object to a variable, it's kept in memory until that variable passes out of scope, at which point the object is marked for deletion at the next garbage collection pass. With anonymous objects, the scope passes as soon as it's on to the next line.

                        It usually doesn't make a difference, but it can...depending how your program is designed.
                        "The issue is there are still many people out there that use religion as a crutch for bigotry and hate. Like Ben."
                        Ben Kenobi: "That means I'm doing something right. "

                        Comment


                        • Originally posted by Asher View Post
                          Why would you construct a JFrame if you may not need it? It's unnecessary -- slows things down, uses more memory, and potentially (hopefully) not needed.
                          Probably because it's 4 oclock in the moning here and I'm a bit drunk I think that C++ would give me a warning that it wasn't used

                          I don't like this because parseDouble throws exceptions. The code is not robust.
                          Not a java programmer, so I wasn't aware - guess that it fixes Kuci's problem .

                          There's other things I'd do to make this most robust (theoretically the JOptionPanes could throw an exception and should be caught, if they threw one now the user would get a misleading message about input). But that would definitely be overkill. I also had to resist the temptation to make an error() function:
                          Code:
                          public void showError(String errorText)
                          {
                          	JOptionPane.showMessageDialog(new JFrame("Error"),errorText);
                          }
                          And I'd then replace all JOptionPane calls with showError("blah");
                          Now you are talking dirty
                          With or without religion, you would have good people doing good things and evil people doing evil things. But for good people to do evil things, that takes religion.

                          Steven Weinberg

                          Comment


                          • I'm anal about good program design. I don't care if sloppy code works, it'll break something in the future.

                            Every time I do a commit at work for a change, I change a ridiculous amount of stuff unrelated to the stuff I'm supposed to be implementing/fixing. As I'm reading over the code I compulsively refactor. I've got some kind of OCD-like annoyance with sloppy code.

                            It annoyed the hell out of one of my managers at one point, who considered the refactorings "pointless" because they did not explicitly add any new user-facing features. Fortunately, my managers today are former developers themselves and appreciate the value.
                            "The issue is there are still many people out there that use religion as a crutch for bigotry and hate. Like Ben."
                            Ben Kenobi: "That means I'm doing something right. "

                            Comment


                            • It doesn't require me to do anything beyond what's in that code, fortunately. I may have to do something like that though in the future since there's another course after this I have to take and I have to develop a portfolio of java applications. If I can finish up task 2 I'll start that course sometime next week probably. So knowing all of this stuff is good one way or another, even if some of it is a bit beyond me at this point. I'm probably going to commit myself to learning a language and getting really good at it after I get this degree because it seems that knowing how to code is useful in a lot of areas and it's just something good to have if even only for personal use. I'm thinking Python would be best for me. Would you agree on Python or maybe a different language? Not from a security standpoint for now.

                              Comment


                              • Originally posted by Asher View Post
                                I'm anal about good program design. I don't care if sloppy code works, it'll break something in the future.
                                Dammit, we agree

                                Every time I do a commit at work for a change, I change a ridiculous amount of stuff unrelated to the stuff I'm supposed to be implementing/fixing. As I'm reading over the code I compulsively refactor. I've got some kind of OCD-like annoyance with sloppy code.

                                It annoyed the hell out of one of my managers at one point, who considered the refactorings "pointless" because they did not explicitly add any new user-facing features. Fortunately, my managers today are former developers themselves and appreciate the value.
                                Well, my boss is a CS and has the same ideas, so our code are pretty stable, so I really seldom have that problem.
                                With or without religion, you would have good people doing good things and evil people doing evil things. But for good people to do evil things, that takes religion.

                                Steven Weinberg

                                Comment

                                Working...