2015-10-08 74 views
-1

我有我的名字4,票據,戴夫,邁克創建一個文件names.txt行號名稱from.txt文件,andjim我不能顯示在控制檯

我可以輸入文件名稱,我可以輸入名稱進行搜索,例如上面的dave,然後控制檯應該返回「dave出現在Names.txt的第2行」。相反,它會返回「dave不存在」,如果它不是這四個名字中的一個,這將是正確的。我在下面的while循環中犯了什麼錯誤?

public class names { 
    public static void main(String[] args) throws IOException { 
     String friendName; // Friend's name  

     // Create a Scanner object for keyboard input. 
     Scanner keyboard = new Scanner(System.in); 

     // Get the filename. 
     System.out.print("Enter the filename: "); 
     String filename = keyboard.nextLine(); 

     // Open the file. 
     File file = new File(filename); 
     Scanner inputFile = new Scanner(file); 

     // Get the name of a friend. 
     System.out.print("Enter name to search: "); 
     friendName = keyboard.nextLine().toLowerCase(); 

     int lineNumber = 1;   

     while (inputFile.hasNextLine()) { 
      if ("friendName".equals(inputFile.nextLine().trim())) { 
       // found 
       String line = inputFile.nextLine(); 
       System.out.println("friendName" + " appears on line " + lineNumber + " of Names.txt"); 
       lineNumber++; 
       //break; 
      } else { 
       // not found 
       System.out.println(friendName + " does not exist. "); 
       break; 
      } 
     } 

     // Close the file. 
     inputFile.close(); 
    } 
} 
+0

爲什麼你要比較字符串「friendName」而不是字符串變量friendName。 –

回答

1

friendName刪除引號,並使用你從文件中讀取的實際變量:

int lineNumber = 1; 
booelan found = false; 

while (inputFile.hasNextLine()) { 
    String nextLine = inputFile.nextLine().trim(); 
    if (friendName.equals(nextLine)) { 
     // found 
     found = true; 
     break;  // name is found, no point in searching any further 
    } 

    lineNumber++;  // always increment the line number 
} 

if (found) { 
    System.out.println(friendName + " appears on line " + lineNumber + " of Names.txt"); 
} 
else { 
    System.out.println(friendName + " does not exist. "); 
} 

我也改變你用你Scanner的方式。在您的原始代碼中,在與朋友姓名匹配的情況下,您撥打Scanner.nextLine()兩次。這將導致掃描儀提前兩個線,這是不是你想要的。

+0

@Jackwhyski請檢查給出的答案,謝謝。 –

0
package com.stackoverflow; 

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

public class Names 
{ 
    public static void main(String[]args) throws IOException 
    { 
     String friendName; // Friend's name 

     // Create a Scanner object for keyboard input. 
     Scanner keyboard = new Scanner(System.in); 

     // Get the filename. 
     System.out.print("Enter the filename: "); 
     String filename = keyboard.nextLine(); 

     // Open the file. 
     File file = new File(filename); 
     Scanner inputFile = new Scanner(file); 

     // Get the name of a friend. 
     System.out.print("Enter name to search: "); 
     friendName = keyboard.nextLine().toLowerCase(); 

     int lineNumber = 0; 
     Boolean isFound = false; 
     while(inputFile.hasNextLine()){ 
      lineNumber++; 
      String line = inputFile.nextLine(); 
      if(line.trim().contains(friendName)){ 
       System.out.println(friendName + " appears on line " + lineNumber + " of Names.txt"); 
      } 
     } 
     if(!isFound){ 
      System.out.println("given friend name "+friendName+" not exists in the file"); 
     } 
     // Close the file. 
     inputFile.close(); 
    } } 
+0

解決了您提供的程序中存在的問題,現在嘗試使用上面發佈的示例程序。 –

+0

糾正了計數器邏輯中的缺陷。一個觀察結果是當前程序搜索名稱區分大小寫。 –