简体   繁体   中英

Loop searching an array, printing multiple results

I have to search a Cadillac object array to bring up results of a certain model, let's say "Escalade", and I am having trouble figuring out where my code specifically needs to be placed in order to have the results print on the console screen. I keep getting stuck in a loop when I try printing what I've got now. Everything else about my program is working, I just need to be able to display multiple results on the console screen.

Here is the code I have:

    while( response != null )
    {
      if( response.length() > 0 )
      {
        count = 0;
        found = false;
        while( count < data.length && data[count] != null && !found )
        {
          item = data[count].getModel();
          if( item.equalsIgnoreCase( response ) )
          {

            found = true;
          }
          else{
            count++;
          }
          message = "Model:  " + data[count].getModel() + "\n" +
                    "Stock Number:  " + data[count].getStockNum() + "\n" +
                    "Color            :  " + data[count].getColor() + "\n" +
                    "Price  :  " + data[count].getPrice();
          System.out.print( message );
        }

        if( !found )
          message = "Model not found in array.";
    }
    }
    response = JOptionPane.showInputDialog( prompt );
  }

Also, the second part of the searching is to display search results for cars that are within $3000 of the user's input, and would definitely appreciate some guidance on that as well, since these two types of searches are relatively similar.

You didn't show all your code but if you are in an infinite loop, then look at the loop condition. It looks like you are checking to see if response is not null, but response is never assigned inside the loop, so once it enters you'll be stuck.

Also, count should probably be incremented ever time since you are checking that in the loop. When one is found, you don't increment it so the loop never breaks.

I would suggest writing this block of code again from scratch. I think you can simplify your conditions to make it easier to understand for yourself (and the grader).

I'll get you started.

do {
    response = JOptionPane.showInputDialog( prompt );
    for (int i=0; i < data.length; i++) {
        // do stuff with data[i] here
    }
} while(response != null);

I think a do-while loop makes sense for checking user input. A for loop makes sense if you are looping through ever element. From here you can check the user input, no need to count the length since equalsIgnoreCase() will handle the string comparison for you.

Your inner loop is never getting over if a model is matched or found.So in this case, the loop becomes infinite.You need to increment the count in the inner loop.So this increment should happen independently and your can safely finish the inner loop.But one more thing you can easily do here to break the inner loop once a model is found and display its information after this.

while( count < data.length && data[count] != null && !found )
        {
          item = data[count].getModel();
          if( item.equalsIgnoreCase( response ) )
          {

            found = true;
            break;
          }
          else{
            count++;
          }
          message = "Model:  " + data[count].getModel() + "\n" +
                    "Stock Number:  " + data[count].getStockNum() + "\n" +
                    "Color            :  " + data[count].getColor() + "\n" +
                    "Price  :  " + data[count].getPrice();
          System.out.print( message );
        }

Hope this will help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM