2013-10-11 73 views
0

我需要一些幫助,無論我試圖把它寫入下面的代碼,它不工作。我希望它從Alice.txt文件中讀取,然後將每個單詞放入數組ordArray中,然後使用其餘的程序來計算每個單詞,每個單詞和每個獨特單詞的每次出現次數。請幫助!如果你能給我一些提示,說明我可以做得更好,或者我應該如何實現將信息寫入文件Opplysning.txt的部分,請不要保持安靜。讀取文件中的每一個字到數組

try { 
     File skrivFil = new File("Opplysning.txt"); 
     FileWriter fw= new FileWriter(skrivFil); 
     BufferedWriter bw = new BufferedWriter(fw); 

     Scanner lesFil = new Scanner("Alice.txt"); 
     int i=0; 
     int totalOrd=0; 
     int antUnikeOrd=0; 

     String[] ordArray = new String[5000]; 
     int[] antallOrd = new int[5000];  
     String ord = lesFil.next().toLowerCase(); 
     totalOrd++; 
     boolean ordFraFor=false; 
     int y=0; 
     int z=0; 

     for(i=0; i<ordArray.length; i++) { 
      if (ord.equals(ordArray[i])) { 
     antallOrd[i]++; 
     ordFraFor=true; 
     } 
    } 
     if(ordFraFor=false) { 
      antUnikeOrd++; 
      y=0; 
      boolean ordOpptelling=false; 

     while(ordOpptelling=false) { 
     if(ordArray[y] == null) { 
      ordArray[y] = ord; 
      antallOrd[y]++; 
      ordOpptelling=true; 
     } 
     y++; 
    } 
} 

     for(String s: ordArray) { 
     System.out.println(s); 
     } 
     lesFil.close(); 

     } catch (Exception e){ 
     System.out.print(e); 
     } 
    } 
} 

編輯: 嘗試添加該文件,我想它的工作,但我仍然沒有能夠寫入到陣列。我真的很糟糕,這就是爲什麼我必須在下週才能真正使用這些東西...... 無論如何,我試圖將所有單詞添加到arraylist中,我希望它能夠工作。它沒有,它給了我一個nosuchelementexception代替。 代碼的一部分:

File tekstFil = new File ("Alice.txt"); 
Scanner lesFil = new Scanner(tekstFil); 
int i=0; 
int totalOrd=0; 
int antUnikeOrd=0; 

ArrayList<String> liste = new ArrayList<String>(); 
while (lesFil.hasNext()){ 
    liste.add(lesFil.next()); 
} 

String[] ordArray =liste.toArray(new String[liste.size()]);; 
int[] antallOrd = new int[5000]; 
+1

嚴重縮進的代碼長城。問題描述:它不起作用。你真的幫我們幫助你... –

回答

2

您的掃描儀正試圖掃描字符串文字,「Alice.txt」,沒有相應的文件。如果你想要的文件,然後先構建你的文件,然後把它傳遞到掃描儀的構造:

File textFile = new File("Alice.text"); // or file path variable 
Scanner fileScanner = new Scanner(textFile); 

// go to town with your Scanner 

,或者......

InputStream inStream = getClass().getResourceAsStream(someResourcePath); 
Scanner myTextScanner = new Scanner(inStream); 

接下來我們將討論關於不使用ArrayList而是Map<String, Integer>,例如HashMap<String, Integer>


編輯
幽州:

嘗試添加該文件,我想它的工作,但我仍然沒有能夠寫入到陣列。我對此非常不滿,這就是爲什麼我必須在下週使用這些東西......無論如何,我試圖將所有的單詞添加到一個數組列表中,我希望它能夠工作。它沒有,它給了我一個nosuchelementexception代替。代碼的一部分:

我建議你停下來,把你的程序擱置下來,並試圖解決你的程序的每個部分孤立。首先看看你是否真的獲得你的文件。

創建這樣的事情,當然,改變你的文件路徑,以便它是有效的:

import java.io.File; 
import java.io.FileNotFoundException; 
import java.util.Scanner; 

public class Foo { 

    // **** you will use a different String literal here!! **** 
    private static final String FILE_PATH = "src/foo/foo.txt"; 

    public static void main(String[] args) throws FileNotFoundException { 
     File file = new File(FILE_PATH); 

     // check if file exits and if we're looking in the right place 
     System.out.println("File path: " + file.getAbsolutePath()); 
     System.out.println("file exists: " + file.exists()); 

     Scanner scan = new Scanner(file); 

     // scan through file to make sure that it holds the text 
     // we think it does, and that scanner works. 
     while (scan.hasNextLine()) { 
     String line = scan.nextLine(); 
     System.out.println(line); 
     } 
    } 
} 

然後你得到這個工作,在閱讀的文本到ArrayList的工作之後。

和往常一樣,請努力改進您的代碼格式,尤其是縮進。如果出現錯誤或異常,請在此處打印整個文本並指出哪一行代碼正在拋出異常。

+0

好的,現在就完成了。 – Makri

+0

@Makri:發佈代碼作爲原始帖子的修改,通常是底部的補充,而不是作爲對答案或原始帖子的評論。如您所見,代碼無法在註釋中格式化,並且無法讀取。此外,您的原始文章中的代碼格式有一些問題,使我們很難理解和閱讀。我們希望您付出更多努力,讓人們更容易幫助您。這當然不會問太多。 –

+0

而且,我不能使用HashMap,因爲我還沒有達到那個部分,需要首先確定這個東西。這樣做很難... -.- – Makri

相關問題