'how to use if statement in main to Create an object with user input and to use a method on the objects

I'm working on an exercise, where I need to create a subclass that has 2 constructors (1 is empty and sets the values as 0 the other one is getting the values. and in the subclass I have an equals method and a betterfat method that checks 2 objects and tell wihch one is better.

My problem is in the main class (I have to get 3 values of 3 chocolates from the user, check the numbers and choose which constructor to use but my problem is when I am putting the if statement and inside it the choose of the constructor after these blocks I have to use a method on this objects my problem is that it says that can't find variable (it seems that it is because of the ifstatment).

the codes is (sub class fats)

public class Fats{

    private int _amount;
    private double _saturated;
    private double _unsaturated;
    public Fats(){//constractor thats sets all the values as 0
        this._amount=0;
        this._saturated=0;
        this._unsaturated=0;
    }

    /*constractor that build an object , if one of the
     *values not between 0-100 it sets all the values to 0.
     **/
    public Fats(int amount, double sat, double unsat){
        if(amount<0&&amount>100){//using if to check if the value is between 0-100
            this._amount=0;
            this._saturated=0;
            this._unsaturated=0;}
        else
            this._amount=amount;
        if(sat<0&&sat>100){
            this._amount=0;
            this._saturated=0;
            this._unsaturated=0;}
        else
            this._saturated=sat;
        if(unsat<0&&unsat>100){
            this._amount=0;
            this._saturated=0;
            this._unsaturated=0;}
        else
            this._unsaturated=unsat;}
    //getters
    public int getAmount(){
        return _amount;
    }

    public double getSaturated(){
        return _saturated;
    }

    public double getUnsaturated(){
        return _unsaturated;
    }
    //setters
    public void setAmount(int amount){
        if(amount<0&&amount>100){
            this._amount=(this._amount+0);}
        else
            this._amount=amount;}

    public void setSaturated(double sat){
        if(sat<00&&sat>100){
            this._saturated=(this._saturated+0);
        }
        else
            this._saturated=sat;
    }

    public void setUnsaturated(double unsat){
        if(unsat<00&&unsat>100){
            this._unsaturated=(this._unsaturated+0);
        }
        else
            this._unsaturated=unsat;
    }
    //equal method 
    public boolean equals (Fats other){
        if(other==this)//checking if other is me
            return true;
        if(!(other instanceof Fats))//if the other not a Fats
            return false;
        Fats otherFats = (Fats)other;//casting other to Fats object to check if the values are equals
        if(this._amount==otherFats._amount&&this._saturated==otherFats._saturated&&this._unsaturated==otherFats._unsaturated)
        //checking each calue of fats if its equals to other values
            return true;
        else
            return false;
    }

    public boolean betterFat (Fats other){
        Fats otherFats = (Fats)other;//casting the other object to be an Fats class object
        if(this._amount<otherFats._amount){
            return true;}
        if(this._amount==otherFats._amount&&this._saturated<otherFats._saturated){
            return true;}
            else
            if(this._amount==otherFats._amount&&this._saturated==otherFats._saturated&&this._saturated<otherFats._saturated)
                return true;
                else
        
            return false;
        
    }

    public String toString(){
        return "in 100 gr: "+"\n"+" "+" "+"Fat: -"+" "+this._amount+"\n"+" "+"Saturated Fat: –"+"  "+this._saturated+"\n"+" "+"Unsaturated Fat:-"+"  "+this._unsaturated;
    }

        
        
        
}

and this is the main class (in the main I have to get 3 values and choose which constructor to use and I have to do it 3 times and then use method to see which one is better and use toString to show it to the user)

import java.util.Scanner;
public class FatDriver{
    public static void main(String[]args){
        Fats num1,num2,num3;
        Fats F1,F2,F3;//,j1,j2,j3;//,darkChocolateFat,milkChocolateFat,whiteChocolateFat;
        int x1,x2,x3,e1;//darkChocolateFat,milkChocolateFat,whiteChocolateFat;
        double y1,y2,y3,e2,e3;//darkChocolatesat,milkChocolatesat,whiteChocolatesat;
        double z1,z2,z3; //darkChocolateunsat,milkChocolateunsat,whiteChocolateunsat;
        Scanner scan= new Scanner(System.in);//using scaner for user input
        System.out.println("Please enter the values of Fat, Saturated Fat and Unsaturated Fat of DarkChocolate Fat");
         Fats whiteChocolate;
           Fats bitterChocolate;
        Fats milkChocolate; 
        x1=scan.nextInt();//darkChocolateFat
        y1=scan.nextDouble();//darkChocolatesat
        z1=scan.nextDouble();//darkChocolateunsat
        double r2,r3;
        
        int r1;
        boolean yes,no;
        //yes=true;
        //no=false;
        final double upRange =100;
        final double lowRange =0;
       // z1=scan.nextDouble();//darkChocolateunsat
        
        if(x1>lowRange||x1<=upRange||y1>lowRange||y1<=upRange||z1>lowRange||z1<=upRange){
             bitterChocolate = new Fats();
             bitterChocolate = new Fats(x1,y1,z1);
            System.out.println("one or more of the value you have entered is invalid");
        }else{ bitterChocolate = new Fats();};
System.out.println("Please enter the values of Fat, Saturated Fat and Unsaturated Fat of milkChocolate Fat");
        x2=scan.nextInt();//darkChocolateFat
        y2=scan.nextDouble();//darkChocolatesat
        z2=scan.nextDouble();
 if(x2<0||x2>100||y2<0||y2>100||z2<0||z2>100){
             milkChocolate = new Fats();
            System.out.println("one or more of the value you have entered is invalid222");
        };
System.out.println("Please enter the values of Fat, Saturated Fat and Unsaturated Fat of whiteChocolate Fat");
        x3=scan.nextInt();//darkChocolateFat
        y3=scan.nextDouble();//darkChocolatesat
        z3=scan.nextDouble();
if(x3<0||x3>100||y3<0||y3>100||z3<0||z3>100){
             whiteChocolate = new Fats();
            System.out.println("one or more of the value you have entered is invalid222");
        };
System.out.println(whiteChocolate);// this is just to check if it is works (i have tochoose the best of 3 using my method)
            System.out.println(bitterChocolate);
            System.out.println(milkChocolate);

I have tried just toString the objects that just made but it says:

"variable might not have been initialized"

because of this I can't work with the objects after getting the user input I might miss something and that's why I am asking for help



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source