2014-10-29 86 views
0

我有一個hentAntall方法在我的代碼如下。它應該在txt文件中找到搜索詞。我沒有得到任何錯誤。它只是不會打印出任何兩條可能的線。while循環將不會讀取文件,或打印出行java

此方法必須首先訪問構造函數才能獲取搜索詞,然後必須在txt文件中找到該搜索詞並將其添加到計數中。構造函數從另一個類獲取搜索詞。像這樣new lolz(「searchword」)。hentAntall();

(我在這個程序中的笨命名道歉,但它只是一個我的節目之一的副本,我只是想糾正它沒有搞砸了原來的。)

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

public class lolz { 

    private String sokeord=null; 
    private int antall = 0; 
    // Constructor 
    lolz(String searchword) throws Exception{ 
    this.sokeord = searchword; 
    } 

    //toString method, to print in the same format. 
    @Override 
    public String toString(){ 
     return "\nSokeordet er: " + sokeord+ "\n"; 
    } 

    // Gets the ammount of the searchword 
    public int hentAntall() throws Exception{ 
    File file = new File("Hvorfor.txt"); 
    Scanner readfile = new Scanner(file); 
    while (readfile.hasNextLine()){ 
      String nextline = readfile.nextLine(); 
      if (nextline.equalsIgnoreCase(sokeord)) { 
      antall ++; 
      System.out.println("Antallet av:" + sokeord + "er " + antall); 
     } 
     else {System.out.println("Error no such search word in the given text");} 
    } 
    return antall; 
    } 

    // void methode to increase the count of a searcheword. 
    void oekAntall() { 
    antall++; 
    } 
} 

這是調用此方法的另一個類,並且還向構造函數提供了信息。

public class Main { 

public static void main(String[] args) throws Exception { 
    new lolz("fungerer").hentAntall(); 

}} 

也試過一些建議,他們沒有工作,我只能取得退出代碼完成的0

+1

告訴我們您的主類,並在調用該函數 – LeatherFace 2014-10-29 18:36:51

+2

你永遠實際上讀什麼 – user2717954 2014-10-29 18:37:10

+0

呀,這種方法是不是在你的代碼中調用.. – ryekayo 2014-10-29 18:37:25

回答

0

您的問題:

您正在嘗試比較一個字符串變量掃描儀變量!?

說明

你嘗試比較掃描器,它是內容

java.util.Scanner中[定界符= \ p {javaWhitespace} +] [位置= 0] [匹配 valid = true] [need input = false] [source closed = false] [skipped = false] [group separator = \,] [decimal separator =。] [positive prefix =] [negative prefix = \ Q- \ E] [positive suffix =] [negative suffix =] [NaN string = \Q \ E] [infinity string = \Q∞\ E]

帶有String變量的內容。


你並不會因爲以下

if (readfile.equals(sokeord)) { 

閱讀每一行,你應該有

if (readfile.nextLine().equals(sokeord)) { 
+0

謝謝你指出,我的壞,但即使我解決了這個問題仍然沒有解決。現在我已經發布了主要方法,以查看是否有任何問題。 – liptonvice 2014-10-29 19:29:52

+0

@liptonvice什麼是其他錯誤? – 2014-10-29 19:33:34

+0

沒有其他錯誤,我只是得到線程過程完成退出代碼0 – liptonvice 2014-10-29 19:46:38

0

而不是一個消息處理:

readfile.equals(sokeord) 

這是一個比較類型爲Scanner的實例與String(永遠不會是true)。你需要讀一行並比較。

String line = readfile.nextLine(); 
if(line.equals(sokeord)){ 
0

添加一個main方法到類:

public static void main(String[] args) 
{ 
    hentAntall(); 
} 

你將不得不作出hentAntall()靜態或創建LOLZ類的實例,並調用它的方式。

而且改變:

while (readfile.hasNext()){ 
    if (readfile.nextLine().contains(sokeord)) { 

您需要實際讀取輸入,然後檢查是否存在sokeord在該行還是不行。

0

你hentAntall方法應該是這樣的:

public int hentAntall() throws Exception { 
    File file = new File("Hvorfor.txt"); 
    Scanner readfile = new Scanner(file); 
    while (readfile.hasNextLine()) { 
     String word = readfile.next(); 
     if (word.contains(sokeord)) { 
      antall++; 
      System.out.println("Antallet av:" + sokeord + "er " + antall); 
     } else { 
      System.out 
        .println("Error no such search word in the given text: "); 
     } 
    } 
    readfile.close(); 
    return antall; 
} 

別忘記關閉掃描儀資源以避免泄漏。