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 6 of 6

Thread: Variables not initialized?

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Posts
    6
    My Mood
    Yeehaw
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Variables not initialized?

    I've just started learning Java and I'm currently writing a code for a homework project. This is the description:
    Write an application that lets the user add, subtract, multiply, or divide two fractions. Use the rational class for your application.

    This is what I have so far: (I added the last part in case someone typed capitals)
    import java.util.*;
    public class chap4pp7
    {public static void main() { 
            Scanner scan = new Scanner(System.in); 
            Rational r1; 
            Rational r2; 
            Rational r3, r4, r5, r6; 
            System.out.println("First fraction's numerator: " );
            int a = scan.nextInt();
            System.out.println("First fraction's denominator: " );
            int b = scan.nextInt();
            System.out.println("Second fraction's numerator: ");
            int c = scan.nextInt();
            System.out.println("Second fraction's denominator: ");
            int d = scan.nextInt(); 
     
            r1 = new Rational(a,b);
            r2 = new Rational(c,d);
     
            int add, subtract, multiply, divide; 
            System.out.println("Please type in the desired function (no caps)-- add, subtract, multiply,"
                + "or divide: ");
            int e = scan.nextInt();
            int Add, Subtract, Multiply, Divide;
     
            if (e == add){
                r3 = r1.add(r2);  
                System.out.println("Your total is: " +r3);
            }
                else 
                if (e == subtract){ 
                    r4 = r1.subtract(r2);
                System.out.println("Your total is: " +r4);
            }
                else 
                if (e == multiply){
                    r5 = r1.multiply(r2);
                System.out.println("Your total is: " +r5);
            }
                else 
                if (e == divide){
                    r6 = r1.divide(r2);
                System.out.println("Your total is: " +r6); 
            }
     
                //the next part is just in case the user types with capitals. 
                //i am aware this part isn't necessary it's just for me
                //I did them each separately. 
                else 
                if (e == Add){
                System.out.println("Please type in lowercase.");
            }
                else
                if (e ==Subtract){
                System.out.println("Please type in lowercase."); 
            }
                else
                if (e == Multiply){
                System.out.println("Please type in lowercase.");
            }
                else
                if (e == Divide){
                System.out.println("Please type in lowercase.");
     
            } 
    }
    }


    My issue was with the beginning of all of the if statements. When I tried to compile, I got an error message that said "variable add might not have been initialized". The problem was in the line if(e==add). I assume it is the same issue with all of the following variables as well. What can I do to fix this? Also, why is the program uncertain? MIGHT not have been initialized?
    Anyway thanks in advance for your help! I realize this isn't the slickest code on the block but if you see any other errors feel free to let me know!

    --- Update ---

    OH GOD i just realized i posted this in the wrong sub forum i think.
    FORGIVE ME I DIDN'T SEE IT BEFORE


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Variables not initialized?

    The error error message is very informative. It says one or more of your variables IS NOT initialised. So what do you think you need to do?
    Improving the world one idiot at a time!

  3. #3
    Junior Member
    Join Date
    Oct 2013
    Posts
    6
    My Mood
    Yeehaw
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Variables not initialized?

    set them equal to 0? where I had them established before?

  4. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Variables not initialized?

    What happened when you tried?
    Improving the world one idiot at a time!

  5. The Following User Says Thank You to Junky For This Useful Post:

    compileDammit (October 9th, 2013)

  6. #5
    Junior Member
    Join Date
    Oct 2013
    Posts
    6
    My Mood
    Yeehaw
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Variables not initialized?

    IT WORKED! thanks for your help do you see any other issues or anything with my code?

  7. #6
    Member
    Join Date
    Sep 2013
    Posts
    70
    Thanks
    1
    Thanked 13 Times in 13 Posts

    Default Re: Variables not initialized?

    Have you tested your code beside compiling it? Just a few issues that you may have fixed already.

    For example in this code block:
            // What are the ints for? The are declared but initialization has not happened
            int add, subtract, multiply, divide;
     
            // In this line of code you ask the user to type in desired function (Strings) from the sounds of it but your program expects int. When the user types in add and it expects an int your application will complain and crash. You could rephrase and ask to type the number of function desired sort of a menu or you could also ask for a string but it will be slightly more involved. Not difficult just a little bit of learning.
            System.out.println("Please type in the desired function (no caps)-- add, subtract, multiply,"
                + "or divide: ");
            int e = scan.nextInt();
     
            // Same as first set of ints created. They are declared but not initialized.  Ints can only take whole numbers (1,2,3,4,5...etc)
            int Add, Subtract, Multiply, Divide;

    Another thing is how are you gonna let your program know what function to do?
    if (e == add){
       // code
    }
    else if (e == subtract){
      // code
    }
    //... etc

    What is the difference between add and subtract? In the upper portion of your program you assign both of them the same value. How will the application know the difference of an int that is 0 vs an int that is 0? In an if-else statement it will run the first condition that is true. So if they are both set to the same value it will always run add portion.

    Keep in mind when Declaring variables it is always
    Datatype variableName = some value;

    I think you are confusing how to use variables and are mistakenly using the name as a way of differentiating ("add" or "Add") which are actually Strings.

  8. The Following User Says Thank You to Ubiquitous For This Useful Post:

    compileDammit (October 11th, 2013)

Similar Threads

  1. a==b, b not initialized
    By wilford006 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 4th, 2013, 06:46 PM
  2. Differences between local variables and instance variables
    By rob17 in forum Java Theory & Questions
    Replies: 2
    Last Post: March 6th, 2012, 08:34 PM
  3. Instance Variables and local variables difference
    By dcwang3 in forum Java Theory & Questions
    Replies: 3
    Last Post: October 31st, 2011, 06:33 AM
  4. variable might not have been initialized
    By SV25 in forum Java Theory & Questions
    Replies: 1
    Last Post: April 25th, 2011, 10:58 AM
  5. Variable might not have been initialized?
    By n56 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 30th, 2010, 03:03 PM

Tags for this Thread