0

So i try to return value of days depending on data input of month that user will enter .I am also using array of object.The problem is the value it return is always 0 since it always print 0 .I don't know where i do wrong .This is my coding . I am newbie and sorry if i don't explain my problem good enough.

import java.util.Scanner;
import java.math.RoundingMode;
import java.text.DecimalFormat;
class BudgetTracker{
  private static DecimalFormat df2 = new DecimalFormat("#.##");

  public static void main(String []args){
  Scanner sc = new Scanner (System.in);

  df2.setRoundingMode(RoundingMode.UP);

  System.out.println(" ----Welcome to EZ-Budgets ---- " );

   System.out.print("How many months you wish to insert data: ");
   int size = Integer.parseInt(sc.nextLine());

  Income incObject[] = new Income[size] ;
   Budget budObject[] = new Budget[size];
   for(int box=0; box<size; box++){

   System.out.print("\n"+"Enter month : ");
   String mon = sc.nextLine();
   System.out.print("Enter year : ");
   int ye = sc.nextInt();
   sc.nextLine();
   System.out.print("Enter income for the month :RM ");
   double in = sc.nextDouble();
   sc.nextLine();
   System.out.print("Enter budget for the month :RM ");
   double budg = sc.nextDouble();
   sc.nextLine();
   int[] cate = new int[3];
   System.out.print("Enter amount of money you spend on food :RM ");
   cate [0] = Integer.parseInt(sc.nextLine());
   System.out.print ("Enter amount of money you spend on university stuffs this month :RM ");
   cate[1] = Integer.parseInt(sc.nextLine());
   System.out.print("Enter amount of money you spend on other stuff :RM ");
   cate[2] =Integer.parseInt(sc.nextLine());

   incObject[box] = new Income(mon,ye,in);
   budObject[box] = new Budget(budg,cate,in);

 }//for loop size
  for(int box = 0 ; box <size ;box++){
  System.out.print("\n");
  System.out.print("RM :" + budObject[box].getBudget());


}//loop1
for(int box = 0 ; box <size ;box++){
  System.out.print("\n");
  System.out.print("Saving RM :" + budObject[box].getSaving());


}//loop2
 for(int box = 0 ; box <size ;box++){
   System.out.print("\n");
   System.out.print( incObject[box].getDay());


}//loop3
for(int box = 0 ; box <size ;box++){ // the part i have problem
  System.out.print("\n");
  System.out.print("Testing decimal format saving : RM" + df2.format(budObject[box].getSaving()));


}//loop2
}//main

 }//class

class Income{
private String month;
private int year;
private double income;



public Income( String mon ,int ye ,double in )
{
    month = mon;
    year = ye;
    income = in ;



}//income

  public String getMonth(){return month;}
  public double getIncome(){return income;}
  public int getYear(){return year;}

  public int getDay(){
       int day = 0;

  if(month == "January"){

          day = 31;
          return day;
      }
        if((month == "February")&(year%4==0)&(year%100!=0)){
          day = 29;
          return day;
          //leap year
        }
        if((month == "February")&(year%4!=0)){
          day = 28;
          return day;
          //not leap year
        }
        if (month == "March"){
            day = 31;
            return day;
        }
        if(month == "April"){
            day =30 ;
            return day;
        }
        if(month == "May"){
            day = 31 ;
            return day;
        }

    return day;
  }//get day


   public void tot(){
       double totAmount = income + 100;
       System.out.print(totAmount);


}
}//class

enter image description here

1 Answers1

1

The problem with your code comes from the string comparisons. In order to compare string in Java you have to use the .equals() method.

An example of the usage of .equals():

String str1 = "string one";

if(str1.equals("string one"))
{
   System.out.println("Correct way to compare strings");
}
halfer
  • 19,824
  • 17
  • 99
  • 186
bhristov
  • 3,137
  • 2
  • 10
  • 26