2015-03-18 35 views
1

這是我的第一篇文章,所以我不知道這裏的工作是如何工作的。 基本上,我需要一些幫助/建議與我的代碼。該方法需要閱讀某條線和輸入文字後打印出的文字和=緩衝讀者閱讀某些行和文本

的文本文件,想

A = Ant 

B = Bird 

C = Cat 

因此,如果用戶其輸入"A"它應該打印出像

東西
-Ant 

到目前爲止,我設法使它忽略"="但仍然打印出整個文件

這裏是我的代碼:

public static void readFromFile() { 
    System.out.println("Type in your word"); 
    Scanner scanner = new Scanner(System.in); 
    String input = scanner.next(); 
    String output = ""; 
    try { 
     FileReader fr = new FileReader("dictionary.txt");   
     BufferedReader br = new BufferedReader(fr);    
     String[] fields; 
     String temp; 
     while((input = br.readLine()) != null) { 
      temp = input.trim(); 
      if (temp.startsWith(input)) { 
       String[] splitted = temp.split("="); 
       output += splitted[1] + "\n"; 
      } 
     } 
     System.out.print("-"+output); 
    } 
    catch(IOException e) { 

    } 
} 
+0

在while循環中使用'hasNext()'來確定是否還有剩下的線...... – ha9u63ar 2015-03-18 21:18:17

+0

不使用輸入而使用另一個新的Strin像線 – 2015-03-18 21:22:42

回答

2

看起來像這條線是問題,因爲它總會是真的。

if (temp.startsWith(input)) 

您需要爲從文件中讀出的行以及從用戶持有的輸入有不同的變量。嘗試是這樣的:

String fileLine; 
while((fileLine = br.readLine()) != null) 
    { 
     temp = fileLine.trim(); 
     if (temp.startsWith(input)) 
     { 
      String[] splitted = temp.split("="); 
      output += splitted[1] + "\n"; 
     } 
    } 
0

試試這個,它的工作原理:步驟:

1)使用scanner 2)使用bufferedreader 3)split每一行使用"-"delimiter 4)閱讀file閱讀inputcomparefirstline的字符與input 5)if first character等於input然後print the associated value,preceded by a "-"

import java.io.BufferedReader; 
import java.io.FileReader; 
import java.io.FileNotFoundException; 
import java.io.IOException; 
import java.io.File; 
import java.util.Scanner; 
class myRead{ 
    public static void main(String[] args) throws FileNotFoundException, IOException { 
     System.out.println("Type in your word"); 
     Scanner scanner = new Scanner(System.in); 
     String input = scanner.next(); 
     long numberOfLines = 0; 
     BufferedReader myReader = new BufferedReader(new FileReader("test.txt")); 
     String line = myReader.readLine(); 
      while(line != null){ 
       String[] parts = line.split("="); 
       if (parts[0].trim().equals(input.trim())) { 
       System.out.println("-"+parts[1]); 
       } 
       line = myReader.readLine(); 
      } 
} 
} 

OUTPUT(取決於INPUT):

- Ant 
- Bird 
- Cat 
+0

太棒了!它完美的作品,非常感謝 – tiberian 2015-03-18 23:21:35

+0

很高興我可以幫助。請接受我的回答,因爲它有效:http://meta.stackexchange。com/questions/5234/how-does-accepting-an-answer-work – adrCoder 2015-03-19 09:26:05

0

您可以使用ScanneruseDelimiter()方法拆分輸入文本

scanner.useDelimiter("(.)*="); // Matches 0 or more characters followed by '=', and then gives you what is after `=` 

下面的代碼是我一直在嘗試IDEONE(http://ideone.com/TBwCFj

Scanner s = new Scanner(System.in); 
     s.useDelimiter("(.)*="); 
     while(s.hasNext()) 
     { 
      String ss = s.next(); 
      System.out.print(ss); 

     } 
/** 
    * Output 
    * 
    */ 
    Ant 
    Bat 
+0

感謝您的幫助 – tiberian 2015-03-18 23:30:32

0

你需要首先用新行「\ n」分割文本文件(假設在每個「A = Ant」,「B = Bird」,「C = Cat」聲明後以新行開頭),然後找到輸入的字符並按照「=」進一步分割。

因此,您需要兩行字符串(String []),每行一個,每行分隔一行。 「A」和「Ant」。 你非常接近。

+0

感謝您的建議 – tiberian 2015-03-18 23:22:46