在对象数组中搜索字符串

时间:2014-07-02 18:20:36

标签: java arrays string object if-statement

我目前正在开展的项目是个人借阅图书馆应用程序。用户可以将对象MediaItems添加到数组中,并可以为对象的字段,格式,onLoan等分配值。

我目前遇到的问题是当我在数组中搜索特定项目时。无论我为seach标准添加什么,它总会回归真实。我想我在某个地方错过了一段代码,因为它几乎就好像程序实际上并没有查看字符串的上下文以查看它是否匹配。这是我的计划:

import java.util.Arrays;
import java.util.Scanner;

public class TrippelTylerPersonalLendingLibrary {

   //Fields
   MediaItem[] items = new MediaItem[100];
   int numberOfItems = 0;

   //Methods
   int displayMenu() {
       Scanner input = new Scanner(System.in);
       int choice;
       System.out.print("\nMenu\n" +
            "1. Add new item\n" +
            "2. Mark an item as on loan\n" +
            "3. List all items\n" +
            "4. Mark an item as returned\n" +
            "5. Quit\n\n" +
            "What would you like to do?");
       choice = input.nextInt();
       if(choice<1 || choice>5) {
           while(choice<1 || choice>5) {
               System.out.println("You have entered an invalid menu choice.\n" +
                    "What would you like to do?");
               choice = input.nextInt();
           }
           return choice;
       }
       else {
           return choice;
       }
   }

   void addNewItem(String title, String format) {
       String titleNew = title;
       String formatNew = format;
       MediaItem newItem = new MediaItem(titleNew, formatNew);
       items[numberOfItems] = newItem;
       numberOfItems= numberOfItems + 1;
   }

   void markItemOnLoan(String title, String name, String date) {
       String itemTitle=title;
       String person=name;
       String itemDate=date;
       for(int i=0; i<items.length;i++) {
           if(itemTitle == items[i].title) {
               items[i].onLoan = true;
               items[i].dateLoaned = itemDate;
               items[i].loanedTo = person;
           }
           else{
               System.out.println("I'm sorry, I couldn't find " + itemTitle + 
                    " in the library.");
           }
       }
   }

   String[] listAllItems() {

   }

   void markItemReturned(String title) {

   }

   //Main method
   public static void main(String[] args) {

       TrippelTylerPersonalLendingLibrary instance = new TrippelTylerPersonalLendingLibrary();
       MediaItem instance2 = new MediaItem();


       //Fields for main method
       int choice;
       int repeat = 1;
       String newTitle;
       String newFormat;
       String name;
       String date;
       boolean loaned;

       while(repeat == 1) {
          choice = instance.displayMenu();
          if(choice == 1) {
             newTitle = instance2.setTitle();
             newFormat = instance2.setFormat();
             instance.addNewItem(newTitle, newFormat);
          }
          else if(choice == 2) {
             Scanner input = new Scanner(System.in);
             String titleToLoan;
             name=instance2.setLoanedTo();
             date=instance2.setDateLoaned();
             loaned=instance2.setOnLoan();
             titleToLoan= input.nextLine();
             instance.markItemOnLoan(titleToLoan, name, date);
          }
       }
   }

}

在main方法中,当choice == 2时,它调用markItemOnLoan()并根据用户键入的内容传递字符串。即使我输入完全错误的东西,它也永远不会进入else语句在markItemOnLoan方法中。我是否需要更改搜索字符串的方式?

谢谢大家!

泰勒

2 个答案:

答案 0 :(得分:0)

if(itemTitle.equals(items[i].title))

答案 1 :(得分:0)

if(itemTitle.equals(items [i] .title))是正确的方法。 .beause ==只比较原始数据类型,比如int.to比较你应该使用的对象数据类型.equals方法示例&#34; hi&#34; .equals(&#34; hi&#34;)是真的