-3

I have two classes: Customer and MovieSeating. (the problem condition is in the assignCustomerAt method in MovieSeating)

public class Customer
{
    private String lastName;
    private String firstName;

    // This constructor sets the first name and last name to "???"
    public Customer(){
          lastName = "???";
          firstName = "???";
    }
      // This method returns a string containing a customer's initials
      // (first characters of firstName and lastName.)
      public String toString()
      {
           String result = firstName.charAt(0) + "." + lastName.charAt(0) + ".";
           return result;
      }        
}

So something.toString(); returns "?.?."

Right?

All is well BUT then there's this.

public class MovieSeating{
public Customer[][] seating;
public int rowNumber;
public int colNumber;

public MovieSeating(int rowNum, int columnNum){
    rowNumber= rowNum;
    colNumber=columnNum;
    Customer thisGuy = new Customer();
    Customer[][] seaTing = new Customer[rowNum][columnNum];
    for(int i = 0; i<rowNum;i++){
        for(int j = 0; j<columnNum; j++){
            seaTing[i][j] = thisGuy;
        }
    }
    seating = seaTing;
}



public boolean assignCustomerAt(int row, int col, Customer tempCustomer){ 
    String a = seating[row][col].toString();
            if(a=="?.?."){     //THE CONDITION IN THIS IF STATEMENT IS THE PROBLEM
                 seating[row][col] = tempCustomer;
                 return true;
            }
            return false;
}

The purpose of the last method is to assign someone to a spot if and only if the seat is not taken (as noted by the customer having the initials "?.?."). But regardless, when the value of String a is "?.?.", the condition returns false. I've tried and tested to see that seating[row][col].toString(); returns "?.?." and it sure does. I've put parenthesis around each item and tried making two different strings and this and that and nothing works! It's like saying that (1*1) != 1. What's the deal???? Thanks in advance

Taylor
  • 23
  • 1
  • 3

2 Answers2

0

String cannot be compared using == as you are comparing Object reference instead of object information.

you have to change to

a.equals("?.?."){
RamonBoza
  • 8,898
  • 6
  • 36
  • 48
0

if(a=="?.?."){ should be if("?.?.".equals(a)) {

M. Abbas
  • 6,409
  • 4
  • 33
  • 46