problem checking equality of user input and array data!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • AZRebelCowgirl73
    New Member
    • Nov 2006
    • 47

    #1

    problem checking equality of user input and array data!

    Here is my problem:

    I have two java files:

    One named Car.java and the other named CarDealerApp.ja va: In the CarDealerApp program, I read in through user input the make, model, year and price of a car and put them into an array called carShop[]. This part of the program is running fine. Every thing in the CarDealerApp.ja va file runs correctly except for the last loop. I need to read in through user input the make of a car and then search the array carShop[] for the make and subtract $1000 from the price listed for all instances of that particular make within the array. I cant seem to make the code work, the user input part does but the for loop does not! Can anyone help! Here is what I have so far: Thanks in advance!! ALI :P

    import java.util.*;
    import java.net.*;
    import java.lang.*;
    public class Car{

    public String make;
    public String model;
    public int year;
    public int price;

    //Car(); constructor with parameters
    public Car(String m, String d, int y, int p) {
    make = m;
    model = d;
    year = y;
    price = p;
    }//end constructor

    // return make of car (make accessor)
    public String getMake() {
    return make;
    }//end method

    //return model of car (model accessor)
    public String getModel() {
    return model;
    }//end method

    //return year of car (year accessor)
    public int getYear() {
    return year;
    }//end method

    //return price of car (price accessor)
    public int getPrice() {
    return price;
    }//end method

    //set new price of car (price mutator)
    public void setPrice(int iPrice) {
    price = iPrice;
    }//end method

    //print method
    public void print() {
    System.out.prin tln("Make = " + getMake() + "," + " Model = " + getModel() + "," + " Year = " + getYear() + "," + " Price = $" + getPrice());
    }//end method
    }

    import java.util.*;
    import java.net.*;
    import java.lang.*;

    public class CarDealerApp{

    public static void main(String[] args) {

    //declare array of Car objects. Array name is carShop
    final int MAX_SIZE = 100;
    Car carShop[] = new Car[MAX_SIZE];

    Scanner stdin = new Scanner(System. in);

    System.out.prin t("Please input the number of effective cars currently in the shop?: ");
    int numberOfCars = stdin.nextInt() ;

    for(int i = 0; (i < numberOfCars) && (i < MAX_SIZE); ++i) {
    System.out.prin tln();
    System.out.prin t("What is the make of your car? ");
    String m = stdin.next();
    System.out.prin t("What is the model of your car? ");
    String d = stdin.next();
    System.out.prin t("What is the Year of your car? ");
    int y = stdin.nextInt() ;
    System.out.prin t("What is the Price of your car? $");
    int p = stdin.nextInt() ;
    Car C = new Car(m, d, y, p);
    carShop[i] = C;
    }
    System.out.prin tln();
    System.out.prin tln(" The effective cars currently in the shop are: ");
    System.out.prin tln();
    for (int i = 0; (i < numberOfCars) && (i < MAX_SIZE); ++i) {
    carShop[i].print();
    }
    System.out.prin tln("The Price of all effective cars currently in the shop is: ");
    int prices[] = new int[numberOfCars];
    int dataSum = 0;
    for (int i = 0; (i < numberOfCars) && (i < MAX_SIZE); ++i) {
    prices[i]= carShop[i].getPrice();
    dataSum += prices[i];
    }
    System.out.prin tln(" Total Price = $" + dataSum);
    System.out.prin tln(" Search the shop for car below a certain price. ");
    System.out.prin tln();
    System.out.prin t("Please enter the price to search for: $ ");
    int searchPrice = stdin.nextInt() ;
    System.out.prin tln("All cars in the shop with a price of: $" + searchPrice + " or lower are: ");
    for (int i = 0; (i < numberOfCars) && (i < MAX_SIZE); ++i) {
    if (carShop[i].getPrice() <= searchPrice) {
    carShop[i].print();
    }
    }
    System.out.prin tln(" Search the shop for car of a certain Make ");
    System.out.prin tln(" and reduce their price by $1000. ");
    System.out.prin tln();
    System.out.prin t("Please enter the make to search for: ");
    String searchMake = stdin.next();
    System.out.prin tln();
    for (int i = 0; (i < numberOfCars) && (i < MAX_SIZE); ++i) {
    if (carShop[i].getMake() == searchMake) {
    int iPrice = carShop[i].getPrice() - 1000;
    carShop[i].setPrice(iPric e);
    carShop[i].print();


    }
    }
    }
    }
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    You can't compare Strings using the '==' operator; use the String.equals()
    method for that purpose.

    kind regards,

    Jos

    ps. and please use the [ code=java ] ... [ code ] tags (without the spaces)

    Comment

    • blazedaces
      Contributor
      • May 2007
      • 284

      #3
      Originally posted by AZRebelCowgirl7 3
      Here is my problem:

      I have two java files:

      One named Car.java and the other named CarDealerApp.ja va: In the CarDealerApp program, I read in through user input the make, model, year and price of a car and put them into an array called carShop[]. This part of the program is running fine. Every thing in the CarDealerApp.ja va file runs correctly except for the last loop. I need to read in through user input the make of a car and then search the array carShop[] for the make and subtract $1000 from the price listed for all instances of that particular make within the array. I cant seem to make the code work, the user input part does but the for loop does not! Can anyone help! Here is what I have so far: Thanks in advance!! ALI :P

      import java.util.*;
      import java.net.*;
      import java.lang.*;
      public class Car{

      public String make;
      public String model;
      public int year;
      public int price;

      //Car(); constructor with parameters
      public Car(String m, String d, int y, int p) {
      make = m;
      model = d;
      year = y;
      price = p;
      }//end constructor

      // return make of car (make accessor)
      public String getMake() {
      return make;
      }//end method

      //return model of car (model accessor)
      public String getModel() {
      return model;
      }//end method

      //return year of car (year accessor)
      public int getYear() {
      return year;
      }//end method

      //return price of car (price accessor)
      public int getPrice() {
      return price;
      }//end method

      //set new price of car (price mutator)
      public void setPrice(int iPrice) {
      price = iPrice;
      }//end method

      //print method
      public void print() {
      System.out.prin tln("Make = " + getMake() + "," + " Model = " + getModel() + "," + " Year = " + getYear() + "," + " Price = $" + getPrice());
      }//end method
      }

      import java.util.*;
      import java.net.*;
      import java.lang.*;

      public class CarDealerApp{

      public static void main(String[] args) {

      //declare array of Car objects. Array name is carShop
      final int MAX_SIZE = 100;
      Car carShop[] = new Car[MAX_SIZE];

      Scanner stdin = new Scanner(System. in);

      System.out.prin t("Please input the number of effective cars currently in the shop?: ");
      int numberOfCars = stdin.nextInt() ;

      for(int i = 0; (i < numberOfCars) && (i < MAX_SIZE); ++i) {
      System.out.prin tln();
      System.out.prin t("What is the make of your car? ");
      String m = stdin.next();
      System.out.prin t("What is the model of your car? ");
      String d = stdin.next();
      System.out.prin t("What is the Year of your car? ");
      int y = stdin.nextInt() ;
      System.out.prin t("What is the Price of your car? $");
      int p = stdin.nextInt() ;
      Car C = new Car(m, d, y, p);
      carShop[i] = C;
      }
      System.out.prin tln();
      System.out.prin tln(" The effective cars currently in the shop are: ");
      System.out.prin tln();
      for (int i = 0; (i < numberOfCars) && (i < MAX_SIZE); ++i) {
      carShop[i].print();
      }
      System.out.prin tln("The Price of all effective cars currently in the shop is: ");
      int prices[] = new int[numberOfCars];
      int dataSum = 0;
      for (int i = 0; (i < numberOfCars) && (i < MAX_SIZE); ++i) {
      prices[i]= carShop[i].getPrice();
      dataSum += prices[i];
      }
      System.out.prin tln(" Total Price = $" + dataSum);
      System.out.prin tln(" Search the shop for car below a certain price. ");
      System.out.prin tln();
      System.out.prin t("Please enter the price to search for: $ ");
      int searchPrice = stdin.nextInt() ;
      System.out.prin tln("All cars in the shop with a price of: $" + searchPrice + " or lower are: ");
      for (int i = 0; (i < numberOfCars) && (i < MAX_SIZE); ++i) {
      if (carShop[i].getPrice() <= searchPrice) {
      carShop[i].print();
      }
      }
      System.out.prin tln(" Search the shop for car of a certain Make ");
      System.out.prin tln(" and reduce their price by $1000. ");
      System.out.prin tln();
      System.out.prin t("Please enter the make to search for: ");
      String searchMake = stdin.next();
      System.out.prin tln();
      for (int i = 0; (i < numberOfCars) && (i < MAX_SIZE); ++i) {
      if (carShop[i].getMake() == searchMake) {
      int iPrice = carShop[i].getPrice() - 1000;
      carShop[i].setPrice(iPric e);
      carShop[i].print();


      }
      }
      }
      }
      First of all... I'm not so sure why you started a new thread about this... but that's fine I guess...

      Now listen, I can't tell from looking at that what is wrong... I'm sorry.

      But, there are ways to debug this and gain more knowledge about what is going on when you do this part... so try changing your bold code to this, process it and post here what you get for an output so we can interpret why it's not working (oh and, use code brackets next time please):

      Code:
      [B]System.out.println(" Search the shop for car of a certain Make ");
      System.out.println(" and reduce their price by $1000. ");
      System.out.println();
      System.out.print("Please enter the make to search for: ");
      String searchMake = stdin.next();
      System.out.println();
      for (int i = 0; (i < numberOfCars) && (i < MAX_SIZE); ++i) {
      System.out.println("\nDoes " + carShop[i].getMake() + " == " + searchMake + " ?\n");
      if (carShop[i].getMake() == searchMake) {
      int iPrice = carShop[i].getPrice() - 1000;
      System.out.println("\niPrice is " + iPrice + "\n")
      carShop[i].setPrice(iPrice); 
      carShop[i].print();[/B]
      Please the entire output. If everything looks as expected, then the problem is more likely in setPrice. That tells you something. But if the error is here, this will tell you why there is an error.

      Hope it helps,
      -blazed

      P.S. Jos posted while I was posting, so I didn't get to see his answer, and well, he was right, so you don't have to listen to the post above me, though I still think if you can't get an answer as to why your program doesn't work debugging using this method is very useful and it would have told you that "==" wasn't working out, then you could have asked, "Why doesn't == work with these two things?"... ya, well, hope it's all good now...

      Comment

      • AZRebelCowgirl73
        New Member
        • Nov 2006
        • 47

        #4
        Code:
        [B]System.out.println(" Search the shop for car of a certain Make ");
        System.out.println(" and reduce their price by $1000. ");
        System.out.println();
        System.out.print("Please enter the make to search for: ");
        String searchMake = stdin.next();
        System.out.println();
        for (int i = 0; (i < numberOfCars) && (i < MAX_SIZE); ++i) {
        System.out.println("\nDoes " + carShop[i].getMake() + " == " + searchMake + " ?\n");
        if (carShop[i].getMake() == searchMake) {
        int iPrice = carShop[i].getPrice() - 1000;
        System.out.println("\niPrice is " + iPrice + "\n")
        carShop[i].setPrice(iPrice); 
        carShop[i].print();[/B]
        Please the entire output. If everything looks as expected, then the problem is more likely in setPrice. That tells you something. But if the error is here, this will tell you why there is an error.

        Hope it helps,
        -blazed

        I replaced the code with what you suggested, and I get the following to print for three cars in the carShop array:

        Please enter the make to search for: toyota

        Does toyota == toyota ?

        Does mazda == toyota ?

        Does Toyota == toyota ?

        AT THIS POINT THE PROGRAM STOPS! ANY IDEAS :P

        Comment

        • blazedaces
          Contributor
          • May 2007
          • 284

          #5
          Originally posted by AZRebelCowgirl7 3
          Code:
          [B]System.out.println(" Search the shop for car of a certain Make ");
          System.out.println(" and reduce their price by $1000. ");
          System.out.println();
          System.out.print("Please enter the make to search for: ");
          String searchMake = stdin.next();
          System.out.println();
          for (int i = 0; (i < numberOfCars) && (i < MAX_SIZE); ++i) {
          System.out.println("\nDoes " + carShop[i].getMake() + " == " + searchMake + " ?\n");
          if (carShop[i].getMake() == searchMake) {
          int iPrice = carShop[i].getPrice() - 1000;
          System.out.println("\niPrice is " + iPrice + "\n")
          carShop[i].setPrice(iPrice); 
          carShop[i].print();[/B]
          Please the entire output. If everything looks as expected, then the problem is more likely in setPrice. That tells you something. But if the error is here, this will tell you why there is an error.

          Hope it helps,
          -blazed

          I replaced the code with what you suggested, and I get the following to print for three cars in the carShop array:

          Please enter the make to search for: toyota

          Does toyota == toyota ?

          Does mazda == toyota ?

          Does Toyota == toyota ?

          AT THIS POINT THE PROGRAM STOPS! ANY IDEAS :P
          Please from now on make sure to read all people's comments, not just mine and make sure if you're going to read someone's comments like mine, read ALL of it.

          As Jos said and I pointed out how correct he was, you need to change your if-statement from using the "==" to using the Object's ".equals" method.

          Also, from your program, when you saw that it printed "toyota == toyota", but DID NOT go into the if-statement shouldn't you have thought, hey, why doesn't it go inside the if-statement? I'm supposed to print something from there, but it never went in...

          Hope that was helpful,

          -blazed

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            See my reply #2.

            kind regards,

            Jos

            Comment

            • AZRebelCowgirl73
              New Member
              • Nov 2006
              • 47

              #7
              You guys are so great thanks so much for all the help EVERYONE! ALI :P

              Comment

              Working...