将条目添加到Arraylist

时间:2014-03-28 18:56:09

标签: java arrays search arraylist

我正在尝试编译将地址簿条目添加到我的数组中的代码。我有大部分的驱动程序,它接受名字和姓氏的输入,然后添加它们,并搜索某些值。我的问题是添加新条目的代码 - 我试图在地址簿中添加多个字符串作为一个条目,然后调用addEntry,但我收到以下错误...

C:\ Users \ Scherpich \ Desktop \ Jim School \ CS 219编程基础\程序\ ScherpichJamesTest2.java:17:错误:类SJAddressBook中的构造函数SJAddressBook不能应用于给定类型;         aBook.add(new SJAddressBook());                   ^   required:String,String   发现:没有争论   原因:实际和正式的参数列表长度不同

C:\ Users \ Scherpich \ Desktop \ Jim School \ CS 219 Programming Fundamentals \ Programs \ ScherpichJamesTest2.java:18:error:找不到符号         aBook.get(计数).addEntry();                         ^   符号:方法addEntry()   location:类SJAddressBook

C:\ Users \ Scherpich \ Desktop \ Jim School \ CS 219 Programming Fundamentals \ Programs \ ScherpichJamesTest2.java:24:error:找不到符号         int foundIndex = abook.search(SJ);                                       ^   符号:变量SJ   位置:类ScherpichJamesTest2

C:\ Users \ Scherpich \ Desktop \ Jim School \ CS 219 Programming Fundamentals \ Programs \ ScherpichJamesTest2.java:24:error:找不到符号         int foundIndex = abook.search(SJ);                          ^   符号:变量abook   位置:类ScherpichJamesTest2

C:\ Users \ Scherpich \ Desktop \ Jim School \ CS 219 Programming Fundamentals \ Programs \ ScherpichJamesTest2.java:30:error:找不到符号             SJ.get(foundIndex)。显示();             ^   符号:变量SJ   location:类ScherpichJamesTest2 。\ SJAddressBook.java:27:错误:缺少方法体,或声明抽象     public static int search(ArrayListaBook);                       ^ 。\ SJAddressBook.java:34:错误:找不到符号         int choice = integer.parseInt(entry.nextLine());                    ^   符号:变量整数   location:类SJAddressBook 。\ SJAddressBook.java:38:error:找不到符号         for(SJAddressBook地址:aBook)                                    ^   符号:变量aBook   location:类SJAddressBook 。\ SJAddressBook.java:44:错误:返回外部方法                 return aBook.IndexOf(Address);                 ^ 。\ SJAddressBook.java:47:错误:找不到符号                 如果(value.equalisIgnoreCase(Address.LAST))                         ^   符号:方法equalisIgnoreCase(String)   location:String类型的变量值 。\ SJAddressBook.java:48:error:返回外部方法                 return aBook.IndexOf(Address);                 ^ 。\ SJAddressBook.java:53:错误:返回外部方法         返回-1;         ^ 12个错误

这是我的主要方法类

{

public static void main(String[] args)

{
    ArrayList<SJAddressBook> aBook = new ArrayList<SJAddressBook>();
    for (int count =0;count <3;count ++)
    {

    aBook.add(new SJAddressBook());
    aBook.get(count).addEntry();
    System.out.println();


    }

    int foundIndex = abook.search(SJ);

    System.out.println();

    if (foundIndex > -1)

        SJ.get(foundIndex).display();

    else

        System.out.println("No Entry Found");

}

}

//我的驱动程序类

public class SJAddressBook                  //start class definition
{
static String FIRST;
private String LAST;

public SJAddressBook(String first, String last)
{
    Scanner entry = new Scanner (System.in);
    System.out.print("Please Enter First Name: ");
    FIRST = entry.nextLine();
    System.out.print("Please Enter Last Name: ");
    LAST = entry.nextLine();
}
public static int search(ArrayList<SJAddressBook>aBook);
{
    Scanner entry = new Scanner (System.in);
    System.out.println("1. First Name");
    System.out.println("2. Last Name");

    System.out.print("Please Enter Field to Search: ");
    int choice=integer.parseInt(entry.nextLine());
    System.out.println("Please enter value to search for: ");
    String value=entry.nextLine();

    for(SJAddressBook Address: aBook)
    {
        switch(choice)
        {
            case 1:
            if(value.equalsIgnoreCase(Address.FIRST))
            return aBook.IndexOf(Address);
            break;
            case 2:
            if(value.equalisIgnoreCase(Address.LAST))
            return aBook.IndexOf(Address);
            break;

        }
    }
    return -1;
}
public void display()
{
    System.out.println("First Name: "+this.FIRST);
    System.out.println("Last Name: "+this.LAST);
}
}

