title: how to use if statement in main to Create an object with user input and to use a method on the objects
body: <p>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.</p>
  <p>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).</p>
  <p>the codes is (sub class fats)</p>
  <pre><code>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&lt;0&amp;&amp;amount&gt;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&lt;0&amp;&amp;sat&gt;100){
              this._amount=0;
              this._saturated=0;
              this._unsaturated=0;}
          else
              this._saturated=sat;
          if(unsat&lt;0&amp;&amp;unsat&gt;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&lt;0&amp;&amp;amount&gt;100){
              this._amount=(this._amount+0);}
          else
              this._amount=amount;}
  
      public void setSaturated(double sat){
          if(sat&lt;00&amp;&amp;sat&gt;100){
              this._saturated=(this._saturated+0);
          }
          else
              this._saturated=sat;
      }
  
      public void setUnsaturated(double unsat){
          if(unsat&lt;00&amp;&amp;unsat&gt;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&amp;&amp;this._saturated==otherFats._saturated&amp;&amp;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&lt;otherFats._amount){
              return true;}
          if(this._amount==otherFats._amount&amp;&amp;this._saturated&lt;otherFats._saturated){
              return true;}
              else
              if(this._amount==otherFats._amount&amp;&amp;this._saturated==otherFats._saturated&amp;&amp;this._saturated&lt;otherFats._saturated)
                  return true;
                  else
          
              return false;
          
      }
  
      public String toString(){
          return &quot;in 100 gr: &quot;+&quot;\n&quot;+&quot; &quot;+&quot; &quot;+&quot;Fat: -&quot;+&quot; &quot;+this._amount+&quot;\n&quot;+&quot; &quot;+&quot;Saturated Fat: –&quot;+&quot;  &quot;+this._saturated+&quot;\n&quot;+&quot; &quot;+&quot;Unsaturated Fat:-&quot;+&quot;  &quot;+this._unsaturated;
      }
  
          
          
          
  }
  </code></pre>
  <p>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)</p>
  <pre><code>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(&quot;Please enter the values of Fat, Saturated Fat and Unsaturated Fat of DarkChocolate Fat&quot;);
           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&gt;lowRange||x1&lt;=upRange||y1&gt;lowRange||y1&lt;=upRange||z1&gt;lowRange||z1&lt;=upRange){
               bitterChocolate = new Fats();
               bitterChocolate = new Fats(x1,y1,z1);
              System.out.println(&quot;one or more of the value you have entered is invalid&quot;);
          }else{ bitterChocolate = new Fats();};
  System.out.println(&quot;Please enter the values of Fat, Saturated Fat and Unsaturated Fat of milkChocolate Fat&quot;);
          x2=scan.nextInt();//darkChocolateFat
          y2=scan.nextDouble();//darkChocolatesat
          z2=scan.nextDouble();
   if(x2&lt;0||x2&gt;100||y2&lt;0||y2&gt;100||z2&lt;0||z2&gt;100){
               milkChocolate = new Fats();
              System.out.println(&quot;one or more of the value you have entered is invalid222&quot;);
          };
  System.out.println(&quot;Please enter the values of Fat, Saturated Fat and Unsaturated Fat of whiteChocolate Fat&quot;);
          x3=scan.nextInt();//darkChocolateFat
          y3=scan.nextDouble();//darkChocolatesat
          z3=scan.nextDouble();
  if(x3&lt;0||x3&gt;100||y3&lt;0||y3&gt;100||z3&lt;0||z3&gt;100){
               whiteChocolate = new Fats();
              System.out.println(&quot;one or more of the value you have entered is invalid222&quot;);
          };
  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);
  </code></pre>
  <p>I have tried just toString the objects that just made but it says:</p>
  <blockquote>
  <p>&quot;variable might not have been initialized&quot;</p>
  </blockquote>
  <p>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</p>