2016-04-17 5 views
3

私は課題に取り組んでいます。しかし、私は最後のエラーメッセージを理解していないだけで、あなたがそれを理解するのを助けることができるかどうか疑問に思っていたので、将来それを避けることができます。コレクションを文字列に変換して、無効なメソッド宣言を取得しました

import java.util.*; 

public class Proj09{ 
    public static void main(String args[]){ 
    new Proj09Runner().getCollection(); 
    } 
} 

class Proj09Runner{ 
    public void getCollection(){ 
     Collection collection = new TreeSet(); 
     Populator.fillIt(collection); 
     Iterator iter = collection.iterator(); 
     while(iter.hasNext()){ 
      System.out.print(iter.next()); 
     } 
    System.out.println(); 
    } 
} 

class Populator{ 
    public static void fillIt(Collection collection){ 
     collection.add("Able"); 
     collection.add("Baker"); 
     collection.add("aBle"); 
     collection.add("Charley"); 
     collection.add("Baker"); 
    } 
} 

出力:

割り当ては、サンプルコードで始まったエイブルベーカーできチャーリー・ベイカー

そして、私たちの仕事は、それは私がだと異なる主に収まるように、そのコードを変更することです編集することはできません:

import java.util.*; 

public class Proj09{ 
    public static void main(String args[]){ 
     Proj09Runner runner = new Proj09Runner(); 

     Collection <String> collection = runner.getCollection(); 

     collection.add("Able"); 
     collection.add("Baker"); 
     collection.add("aBle"); 
     collection.add("Charley"); 
     collection.add("Baker"); 

     Iterator <String> iter = collection.iterator(); 
     while(iter.hasNext()){ 
      System.out.print(iter.next() + " "); 
     } 
     System.out.println(); 
    } 
} 

これまでのところ、私はProj09Runnerクラスでプレーしてきた:

class Proj09Runner extends Proj09{ 
    public void getCollection(){ 
    System.out.println("My Name."); 
    System.out.println(); 
    } 
} 

小さな出力が変更され、私の名前を追加することになっています。 所望の出力: エイブルベーカーできチャーリー・ベイカー

は、しかし、私は、私が変更になっていないよメインにラインCollection <String> collection = runner.getCollection();に戻って別のエラーを得続ける 私の名前。私はvoid型が呼び出されてCollection <String>に変換できないか、または "無効なメソッドの宣言;戻り値の型が必要です。"

なぜ私はこれらのメッセージを受け取っているのか理解できて、将来的にそれらを避けることができますか?

ありがとう、本当にありがとうございます。必ずニーズ改善のための

+0

'getCollection'の戻り値の型を変更する必要があります。 – Radiodef

答えて

0

私は先に行くと答えを共有したいと思った:

import java.util.*; 

class Proj09Runner{ 
    public Collection <String> getCollection(){ 
     System.out.println("Name."); 
     Collection collection = new ArrayList(); 
     return collection; 
    } 
} 

それは重複を受け付けないので、一度だけ「ベイカー」を印刷し、私はArrayListのと一緒に行ったので、私はTreeSetので行くことができませんでした代わりに。

フィードバックいただきありがとうございます!

0

この:これはあなたの方法に注意し、Collection<String>を返さない

Collection <String> collection = runner.getCollection(); 

は何も返しません。

public void getCollection(){ 

あなたのメソッドの戻り値の型と定義は次のように何かを変更次のいずれか

public Collection<String> getCollection(){ //followed by corresponding definition 

それとも、単に

Collection <String> collection = new HashSet() ; //whatever your collection pertain to 
0
public Collection<String> getCollection(){ 
     Collection collection = new TreeSet(); 
     Populator.fillIt(collection); 
     Iterator iter = collection.iterator(); 
     while(iter.hasNext()){ 
      System.out.print(iter.next()); 
     } 
    System.out.println(); 
    return collection; 
    } 

を使用することができ、あなたのgetCollection methodnothingを返しています。それは返す必要がありますcollection

0

私はこれを試してきました、それは良い方向に思われる。メインを維持:

import java.util.*; 

public class Proj09{ 
    public static void main(String args[]){ 
     Proj09Runner runner = new Proj09Runner(); 

     Collection <String> collection = runner.getCollection(); 

     collection.add("Able"); 
     collection.add("Baker"); 
     collection.add("aBle"); 
     collection.add("Charley"); 
     collection.add("Baker"); 

     Iterator <String> iter = collection.iterator(); 
     while(iter.hasNext()){ 
      System.out.print(iter.next() + " "); 
     } 
     System.out.println(); 
    } 
} 

私が今持っている:

class Proj09Runner{ 
    public Collection<String> getCollection(){ 
     return "Hailey"; 
    } 
} 

私はそれが不要に思えたとしてメインにすでにあったものを繰り返すしたくありませんでした。文字列 "Hailey"を返すと、古いエラーに対処しましたが、新しいエラーが発生しました。

error: cannot find symbol 
public Collection<String> getCollection(){ 
    ^
symbol: class Collection 
location: class Proj09Runner 

エラーの種類がわかっていますか?

私たちの最後のユニットは継承されていましたが、「Proj09Runner extends Proj09 {」と演奏しましたが効果がないようです。

関連する問題