1 个答案:

答案 0 :(得分:2)

  

C:\ Users \ Scherpich \ Desktop \ Jim School \ CS 219编程基础\程序\ ScherpichJamesTest2.java:17:错误:类SJAddressBook中的构造函数SJAddressBook不能应用于给定类型; aBook.add(new SJAddressBook()); ^ required:String,找到的字符串:无参数原因:实际和形式参数列表的长度不同

  • 您正在调用new SJAddressBook ()作为空构造函数,该类的唯一构造函数需要2个参数SJAddressBook (String first, String last)
  

C:\ Users \ Scherpich \ Desktop \ Jim School \ CS 219 Programming Fundamentals \ Programs \ ScherpichJamesTest2.java:18:error:找不到符号aBook.get(count).addEntry(); ^符号:方法addEntry()location:类SJAddressBook

  • SJAddressBook 中没有定义addEntry ()。通过调用 aBook.get(count),您将在 get(count)位置返回 SJAddressBook
  

C:\ Users \ Scherpich \ Desktop \ Jim School \ CS 219 Programming Fundamentals \ Programs \ ScherpichJamesTest2.java:24:error:找不到符号int foundIndex = abook.search(SJ); ^符号:变量SJ位置:类ScherpichJamesTest2

  • SJ 未在任何地方定义(我可以在您的示例中看到)
  

C:\ Users \ Scherpich \ Desktop \ Jim School \ CS 219 Programming Fundamentals \ Programs \ ScherpichJamesTest2.java:24:error:找不到符号int foundIndex = abook.search(SJ); ^符号:变量abook位置:类ScherpichJamesTest2

  • 也未定义 - 应该 aBook 而不是 abook
  

C:\ Users \ Scherpich \ Desktop \ Jim School \ CS 219 Programming Fundamentals \ Programs \ ScherpichJamesTest2.java:30:error:找不到符号SJ.get(foundIndex).display(); ^符号:变量SJ位置:类ScherpichJamesTest2

  • 再次 SJ 未定义
  

。\ SJAddressBook.java:27:错误:缺少方法体,或声明抽象的public static int search(ArrayListaBook); ^

  • 你有一个&#39 ;;&#39;不应该在那里的public static int search(ArrayList<SJAddressBook>aBook);
  

。\ SJAddressBook.java:34:错误:找不到符号int choice = integer.parseInt(entry.nextLine()); ^符号:变量整数位置:类SJAddressBook

  • 整数不是整数
  

。\ SJAddressBook.java:38:错误:找不到符号(SJAddressBook地址:aBook)^符号:变量aBook位置:类SJAddressBook

  • 由于之前的错误与&#39;;&#39;编译器认为这是静态代码块并且认为aBook未定义
  

。\ SJAddressBook.java:44:错误:返回外部方法返回aBook.IndexOf(Address); ^

  • 同样,因为&#39 ;;&#39;
  

。\ SJAddressBook.java:47:错误:找不到符号if(value.equalisIgnoreCase(Address.LAST))^符号:方法equalisIgnoreCase(String)位置:String类型的变量值

  • equalsIgnoreCase 不是 equalisIgnoreCase
  

。\ SJAddressBook.java:48:错误:返回外部方法返回aBook.IndexOf(Address); ^

  • 同样,因为&#39 ;;&#39;
  

。\ SJAddressBook.java:53:错误:返回外部方法返回-1; ^                                           ; 12个错误

  • 同样,因为&#39 ;;&#39;

免费的ide可能会让您的生活更轻松。 eclipseintellij