Brandon
Brandon

Reputation: 1

bad operand types for binary operator error '-' java

I don't know how to fix my error. The error states

"DayCare.java:29: error: bad operand types for binary operator '-' [numDaysString - 1]) first type: String second type: int"

My code is


public class DayCare
{
   public static void main(String args[]) 
   {
      // Declare two-dimensional array here.
      double weeklyRate[][] =
   {{30.00, 60.00, 88.00, 115.00, 140.00},
      {26.00, 52.00, 70.00, 96.00, 120.00},
    {24.00, 46.00, 67.00, 89.00, 110.00 }, 
    {22.00, 40.00, 60.00, 75.00, 88.00},
     {20.00, 35.00, 50.00, 66.00, 84.00}};
      // Declare other variables.
      int numDays;   
      int age;
      String numDaysString;
      String ageString;
      int QUIT = 99;
      Scanner input = new Scanner(System.in);

      while(age != QUIT)
      {  System.out.println("Enter number of days: ");
       numDaysString = input.nextLine(); numDays = Integer.parseInt(numDaysString);
        if(age >= 4) age = 4; System.out.println("Weekly charge is $" + weeklyRate[age]
         [numDaysString - 1]); 
        System.out.println("Enter the age of the child or 99 to quit: "); } 
         System.out.println("End of program"); System.exit(0); }  
      }

Upvotes: 0

Views: 1798

Answers (1)

Rendón
Rendón

Reputation: 300

The type for numDaysString is a String and also you need to initialize the age to any value

import java.util.Scanner;

public class Split {

    public static void main(String args[]) {
        // Declare two-dimensional array here.
        double weeklyRate[][] = { { 30.00, 60.00, 88.00, 115.00, 140.00 }, { 26.00, 52.00, 70.00, 96.00, 120.00 },
                { 24.00, 46.00, 67.00, 89.00, 110.00 }, { 22.00, 40.00, 60.00, 75.00, 88.00 },
                { 20.00, 35.00, 50.00, 66.00, 84.00 } };
        // Declare other variables.
        int numDays;
        int age = 0;
        String numDaysString;
        String ageString;
        int QUIT = 99;
        Scanner input = new Scanner(System.in);

        while (age != QUIT) {
            System.out.println("Enter number of days: ");
            numDaysString = input.nextLine();
            numDays = Integer.parseInt(numDaysString);
            if (age >= 4)
                age = 4;
            System.out.println("Weekly charge is $" + weeklyRate[age][numDays - 1]);
            System.out.println("Enter the age of the child or 99 to quit: ");
        }
        System.out.println("End of program");
        System.exit(0);
    }

}

Upvotes: 0

Related Questions