Results 1 to 3 of 3
  1. #1
    computerbum is offline Member
    Join Date
    Feb 2010
    Posts
    26
    Rep Power
    0

    Default Exception handling problem

    Why in the world am I getting a continuous loop when I enter a character instead of a float variable when the program asks the user to enter a float value?

    I created an exception to handle the incorrect data but it will not work correctly, why?



    Java Code:
    import java.util.*;
    import java.io.*;
    
    public class FloatingNumbersDriver {
    
        public static void main(String[] args) {
    
            Scanner in = new Scanner(System.in);
            FloatingNumbers myValues = new FloatingNumbers();
            final int MAX_ERRORS = 0;
            int errors = 0, totalValues = 0, count = 1;
            String answer;
            float value = 0, sum = 0;
    
     
          
    
                System.out.print("\nEnter the total amount of float values to sum up: ");
                totalValues = in.nextInt();
    
    
                while(count <= totalValues){
                    try
                    {
                System.out.print("\nEnter a floating-point value: ");
                value = in.nextFloat();
                sum += value;
                count++;                               // increment total
                    }
          
            catch(InputMismatchException e){
                    System.out.println(" Invalid input, must enter a float value");
                    errors++;
          
            }
    
                }
        }               
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Don't forget to grab the end-of-line char with the Scanner object:
    Java Code:
    import java.util.*;
    
    public class FloatingNumbersDriver {
    
      public static void main(String[] args) {
    
        Scanner in = new Scanner(System.in);
        int errors = 0, totalValues = 0, count = 1;
        float value = 0, sum = 0;
    
        System.out.print("\nEnter the total amount of float values to sum up: ");
        totalValues = in.nextInt();
        in.nextLine();
    
        while (count <= totalValues) {
          try {
            System.out.print("\nEnter a floating-point value: ");
            value = in.nextFloat();
            in.nextLine();
            sum += value;
            count++; // increment total
          }
    
          catch (InputMismatchException e) {
            in.nextLine(); // *** this one especially! ***
            System.out.println(" Invalid input, must enter a float value");
            errors++;
          }
        }
      }
    }

  3. #3
    computerbum is offline Member
    Join Date
    Feb 2010
    Posts
    26
    Rep Power
    0

    Default

    I appreciate that, I have been going crazy trying to figure out what it wrong. I will always remember that.

    Thanks again:)

Similar Threads

  1. Exception Handling help
    By MZA in forum New To Java
    Replies: 3
    Last Post: 02-10-2010, 09:23 AM
  2. Exception Handling Related
    By dnzzn in forum New To Java
    Replies: 9
    Last Post: 09-29-2009, 09:45 AM
  3. Exception handling and logging
    By jurka in forum New To Java
    Replies: 8
    Last Post: 09-03-2008, 07:07 PM
  4. Problem with JSP exception handling page
    By sidster in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 06-19-2008, 07:28 PM
  5. Exception Handling...
    By focus_nitin in forum New To Java
    Replies: 1
    Last Post: 02-16-2008, 03:13 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
  •