English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

if (choice == 'n'){

response = JOptionPane.showInputDialog(null, promptName);

while ( response != null && response.length() > 0){
System.out.println("You are searching for " + response);
for (int i = 0; i < modelName.length; i++)
{
if (modelName[i] == response){
System.out.println( modelName[i] + "\t\t" + digitStockNumber[i] + "\t" + modelColor[i] + "\t" + integerPrice[i] );
}
}

response = JOptionPane.showInputDialog(null, promptName);

}
}

am i using arrays correctly or am i way off? The way im thinking it should run the for loop and test the modelName[i] and if it is == to response(name of vehicle user input) it will then display that line.

2007-07-15 16:17:42 · 1 answers · asked by Anonymous in Computers & Internet Programming & Design

1 answers

You're using arrays correctly, but not Strings, really. You want something like "response.equals( modelName[i])" because "==" tests for object identity. The input String will not be the exact same object as any model name, even though it might have the same content.

One other note:

There's an obvious improvement to make in the program design.

If you have modelName, digitStockNumber, modelColor, integerPrice, each of which is a value of a given catalog item... you'd probably want to create a class to hold all those values:

class CatalogItem {
String modelName;
String digitStockNumber;
String modelColor;
int integerPrice;
};

Then, instead of having four separate arrays with the same index, you'd have ONE array of CatalogItem [], and all of the values relevant to a single item would be together in one place.

The loop would look something like this:

// CatalogItem items[] filled in elsewhere;
for (int i = 0; i < items.length; i++) {
CatalogItem curItem = items[i];
if (curItem. modelName.equals( response) ){
System.out.println( curItem. modelName + "\t\t" + curItem. digitStockNumber + "\t" + curItem. modelColor + "\t" + curItem. integerPrice );
}
}

2007-07-15 17:33:11 · answer #1 · answered by McFate 7 · 0 1

fedest.com, questions and answers