2014-09-02 48 views
-2

目前我正在做一個HangMan GUI遊戲Java。當我把這些單詞放入程序中時,它是有效的。Java文件文件=新文件不起作用;找不到符號

但現在我想加載一個文本文件,並在字符串內容下面的代碼中創建它的字符串。

這裏在StackOverflow我已閱讀有關使用掃描儀。

現在我有這個代碼,但它不會接受File file = new File("woordenlijst.txt");聲明,它在'文件'中說它找不到符號。你可以幫我嗎?這是我的代碼:

import java.util.Scanner; 

public class galgjeGUI extends javax.swing.JFrame { 

/** 
* Creates new form galgjeGUI 
*/ 

private String wGalg; // het te raden woord 
private int fouten;  // globale variabele toegevoegd jonp 
private int pogingen; 
private int levens = 7; 

public galgjeGUI() { 
    initComponents(); 

    buttonDisableFunction(); 

    File file = new File("woordenlijst.txt"); 
    Scanner scan = new Scanner(file); 
    scan.useDelimiter("\\Z"); 
    String content = scan.next(); 
} 
+2

導入它;它在'java.io'中。 – rgettman 2014-09-02 16:44:02

+2

您忘記了您的導入。 'import java.io.File;' – 2014-09-02 16:44:06

+0

一個好的IDE應該爲你管理導入(否則它們會非常快速地變得繁瑣)。 – 2014-09-02 16:44:40

回答

0

1 )導入適當的包。

2)處理異常。

3)close()Scanner使用後的對象。

import java.io.*; //import 
Scanner scan = null; 
try {  //handle exceptions 
    File file = new File("woordenlijst.txt"); 
    scan = new Scanner(file); 
} 
catch(FileNotFoundException e) { 
    System.out.println(e); 

} 
finally { 
    scan.close(); // give up the resource. 
} 
1

怎樣的Java知道您File的意思,也沒有叫File類,你正在尋找java.io.File這樣告訴編譯器使用,通過增加

import java.io.File; 
+1

謝謝!但知道我在下一行有一個問題:new Scanner(file);它說:未報告的異常FileNotFoundException;必須被捕獲或聲明爲拋出 ---- – 2014-09-02 16:49:22

+0

您將需要使用帶有一致性例外的try/catch塊。 – pzaenger 2014-09-02 16:52:42