2013-06-28 40 views
2

我的方法searchSong()在我的主體中不起作用。 這裏是從類宋數組對象方法不會工作?

public class Library{ 
Song[] thelist=new Song[10]; 
int counter=0; 
private int i=0; 

public void addSong(Song s){//adds Song method to array 
    if(i<thelist.length){ 
    thelist[i]=s; 
    i++;} 
    else 

    public Song searchSong(String title, String album, String author, String interpreter) { 
    for(int j=0;j<thelist.length;j++) 
     if(thelist[j].title.equals(title) && thelist[j].album.equals(album) && thelist[j].author.equals(author) && 
      thelist[j].interpreter.equals(interpreter)) 

       return thelist[j]; 


      return null;} 

在我主我的對象數組,我必須輸入字符串標題,專輯,作家和翻譯返回的thelist [J]。

這裏是我的馬寧

Library list=new Library(); 
Song one=new Song(); 
list.addSong(one); 
one.title="hello"; 
one.album="world"; 
one.interpreter="luar"; 
one.author="me"; 
} 
list.searchSong(hello,world,luar,me); 

的list.searchSong()方法應該返回的東西,但我不斷收到此錯誤

TestLibrary.java:31: error: cannot find symbol 
    list.searchSong(hello,world,luar,me); 
        ^
    symbol: variable hello 
    location: class TestLibrary 
    TestLibrary.java:31: error: cannot find symbol 
    list.searchSong(hello,world,luar,me); 
+1

搜索參數需要字符串參數。如果你要傳遞'String'字面值,你必須將它們包裝在'''''中。其他方面,編譯器會認爲這些是變量名稱,並嘗試查找該變量。 – Thihara

+1

真的,請遵循Java編程的教程。 –

+0

任何鏈接到偉大的視頻? – raul

回答

5

把你好,世界,LUAR,我在雙引號: 「你好」, 「世界」, 「LUAR」, 「我」

+0

TestLibrary.java:31:錯誤:無法找到符號 \t \t list.searchSong(」你好」, 「世界」, 「LUAR」, 「我」); \t \t^ 符號:方法searchSong(字符串,字符串,字符串,字符串) 位置:類型Librarythen我得到這個錯誤 – raul

+0

@luar的變量列表 - 有編譯器錯誤在你的眼前今後相當長的名單。在您學習編程時,我們不打算與您一起檢查每一個。你最好的選擇是去做一些基本的教程,以便你瞭解基礎知識。 – jahroy

2

它應該是:

list.searchSong("hello","world","luar","me"); 
3

您沒有任何變量名爲hello,world,luarme。這正是Java正在尋找的。

我不確定你的Song對象的結構(或爲什麼你會這樣做),但似乎這是你想要的。我假定這些字段是String文字,或者你必須編譯失敗早得多:

list.searchSong(one.title, one.album, one.interpreter, one.author); 

或者,也可以在字符串文本傳遞。然而,這似乎是一種浪費,因爲你已經有了這樣的信息。

哦 - 你也不會對你的回報值做任何事情。你想捕捉的Song的實例:

Song result = list.searchSong(one.title, one.album, one.interpreter, one.author); 
+0

也許這就是luar的意思...... – Thihara

+0

+1我不確定這個問題是否應該得到答案,但至少你沒有給出不好的建議來像其他人一樣傳遞字符串文字。 – jahroy

+0

該列表最終將保存10首歌曲對象,因此我需要按標題,專輯,解釋器和作者搜索數組。 – raul

0

你在你的searchSong()方法的參數中聲明的字符串值。

所以,當你調用你的主要方法,它應該是:

list.searchSong("hello","world","luar","me");

相關問題