Results 1 to 6 of 6
  1. #1
    reddens84 is offline Member
    Join Date
    Jul 2014
    Posts
    12
    Rep Power
    0

    Default Exceptions for multiple inputs

    I'm writing a program that accepts three doubles from the user, and performs calculations on them. I want to be able to handle input mismatch exceptions on each individually (i.e., if the user enters a double for the first, then a letter for the second, I'd like to be able to keep the value entered for the first, and only require that the user try again with the second, then the third, etc.)... I tried putting each input instance into its own try/catch blocks, but it always goes back to #1 when an invalid input is entered. Right now, I have all three in one try/catch:

    Java Code:
    package com.itse2317;
    import java.util.Scanner;
    import java.util.InputMismatchException;
    
    class Triangle
    {
        public static double side1 = 0.0, side2 = 0.0, side3 = 0.0;
        
        public static void main(String[] args)
        {
            double perimeter = 0.0, semi = 0.0, area = 0.0;
            String choice = "y";
            int count = 0;
    
            Scanner input = new Scanner(System.in);
            
            System.out.println("Welcome to the triangle calculation program.");
            do
            {
            try
                {
                    System.out.print("Please enter the length of side 1: ");
                    side1 = input.nextDouble();
                    System.out.print("Please enter the length of side 2: ");
                    side2 = input.nextDouble();
                    System.out.print("Please enter the length of side 3: ");
                    side3 = input.nextDouble();
                }
            
            catch(InputMismatchException e)
                {
                    if(count < 4)
                    {
                        System.out.println("Not a valid entry.");
                        input.nextLine();
                        count++;
                        continue;
                    }
                    
                    else
                    {
                        break;
                    }
    
                }
            
            triangleType();
            perimeter = perimeter(side1, side2, side3);
            semi = perimeter / 2;
            System.out.println("\nThe triangle's perimeter is: " + perimeter);
            area = area(semi, side1, side2, side3);
            System.out.println("The triangle's area is: " + area);
            input.nextLine();
            System.out.print("Would you like to continue (Y/N)?: ");
            choice = input.next();
            }
            
            while(choice.equalsIgnoreCase("y"));
            
            System.out.println("Goodbye.");
        }
        
        public static void triangleType()
        {
             if(side1 == side2 && side2 == side3)
             {
                 System.out.println("The triangle is Equilateral since it has equal"
                         + " length on all three sides.");
             }
             else if(side1 == side2 || side2 == side3 || side1 == side3)
             {
                 System.out.println("The triangle is Isosceles since it has two "
                         + "sides of equal length.");
             }
             else
             {
                 System.out.println("The triangle is Scalene with no equal sides.");
             }
        }
        
        public static double perimeter(double a, double b, double c)
        {
            double perimeter = a + b + c;
            return perimeter;
        }
        
        public static double area(double semi, double a, double b, double c)
        {
            double area = Math.sqrt(semi*(semi - a) * (semi - b) * (semi - c));
            return area;
        }
    }
    Can someone please help? Thanks

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Exceptions for multiple inputs

    Break your code down a bit:
    Java Code:
    do while "continue" {
        // Get the input
        // do calculation
        // prompt for continue
    }
    The "Get the input" part can be its own method then which returns a double[] of the three sides.
    That method would:
    Java Code:
    for each side {
        do while invalid input {
            // Prompt for input.
            // On exception then loop again
            // On valid value then exit.
        }
    }
    return the array
    If you can't do arrays yet then just make a method that returns a double and that does the inner do/while loop.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    reddens84 is offline Member
    Join Date
    Jul 2014
    Posts
    12
    Rep Power
    0

    Default Re: Exceptions for multiple inputs

    I guess I don't understand the 'for each' syntax. I tried looking it up, but the only example I found was printing 'i'... Here's what I tried:

    Java Code:
     static double[] getNumber()
        {
            double[] sides = new double[3];
            
            for(double i : sides)
                    {
                        try
                        {
                            System.out.println("Please enter the length: ");
                            i = input.nextDouble();
                        }
                        
                        catch(InputMismatchException e)
                                {
                                    System.out.println("Invalid input entered.");
                                }
                        
                    }
            
            System.out.println("Side1: " + sides[0]);
            System.out.println("Side2: " + sides[1]);
            System.out.println("Side3: " + sides[2]);
            return sides;
        }
    The printlns were just to see if it worked correctly, which it didn't... It showed 0.0 for all three values, even after I entered input. What am I doing wrong?

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Exceptions for multiple inputs

    That was pseudo code.
    It didn't actually mean use for-each.
    Sorry.
    Java Code:
    for (int i = 0; i < <your array>/length; i++) {
        // Get a valid value for the double at index i.
    }
    ETA: You;re also missing the key do-while inside the for loop.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    reddens84 is offline Member
    Join Date
    Jul 2014
    Posts
    12
    Rep Power
    0

    Default Re: Exceptions for multiple inputs

    Okay, I think I'm getting closer... Here's my full code:
    Java Code:
    package com.itse2317;
    import java.util.Scanner;
    import java.util.InputMismatchException;
    
    class Triangle
    {
        public static double[] sides = new double[3];
        public static Scanner input = new Scanner(System.in);
        
        public static void main(String[] args)
        {
            double perimeter = 0.0, semi = 0.0, area = 0.0;
            String choice = "y";
            
            System.out.println("Welcome to the triangle calculation program.");
            do
            {
            sides = getNumber();        
            System.out.println(sides[0]);
            System.out.println(sides[1]);
            System.out.println(sides[2]);
            triangleType();
            perimeter = perimeter(sides[0], sides[1], sides[2]);
            semi = perimeter / 2;
            System.out.println("\nThe triangle's perimeter is: " + perimeter);
            area = area(semi, sides[0], sides[1], sides[2]);
            System.out.println("The triangle's area is: " + area);
            input.nextLine();
            System.out.print("Would you like to continue (Y/N)?: ");
            choice = input.next();
            }
            
            while(choice.equalsIgnoreCase("y"));
            
            System.out.println("Goodbye.");
        }
        
        public static void triangleType()
        {
             if(sides[0] == sides[1] && sides[1] == sides[2])
             {
                 System.out.println("The triangle is Equilateral since it has equal"
                         + " length on all three sides.");
             }
             else if(sides[0] == sides[1] || sides[1] == sides[2] || sides[0] == sides[2])
             {
                 System.out.println("The triangle is Isosceles since it has two "
                         + "sides of equal length.");
             }
             else
             {
                 System.out.println("The triangle is Scalene with no equal sides.");
             }
        }
        
        public static double perimeter(double a, double b, double c)
        {
            double perimeter = a + b + c;
            return perimeter;
        }
        
        public static double area(double semi, double a, double b, double c)
        {
            double area = Math.sqrt(semi*(semi - a) * (semi - b) * (semi - c));
            return area;
        }
        
        static double[] getNumber()
        {
            double[] sides = new double[3];
            
            for(int i = 0; i < sides.length; i++)
                    {
                        do
                        {
                        try
                        {
                            System.out.print("Please enter the length: ");
                            sides[i] = input.nextDouble();
                            input.nextLine();
                        }
                        
                        catch(InputMismatchException e)
                                {
                                    System.out.println("Invalid input entered.");
                                }
                        }
                        while(!input.hasNextDouble());
                    }
            
            System.out.println("Side1: " + sides[0]);
            System.out.println("Side2: " + sides[1]);
            System.out.println("Side3: " + sides[2]);
            return sides;
        }
    }
    Two problems -- First, after the initial prompt for a length, you enter a number, and it prints a newline...And won't continue until you enter another value (without a prompt) then it moves on to the prompt for a second value, then third, etc... And it stores the second "first" value instead of the one entered at the prompt.

    The next thing is that once an invalid entry is made, it enters into an infinite loop, printing the "invalid input" message over and over... I would have thought a 'continue' at the end of the catch block would fix this, but apparently not...

    I appreciate all the help so far.

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Exceptions for multiple inputs

    Java Code:
    input.nextLine();
    You don't need that after the nextDouble();
    That's why you're having to hit return again.
    And you need a boolean flag here instead:
    Java Code:
    while(!input.hasNextDouble());
    So something like:
    Java Code:
    boolean valid = false;
    do {
        // set valid true if you successfuly read a number
    } while (!valid);
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Take multiple inputs in a single line.
    By kunalgaurav18 in forum New To Java
    Replies: 13
    Last Post: 10-27-2012, 05:36 AM
  2. Replies: 1
    Last Post: 03-19-2012, 01:11 AM
  3. Input class can't handle multiple inputs
    By musdem in forum New To Java
    Replies: 2
    Last Post: 03-17-2012, 12:03 AM
  4. Multiple Exceptions with Array
    By Cutter in forum New To Java
    Replies: 1
    Last Post: 12-11-2009, 07:02 PM
  5. if statement with multiple inputs?
    By soc86 in forum New To Java
    Replies: 3
    Last Post: 01-20-2009, 04:44 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •