2012-08-22 61 views
4

我試圖隱藏這是我從一個文本文件列表檢索到的隨機字,但代碼不斷給我下面的錯誤:Array Required, but java.lang.String found陣列所需,但java.lang.String中發現

import java.awt.*; 
import java.awt.event.*; 
import java.util.Arrays; 
import javax.swing.*; 
import java.io.*; 
import java.util.ArrayList; 
import java.util.Random; 
import java.util.List; 

public class Hangman extends JFrame 
{ 
private static final char HIDECHAR = '_'; 
String imageName = null; 
String Path = "D:\\Varsity College\\Prog212Assign1_10-013803\\images\\"; 
static int guesses =0; 
private String original = readWord(); 
private String hidden; 
int i = 0; 
static JPanel panel; 
static JPanel panel2; 
static JPanel panel3; 
static JPanel panel4; 

public Hangman(){ 
JButton[] buttons = new JButton[26]; 

this.original = original; 
this.hidden = this.createHidden(); 
panel = new JPanel(new GridLayout(0,9)); 
panel2 = new JPanel(); 
panel3 = new JPanel(); 
panel4 = new JPanel(); 

JButton btnRestart = new JButton("Restart"); 
btnRestart.addActionListener(new ActionListener(){ 
    public void actionPerformed(ActionEvent e) 
    { 

    } 
}); 

JButton btnNewWord = new JButton("Add New Word"); 
btnNewWord.addActionListener(new ActionListener(){ 
    public void actionPerformed(ActionEvent e) 
{ 
try 
{ 
    FileWriter fw = new FileWriter("Words.txt", true); 
    PrintWriter pw = new PrintWriter(fw, true); 

    String word = JOptionPane.showInputDialog("Please enter a word: "); 

    pw.println(word); 
    pw.close(); 
} 
catch(IOException ie) 
{ 
    System.out.println("Error Thrown" + ie.getMessage()); 
} 
} 
}); 

JButton btnHelp = new JButton("Help"); 
btnHelp.addActionListener(new ActionListener(){ 
    public void actionPerformed(ActionEvent e) 
    { 
     String message = "The word to guess is represented by a row of dashes, giving the number of letters and category of the word." 
       + "\nIf the guessing player suggests a letter which occurs in the word, the other player writes it in all its correct positions." 
       + "\nIf the suggested letter does not occur in the word, the other player draws one element of the hangman diagram as a tally mark." 
       + "\n" 
       + "\nThe game is over when:" 
       + "\nThe guessing player completes the word, or guesses the whole word correctly" 
       + "\nThe other player completes the diagram"; 
     JOptionPane.showMessageDialog(null,message, "Help",JOptionPane.INFORMATION_MESSAGE); 
    } 
}); 

JButton btnExit = new JButton("Exit"); 
btnExit.addActionListener(new ActionListener(){ 
    public void actionPerformed(ActionEvent e) 
    { 
     System.exit(0); 
    } 
}); 
JLabel lblWord = new JLabel(original); 

if(guesses >= 0) imageName = "Hangman1.jpg"; 
if(guesses >= 1) imageName = "Hangman2.jpg"; 
if(guesses >= 2) imageName = "Hangman3.jpg"; 
if(guesses >= 3) imageName = "Hangman4.jpg"; 
if(guesses >= 4) imageName = "Hangman5.jpg"; 
if(guesses >= 5) imageName = "Hangman6.jpg"; 
if(guesses >= 6) imageName = "Hangman7.jpg"; 
ImageIcon icon = null; 
if(imageName != null){ 
    icon = new ImageIcon(Path + File.separator + imageName); 
} 

JLabel label = new JLabel(); 
label.setIcon(icon); 
String b[]={"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"}; 
for(i = 0; i < buttons.length; i++) 
{ 
    buttons[i] = new JButton(b[i]); 

    panel.add(buttons[i]); 
} 

panel2.add(label); 

panel3.add(btnRestart); 
panel3.add(btnNewWord); 
panel3.add(btnHelp); 
panel3.add(btnExit); 
panel4.add(lblWord); 
} 
public String readWord() 
{ 
try 
{ 
BufferedReader reader = new BufferedReader(new FileReader("Words.txt")); 
String line = reader.readLine(); 
List<String> words = new ArrayList<String>(); 
while(line != null) 
{ 
    String[] wordsLine = line.split(" "); 
    boolean addAll = words.addAll(Arrays.asList(wordsLine)); 
    line = reader.readLine(); 
} 
Random rand = new Random(System.currentTimeMillis()); 
String randomWord = words.get(rand.nextInt(words.size())); 
return randomWord; 

}catch (Exception e){ 
    return null; 
} 

} 
private String printWord(){ 
StringBuilder sb = new StringBuilder(); 
for (int i = 0; i < this.original.length(); i++){ 
    sb.append(HIDECHAR); 
} 
return sb.toString(); 
} 
public boolean check(char input){ 
boolean found = false; 
for (int i = 0; i < this.original.length(); i++){ 
    if(this.original.charAt(i)== input)){ 
     found = true; 
     this.hidden[i] = this.original.charAt(i); 
    } 
} 
return found; 
} 

public static void main(String[] args) 
{ 

    System.out.println(); 
    Hangman frame = new Hangman(); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    Box mainPanel = Box.createVerticalBox(); 
    frame.setContentPane(mainPanel); 
    mainPanel.add(panel, BorderLayout.NORTH); 
    mainPanel.add(panel2); 
    mainPanel.add(panel4); 
    mainPanel.add(panel3); 
    frame.pack(); 
    frame.setVisible(true); 
} 

} 

好吧有整個代碼<錯誤現在在線151和149 ...我也試圖根據其中一個帖子修復它

+4

你沒有向我們展示整個代碼庫, ne是錯誤嗎? – jn1kk

+2

你可以發佈以下變量定義:'HIDECHAR','original','hidden'? – Florent

回答

9

你不能使用數組下標:[],以索引到一個String,並且hiddenoriginal都是Strings

您可改爲使用original.charAt(i)請參閱索引處的字符。

一個字符在一個索引:java Strings是不可變的,所以你不能改變個別字符。而是讓hidden一個StringBuilder,或只是一個char[]

// in your class member declarations: 
char hidden[] = createHidden(); 

// possible implementation of createHidden: 
char[] createHidden() 
{ 
    if (original != null) 
     return new char[original.length()]; 
    else return null; 
} 

然後你的循環可以使用original.charAt像這樣:

if (this.original.charAt(i) == input) 
{ 
    found = true; 
    this.hidden[i] = this.original.charAt(i); 
+0

你確定'this.hidden.charAt(i)= this.original.charAt(i)'line compiles?即使它編譯,是否真的有用? –

+0

@LuiggiMendoza我相信它沒有。謝謝你的收穫 – pb2q

1

當您使用original.length()的字符串,長度爲()方法適用於字符串,不適用於Array,對於數組,長度是實例變量。

2.嘗試這樣....

this.hidden[i] = original.charAt(i);

3.正如char是不是一個對象一個原始,使用"=="

if (this.original[i] == input)