5 Replies - 1656 Views - Last Post: 25 October 2012 - 10:44 AM Rate Topic: -----

#1 Mzaq  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 24-October 12

Triangle Type Calculator try/catch error

Posted 24 October 2012 - 09:39 PM

The question I have is that when I run this program, it doesn't do what I exactly want it to do. I set up a try/catch statement so that if the user enters anything besides an integer it would tell them that it is not an integer. The way the code is set up currently it never gets to the try/catch statement because the Integer.parseInt comes up with a NumberFormatException error.

//Tests to see if a triangle is equilateral, isosceles, or scalene.

import javax.swing.*;
  
public class Sides
{
    public static void main(String[] args)
    {
 		  String s1, s2, s3, type;
		  int side1, side2, side3;
		  	
        s1 = JOptionPane.showInputDialog("Enter the length of side 1: ");
        side1 = Integer.parseInt(s1);
 
        s2 = JOptionPane.showInputDialog("Enter the length of side 2: ");
        side2 = Integer.parseInt(s2);
 
		  s3 = JOptionPane.showInputDialog("Enter the length of side 3: ");
        side3 = Integer.parseInt(s3);
		  
		  try
		  {
         side1 = Integer.parseInt(s1);
         side2 = Integer.parseInt(s2);
			side3 = Integer.parseInt(s3);
			
			}		
		  catch (NumberFormatException e)
		  {
          JOptionPane.showMessageDialog(null, "This is not a valid side length.");
			 System.exit(0);
			}
	
		  if(side1 == side2 && side2 == side3)
		  {
        	type = "Equilateral";
		  }
 
        else if(side1 == side2 && side2 != side3)
		  {
        	type = "Isosceles";
		  }
 
        else if(side1 == side3 && side2 != side3)
		  {
        	type = "Isosceles";
		  }
 
        else if(side1 != side3 && side2 == side3)
		  {
        	type = "Isosceles";
		  }
 
        else
		  {
        	type = "Scalene";
		  }
 
        JOptionPane.showMessageDialog(null, "This triangle is a " + type + " triangle.");
    }
 
}


Basically, I'm asking where is the correct spot for the try/catch statement and if the current code in the try statement is correct.

Is This A Good Question/Topic? 0
  • +

Replies To: Triangle Type Calculator try/catch error

#2 farrell2k  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 823
  • View blog
  • Posts: 2,534
  • Joined: 29-July 11

Re: Triangle Type Calculator try/catch error

Posted 24 October 2012 - 10:15 PM

The proper spott for the try/catch is when you call parseInt(). You call parseInt() after prompting for a number, but don't bother to enclose it into a try/catch. You then have parseInt() enclosed in a try catch later on in your code, which is pointless.
Was This Post Helpful? 0
  • +
  • -

#3 Mzaq  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 24-October 12

Re: Triangle Type Calculator try/catch error

Posted 25 October 2012 - 08:26 AM

View Postfarrell2k, on 24 October 2012 - 10:15 PM, said:

The proper spott for the try/catch is when you call parseInt(). You call parseInt() after prompting for a number, but don't bother to enclose it into a try/catch. You then have parseInt() enclosed in a try catch later on in your code, which is pointless.

So would I have to make a separate try/catch for each parseInt()?
Was This Post Helpful? 0
  • +
  • -

#4 farrell2k  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 823
  • View blog
  • Posts: 2,534
  • Joined: 29-July 11

Re: Triangle Type Calculator try/catch error

Posted 25 October 2012 - 09:52 AM

View PostMzaq, on 25 October 2012 - 03:26 PM, said:

View Postfarrell2k, on 24 October 2012 - 10:15 PM, said:

The proper spott for the try/catch is when you call parseInt(). You call parseInt() after prompting for a number, but don't bother to enclose it into a try/catch. You then have parseInt() enclosed in a try catch later on in your code, which is pointless.

So would I have to make a separate try/catch for each parseInt()?


You could, sure. I would.
Was This Post Helpful? 0
  • +
  • -

#5 kellzor  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 01-May 12

Re: Triangle Type Calculator try/catch error

Posted 25 October 2012 - 10:24 AM

pretty much what he said, there are other ways of doing this but if you simply rearrange the code you can get what you wanted.

        String s1, s2, s3, type;
	int side1, side2, side3;
		  	
        s1 = JOptionPane.showInputDialog("Enter the length of side 1: ");
        s2 = JOptionPane.showInputDialog("Enter the length of side 2: ");
        s3 = JOptionPane.showInputDialog("Enter the length of side 3: ");
        try{
            Integer.parseInt(s1);
            Integer.parseInt(s2);
            Integer.parseInt(s3);
        }
        catch(NumberFormatException e){
            JOptionPane.showMessageDialog(null, "One of the values are not a valid integer.");
            System.exit(0);
        }
        side1 = Integer.parseInt(s1);
        side2 = Integer.parseInt(s2);
        side3 = Integer.parseInt(s3);

Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Triangle Type Calculator try/catch error

Posted 25 October 2012 - 10:44 AM

Kind of wasting system resources calling parseInt() twice not counting the additional lines of code

just initialize you side to 0 and assign the values inside the try {

    
     int side1 = 0, side2 = 0, side3 = 0;
	  	
       s1 = JOptionPane.showInputDialog("Enter the length of side 1: ");
       s2 = JOptionPane.showInputDialog("Enter the length of side 2: ");
       s3 = JOptionPane.showInputDialog("Enter the length of side 3: ");
       try{
           side1 = Integer.parseInt(s1);
           side2 = Integer.parseInt(s2);
           side3 = Integer.parseInt(s3);
       }
       catch(NumberFormatException e){
           JOptionPane.showMessageDialog(null, "One of the values are not a valid integer.");
           System.exit(0);
       }


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1