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

Thread: Vari not initiliazed?

  1. #1
    Member
    Join Date
    Sep 2012
    Posts
    98
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Vari not initiliazed?

    calculateAnswer (circRadi, triBase, lengthRect, widthRect, shapeNum, triHeight); This line, all but shapeNum say may not be initialized, im confused on what to do, they're all declared by user input.

    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
     
    /**
     *
     * @author Logan
     */
    import java.util.Scanner;
    public class area
    {
     
     
        public static void main(String[] args) 
        {
            //Declaring Variables.
            int triHeight, triBase, lengthRect, widthRect, shapeNum;
            double circRadi;
     
            Scanner reader = new Scanner (System.in);
     
            //Receiving Input
            System.out.println ("Which shape do you want area for?");
            System.out.println("1 Triangle 2 Circle 3 Rectangle 0 None");
            shapeNum = reader.nextInt ();
            if (shapeNum == 1) {
                System.out.println("Base of triangle?");
                triBase = reader.nextInt ();
                System.out.println("Height of triangle");
                triHeight = reader.nextInt ();
     
             }
           else if (shapeNum == 2) {
                System.out.println("Radius of circle?");
                circRadi = reader.nextDouble ();
            }
           else if (shapeNum == 3) {
                System.out.println ("Length of rectangle?");
                lengthRect = reader.nextInt ();
                System.out.println("Width of rectangle?");
                widthRect = reader.nextInt ();                               
            }
     
        calculateAnswer (circRadi, triBase, lengthRect, widthRect, shapeNum, triHeight);
     
        } 
     
        public static void calculateAnswer(double circRadi, int triHeight, int triBase, int lengthRect, int widthRect, int shapeNum){
     
     
        }
     
        public static void areaTriangle(int triBase,int triHeight, int triOutput) {
            triOutput = (triBase*triHeight)/2;
     
        }
     
        public static void areaCircle (double circOutput,int circRadi ) {
            circOutput = (circRadi*Math.PI);
        }
     
        public static void areaRectangle(int rectOutput, int lengthRect, int widthRect) {
            rectOutput = lengthRect*widthRect;   
     
     
        }
     
     
     
     
    }


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Vari not initiliazed?

    So why not give them initial values when you declare them? Also you'll want to declare each variable on its own line. Code real estate is free, so I suggest that you not be afraid to use it judiciously when it will help clarify your code.

  3. #3
    Member
    Join Date
    Sep 2012
    Posts
    98
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Vari not initiliazed?

    Because it will be changed later in the code? Should I just set them to 1?

  4. #4
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Vari not initiliazed?

    You need to either 1) set local variables (variables declared in methods, constructors or other similar blocks) to something as the compiler will not allow you to use uninitialized local variables, or 2) you need to change them to class fields in which case the compiler will automatically initialize the variables to their default values, such as 0 for ints, false for booleans, and null for reference variables.

    What initial variable you use is up to you as long as it makes sense for your program.

  5. #5
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Vari not initiliazed?

    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
     
    /**
     *
     * @author Logan
     */
    import java.util.Scanner;
    public class area
    {
     
     
        public static void main(String[] args) 
        {
            //Declaring Variables.
            int triHeight, triBase, lengthRect, widthRect, shapeNum;
            double circRadi;
            //At this point in the code, the variables have not been initialized **
     
            Scanner reader = new Scanner (System.in);
     
            //Receiving Input
            System.out.println ("Which shape do you want area for?");
            System.out.println("1 Triangle 2 Circle 3 Rectangle 0 None");
            shapeNum = reader.nextInt ();
            //Pretend user enters 4 for shapeNum... This is the scenario the compiler is upset over (to put it short)
            if (shapeNum == 1) {// 4 != 1 **
                System.out.println("Base of triangle?");
                triBase = reader.nextInt ();
                System.out.println("Height of triangle");
                triHeight = reader.nextInt ();
     
             }
           else if (shapeNum == 2) {//4 != 2 **
                System.out.println("Radius of circle?");
                circRadi = reader.nextDouble ();
            }
           else if (shapeNum == 3) {//4 != 3 **
                System.out.println ("Length of rectangle?");
                lengthRect = reader.nextInt ();
                System.out.println("Width of rectangle?");
                widthRect = reader.nextInt ();                               
            }
           //At this point the variables still have not been initialized,
          //even if the user enters 0,1,2,or3, some of the variables were not initialized, and you are trying to use them all
        calculateAnswer (circRadi, triBase, lengthRect, widthRect, shapeNum, triHeight);
     
        } 
     
        public static void calculateAnswer(double circRadi, int triHeight, int triBase, int lengthRect, int widthRect, int shapeNum){
        //The compiler has seen that there is a path through the code where some the variables have not been initialized by this point
     
        }
     
        public static void areaTriangle(int triBase,int triHeight, int triOutput) {
            triOutput = (triBase*triHeight)/2;
     
        }
     
        public static void areaCircle (double circOutput,int circRadi ) {
            circOutput = (circRadi*Math.PI);
        }
     
        public static void areaRectangle(int rectOutput, int lengthRect, int widthRect) {
            rectOutput = lengthRect*widthRect;   
     
     
        }
     
     
     
     
    }
    Keep in mind that even one variable has the chance of dropping through without being initialized in some way will prevent the code from compiling.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Vari not initiliazed?

    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Sep 2012
    Posts
    98
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Vari not initiliazed?

    You told me If I had a different question to make another thread. I currently have this program all finished except when it calculates the information, it will output the answer along with 2 other numbers, for example.
    run:
    Which shape do you want area for?
    1 Triangle 2 Circle 3 Rectangle 0 None
    3
    Length of rectangle?
    5
    Width of rectangle?
    10
    0
    3.141592653589793
    50
    BUILD SUCCESSFUL (total time: 5 seconds)

    This is my actual code.
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
     
    /**
     *
     * @author Logan
     */
    import java.util.Scanner;
    public class Area {
     
     
        public static void main(String[] args) 
        {
            //Declaring Variables.
            int triHeight;
            int triBase;
            int lengthRect;
            int widthRect ;
            int shapeNum;
            int triOutput;
            int rectOutput;
            double circRadi;
            double circOutput;
     
            triHeight = 1;
            triBase = 1;
            lengthRect = 1;
            widthRect = 1;
            triOutput = 1;
            circRadi = 1;
            circOutput = 1;
            rectOutput = 1;
     
            Scanner reader = new Scanner (System.in);
     
            //Receiving Input
            System.out.println ("Which shape do you want area for?");
            System.out.println("1 Triangle 2 Circle 3 Rectangle 0 None");
            shapeNum = reader.nextInt ();
            if (shapeNum == 1) {
                System.out.println("Base of triangle?");
                triBase = reader.nextInt ();
                System.out.println("Height of triangle");
                triHeight = reader.nextInt ();           
             }
            if (shapeNum == 2) {
                System.out.println("Radius of circle?");
                circRadi = reader.nextDouble ();
            }
            if (shapeNum == 3) {
                System.out.println ("Length of rectangle?");
                lengthRect = reader.nextInt ();
                System.out.println("Width of rectangle?");
                widthRect = reader.nextInt ();                   
            }
     
         areaTriangle (triHeight, triBase, triOutput);  
         areaCircle (circOutput, circRadi);
         areaRectangle (rectOutput, lengthRect, widthRect);
     
     
        }
        public static void areaTriangle(int triBase,int triHeight, int triOutput) {
            triOutput = (triBase*triHeight)/2;
            System.out.println(triOutput);
        }
     
        public static void areaCircle (double circOutput,double circRadi ) {
            circOutput = (Math.pow (circRadi, 2)*Math.PI);
            System.out.println(circOutput);
        }
     
        public static void areaRectangle(int rectOutput, int lengthRect, int widthRect) {
            rectOutput = lengthRect*widthRect;
            System.out.println(rectOutput);
        }
    }

    What am I doing wrong

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Vari not initiliazed?

    What am I doing wrong
    Please explain what the problem is? What are you expecting the program to do? Does it do it?

    If you get any error messages, copy and paste them here.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Member
    Join Date
    Sep 2012
    Posts
    98
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Vari not initiliazed?

    In my latest post, i said at the top
    I currently have this program all finished except when it calculates the information, it will output the answer along with 2 other numbers, for example.
    [Code]run:
    Which shape do you want area for?
    1 Triangle 2 Circle 3 Rectangle 0 None
    3
    Length of rectangle?
    5
    Width of rectangle?
    10
    0
    3.141592653589793
    50
    BUILD SUCCESSFUL (total time: 5 seconds)
    [Code/]

    It will give me the answer of 50, but the answers 3.14 and 0 are irrelevant.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Vari not initiliazed?

    Can you explain what the problem is? Does the program generate the desired output?
    If not, please show what you want the output to be.

    the answers 3.14 and 0 are irrelevant.
    What does that mean? If you don't want those numbers printed change the code so it does not print them.

    When printing out numbers, it makes the output easier to understand if you include a String as an identifier. Something like:
    System.out.println ("the name="+theVar);
    Replace "the name" with a description of the value that is in theVar.
    Last edited by Norm; September 23rd, 2012 at 01:14 PM.
    If you don't understand my answer, don't ignore it, ask a question.