2015-09-18 86 views
2

我使用了兩個BufferedReaders,一個讀取文檔,另一個獲取從用戶搜索字符串的輸入,請注意here。這裏是到目前爲止的代碼:使用BufferedReaders循環搜索字符串?

import java.io.*; 
import java.util.*; 

public class Ejercicio6 { 

    public static void main(String[] args) { 

     Character answer = 'S'; 
     boolean exit = false; 
     String name; 
     String line; 
     Scanner sc = new Scanner (System.in); 
     boolean found = false; 

     File file = new File ("/test/Ejercicio6/nombres.txt"); 

     try { 

      do{ 

       exit = false; 
       FileInputStream fis = new FileInputStream(file); 
       BufferedReader readFile = new BufferedReader(new FileReader(file)); 
       BufferedReader userInput = new BufferedReader(new InputStreamReader(System.in)); 
       System.out.println("Search a name, I'll tell you if it's found:"); 
       name = userInput.readLine(); 
       while ((line = readFile.readLine()) != null && found == false){ 
        if(line.equals(name)) { 
         found = true; 
        }else 
         found = false; 
       } 

       if (found == true) 
        System.out.println("I have found the name " +name+ " in the file " +file.getName()); 
       if (found == false) 
         System.out.println("Can't find the name"); 
       fis.getChannel().position(0); 
       fileRead = new BufferedReader(new InputStreamReader(fis)); 

       System.out.println("Do you want to try again? (Y/N)"); 
       answer = sc.nextLine().toUpperCase().charAt(0); 
       if (answer =='S'){ 

        exit = false; 
       }else 

        exit = true; 
       fileRead.close(); 
      }while (exit == false); 

//  }catch (FileNotFoundException e) { 
//   e.printStackTrace(); 
     }catch (IOException e) { 
      e.printStackTrace(); 

     } 

    } 

} 

上有文件3名的名字,但我總是得到「名發現」打印,無論是輸入匹配與否。我試圖弄清楚getChannel()和緩衝區清理如何告知here,但我遇到了很多麻煩。我錯過了什麼?

回答

2

在打印匹配或不匹配的名稱後,您需要將找到的標誌重新設置爲false。 只需添加

found = false;

fis.getChannel().position(0);

+0

簡單的解決方案,如我所料。非常感謝,像魅力:) – wickedchild

1
if (line.equals(name)) { 
    found = true; 
} else 
    found = false; 

可以簡單求:

found = line.equals(name); 

恰到好處之前移動boolean found = false; while循環

package com.company; 

import java.io.*; 
import java.util.Scanner; 

public class Ejercicio6 { 
    public static void main(String[] args) { 
     Character answer; 
     boolean exit; 
     String name; 
     String line; 
     Scanner sc = new Scanner(System.in); 
     File file = new File("file.txt"); 
     try { 
      do { 
       FileInputStream fis = new FileInputStream(file); 
       BufferedReader readFile = new BufferedReader(new FileReader(file)); 
       BufferedReader userInput = new BufferedReader(new InputStreamReader(System.in)); 
       System.out.println("Search a name, I'll tell you if it's found:"); 
       name = userInput.readLine(); 
       boolean found = false; 
       while ((line = readFile.readLine()) != null && found == false) { 
        found = line.equals(name); 
       } 
       if (found == true) 
        System.out.println("I have found the name " + name + " in the file " + file.getName()); 
       if (found == false) 
        System.out.println("Can't find the name"); 
       fis.getChannel().position(0); 
       BufferedReader fileRead = new BufferedReader(new InputStreamReader(fis)); 
       System.out.println("Do you want to try again? (Y/N)"); 
       answer = sc.nextLine().toUpperCase().charAt(0); 
       exit = answer != 'S'; 
       fileRead.close(); 
      } while (exit == false); 
//  }catch (FileNotFoundException e) { 
//   e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 
}