Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: program worked until i tried to implement InputMismatchException

  1. #1
    Junior Member
    Join Date
    Jun 2014
    Location
    Northern Utah
    Posts
    13
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default program worked until i tried to implement InputMismatchException

    was working fine but when I tried to use try and catch, it now says my variables are not initialized.


     
    /***********************
    *  BMI.java
    *  Eddy Caraballo
    *  
    ***********************/
     
    import java.util.*;   
     
    public class BMI
     
    {
     
       public static void main(String[] args)
       {
       Scanner stdIn = new Scanner(System.in); 
     
       System.out.println("Welcome to the CS1400 BMI Calculator");
       System.out.println("Based on your height and weight we will figure out your BMI");
     
       int heightInches; 
       int weightPounds;
       int bMI;
     
       try
       {
       System.out.print("How tall are you in inches?");
       heightInches = stdIn.nextInt();                              
       }
     
       catch (InputMismatchException e)
       {
       System.out.println("Please use numbers when entering your height");
       }
       try
       {
       System.out.print("How much do you weigh in pounds?");
       weightPounds = stdIn.nextInt();                              
       }
       catch (InputMismatchException e)
       {
       System.out.println("Come on now, Please use numbers when entering your weight");
       }
     
       bMI = (((704) * weightPounds) / (heightInches * heightInches)); 
       System.out.println("Your height is " + heightInches + "in.");
     
       System.out.println("Your weight is " + weightPounds + "lbs.");
     
       System.out.println("Your BMI is " + bMI);
     
     
     
     
     
     
     
       }
     
     
    }

    here are the errors

    BMI.java:44: error: variable weightPounds might not have been initialized
       bMI = (((704) * weightPounds) / (heightInches * heightInches)); 
                       ^
    BMI.java:44: error: variable heightInches might not have been initialized
       bMI = (((704) * weightPounds) / (heightInches * heightInches)); 
                                        ^
    2 errors


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,400
    My Mood
    Hungover
    Thanks
    144
    Thanked 628 Times in 538 Posts

    Default Re: program worked until i tried to implement InputMismatchException

    The error means what it says: the variables weightPounds and heightInches will not be initialized if the user enters an incorrect variable. Java doesn't know what to do with that, so it gives you this compiler error.

    You need to either give the variables a default value, or rearrange your code so that you only use the variables after they're set.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. The Following User Says Thank You to KevinWorkman For This Useful Post:

    Heri623 (July 2nd, 2014)

  4. #3
    Junior Member
    Join Date
    Jun 2014
    Location
    Northern Utah
    Posts
    13
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: program worked until i tried to implement InputMismatchException

    Awesome, Thank you!!

Similar Threads

  1. Replies: 12
    Last Post: October 22nd, 2013, 09:11 PM
  2. Solve InputMismatchException
    By idkwtp100 in forum Exceptions
    Replies: 0
    Last Post: February 28th, 2013, 12:28 AM
  3. [SOLVED] InputMisMatchException
    By jmack in forum Exceptions
    Replies: 3
    Last Post: January 26th, 2011, 09:30 AM
  4. Hours worked help
    By glacier23 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: December 10th, 2010, 11:58 PM
  5. INPUTMISMATCHEXCEPTION
    By james in forum What's Wrong With My Code?
    Replies: 7
    Last Post: February 15th, 2010, 12:02 AM