By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,967 Members | 843 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,967 IT Pros & Developers. It's quick & easy.

problem checking equality of user input and array data!

P: 47
Here is my problem:

I have two java files:

One named Car.java and the other named CarDealerApp.java: 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.java 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.println("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.print("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.println();
System.out.print("What is the make of your car? ");
String m = stdin.next();
System.out.print("What is the model of your car? ");
String d = stdin.next();
System.out.print("What is the Year of your car? ");
int y = stdin.nextInt();
System.out.print("What is the Price of your car? $");
int p = stdin.nextInt();
Car C = new Car(m, d, y, p);
carShop[i] = C;
}
System.out.println();
System.out.println(" The effective cars currently in the shop are: ");
System.out.println();
for (int i = 0; (i < numberOfCars) && (i < MAX_SIZE); ++i) {
carShop[i].print();
}
System.out.println("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.println(" Total Price = $" + dataSum);
System.out.println(" Search the shop for car below a certain price. ");
System.out.println();
System.out.print("Please enter the price to search for: $ ");
int searchPrice = stdin.nextInt();
System.out.println("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.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) {
if (carShop[i].getMake() == searchMake) {
int iPrice = carShop[i].getPrice() - 1000;
carShop[i].setPrice(iPrice);
carShop[i].print();


}
}
}
}
Jun 1 '07 #1
Share this Question
Share on Google+
6 Replies


Expert 10K+
P: 11,448
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)
Jun 1 '07 #2

blazedaces
100+
P: 284
Here is my problem:

I have two java files:

One named Car.java and the other named CarDealerApp.java: 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.java 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.println("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.print("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.println();
System.out.print("What is the make of your car? ");
String m = stdin.next();
System.out.print("What is the model of your car? ");
String d = stdin.next();
System.out.print("What is the Year of your car? ");
int y = stdin.nextInt();
System.out.print("What is the Price of your car? $");
int p = stdin.nextInt();
Car C = new Car(m, d, y, p);
carShop[i] = C;
}
System.out.println();
System.out.println(" The effective cars currently in the shop are: ");
System.out.println();
for (int i = 0; (i < numberOfCars) && (i < MAX_SIZE); ++i) {
carShop[i].print();
}
System.out.println("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.println(" Total Price = $" + dataSum);
System.out.println(" Search the shop for car below a certain price. ");
System.out.println();
System.out.print("Please enter the price to search for: $ ");
int searchPrice = stdin.nextInt();
System.out.println("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.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) {
if (carShop[i].getMake() == searchMake) {
int iPrice = carShop[i].getPrice() - 1000;
carShop[i].setPrice(iPrice);
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):

Expand|Select|Wrap|Line Numbers
  1. System.out.println(" Search the shop for car of a certain Make ");
  2. System.out.println(" and reduce their price by $1000. ");
  3. System.out.println();
  4. System.out.print("Please enter the make to search for: ");
  5. String searchMake = stdin.next();
  6. System.out.println();
  7. for (int i = 0; (i < numberOfCars) && (i < MAX_SIZE); ++i) {
  8. System.out.println("\nDoes " + carShop[i].getMake() + " == " + searchMake + " ?\n");
  9. if (carShop[i].getMake() == searchMake) {
  10. int iPrice = carShop[i].getPrice() - 1000;
  11. System.out.println("\niPrice is " + iPrice + "\n")
  12. carShop[i].setPrice(iPrice); 
  13. carShop[i].print();
  14.  
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...
Jun 1 '07 #3

P: 47
Expand|Select|Wrap|Line Numbers
  1. System.out.println(" Search the shop for car of a certain Make ");
  2. System.out.println(" and reduce their price by $1000. ");
  3. System.out.println();
  4. System.out.print("Please enter the make to search for: ");
  5. String searchMake = stdin.next();
  6. System.out.println();
  7. for (int i = 0; (i < numberOfCars) && (i < MAX_SIZE); ++i) {
  8. System.out.println("\nDoes " + carShop[i].getMake() + " == " + searchMake + " ?\n");
  9. if (carShop[i].getMake() == searchMake) {
  10. int iPrice = carShop[i].getPrice() - 1000;
  11. System.out.println("\niPrice is " + iPrice + "\n")
  12. carShop[i].setPrice(iPrice); 
  13. carShop[i].print();
  14.  
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
Jun 4 '07 #4

blazedaces
100+
P: 284
Expand|Select|Wrap|Line Numbers
  1. System.out.println(" Search the shop for car of a certain Make ");
  2. System.out.println(" and reduce their price by $1000. ");
  3. System.out.println();
  4. System.out.print("Please enter the make to search for: ");
  5. String searchMake = stdin.next();
  6. System.out.println();
  7. for (int i = 0; (i < numberOfCars) && (i < MAX_SIZE); ++i) {
  8. System.out.println("\nDoes " + carShop[i].getMake() + " == " + searchMake + " ?\n");
  9. if (carShop[i].getMake() == searchMake) {
  10. int iPrice = carShop[i].getPrice() - 1000;
  11. System.out.println("\niPrice is " + iPrice + "\n")
  12. carShop[i].setPrice(iPrice); 
  13. carShop[i].print();
  14.  
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
Jun 4 '07 #5

Expert 10K+
P: 11,448
See my reply #2.

kind regards,

Jos
Jun 4 '07 #6

P: 47
You guys are so great thanks so much for all the help EVERYONE! ALI :P
Jul 2 '07 #7

Post your reply

Sign in to post your reply or Sign up for a free account.