2013-07-19 7 views
9

私はFriends f = new Friends(friendsName, friendsAge);の()カッコ内の何かを置くしようとすると、それはエラーを思い付く:「実または仮引数リストの長さが異なる」

Constructor Friends in class Friends cannot by applied to given types. Required: no arguments. Found: String, int. Reason: actual or formal argument lists differ in length.

しかし、私は、引数友達リストを取り出し「null 0」のみが表示されます。 String friendsName = input.next();があっても値が設定されていませんか?

また、友人を削除しようとすると、何もしません。ソースコードでは、警告を表示しています。

Suspicious call to util.java.Collection.remove: Given object cannot contain given instances of String (expected Friends).

私はそれを覚えていますか?

import java.util.ArrayList; 
import java.util.Scanner; 

public class Friends 
{ 
    public static void main(String[] args) 
    { 
     int menu;  
     int choice; 
     choice = 0;  

     Scanner input = new Scanner(System.in); 
     ArrayList<Friends> friendsList = new ArrayList< >();  

     System.out.println(" 1. Add a Friend "); 
     System.out.println(" 2. Remove a Friend "); 
     System.out.println(" 3. Display All Friends "); 
     System.out.println(" 4. Exit "); 
     menu = input.nextInt(); 

     while(menu != 4) 
     {  

      switch(menu) 
      {      

      case 1: 

       while(choice != 2) 
       { 
        System.out.println("Enter Friend's Name: "); 
        String friendsName = input.next(); 
        System.out.println("Enter Friend's Age: "); 
        int friendsAge = input.nextInt();        
        Friends f = new Friends(friendsName, friendsAge); 
        friendsList.add(f); 
        System.out.println("Enter another? 1: Yes, 2: No"); 
        choice = input.nextInt(); 
       } break; 

      case 2: 

       System.out.println("Enter Friend's Name to Remove: "); 
       friendsList.remove(input.next());     
       break; 

      case 3: 

       for(int i = 0; i < friendsList.size(); i++) 
       { 
        System.out.println(friendsList.get(i).name + " " + friendsList.get(i).age);       
       } break;     
     } 

     System.out.println(" 1. Add a Friend "); 
     System.out.println(" 2. Remove a Friend "); 
     System.out.println(" 3. Display All Friends "); 
     System.out.println(" 4. Exit "); 
     menu = input.nextInt(); 

    } 

    System.out.println("Thank you and goodbye!"); 

} 

    public String name; 
    public int age;  

    public void setName(String friendsName) 
    { 
     name = friendsName; 
    } 
    public void setAge(int friendsAge) 
    { 
     age = friendsAge; 
    } 
    public String getName() 
    { 
     return name; 
    } 
    public int getAge() 
    { 
     return age; 
    } 
} 
+0

あなたは友人 'のコンストラクタを作成する必要があります(' – hexafraction

答えて

7

:あなたはcustructorを指定する必要が

Friends f = new Friends(friendsName, friendsAge); 

クラスはパラメータを取るコンストラクタを持っていません。コンストラクタを追加するか、存在するコンストラクタを使用してオブジェクトを作成し、次にset-methodsを使用する必要があります。たとえば、代わりに上記の:

Friends f = new Friends(); 
f.setName(friendsName); 
f.setAge(friendsAge); 
+0

恐ろしいです。助けてくれてありがとう! "friendsList.remove(input.next());"が幸せでない理由は何ですか? – user2585969

+1

あなたは、 'Friend'オブジェクトを含むリストから' String'を削除すると、 'Friend'のリストを繰り返して、入力したものと同じ名前の' Friend'を見つける必要があります。 – PaulProgrammer

+2

"空のコンストラクタ+モディファイア"をアンチパターンと見なしてください。obを構築することは良い考えではないと思います後で(忘れ去られたかもしれない)ステップで初期化されるまで、使用できない、またはエラーやその他の驚きを引き起こす可能性があります。 – PaulProgrammer

1

デフォルトコンストラクタには引数はありません。あなたはこのようFriendsクラスのオブジェクトをインスタンス化しようと

public Friends(String firstName, String age) { ... } 
+0

彼は実際にそれにいくつかの性質を持っている彼らはちょうどゲッター/セッターの上の代わりに位置しています。トップに。 – resueman

+0

私の返信を編集しました。 – PaulProgrammer

関連する問題