2012-09-05 60 views
1

我被卡住了,需要您的幫助(是的,它是作業),我正在嘗試做的是讓我的代碼讀取文本文件中的內容並輸出用特定詞語來表達這些詞。例如,我希望它輸出以字母「g」開頭的所有單詞。使用掃描儀在.txt文件中顯示特定單詞

這裏是一個僞碼的代碼,如果我沒有解釋好:

BEGIN 

Get the initial letter from the user 

While there are more entries in the file 

Get the next personal name 

Get the next surname 

Get the next year info 

If the surname starts with the initial letter 

Output the person name, surname and year info 

End while 

END 

到目前爲止,我已經成功地完成這件事,現在我被困在那裏你輸出的名稱正確。任何幫助或教程將不勝感激。

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

public class PrimeMinisters 
{ 
    public static void main(String[] args) throws FileNotFoundException 
    { 
     // ask the user for the first letter 
     Scanner keyboard = new Scanner(System.in); 
     System.out.print("What is the first letter? "); 
     String input = keyboard.next().toLowerCase(); 
     char firstLetter = input.charAt(0); 

     // open the data file 
     File pmFile = new File ("OZPMS.txt"); 
     // create a scanner from the file 
     Scanner pmInput = new Scanner (pmFile); 

     // read one line of data at a time, processing each line 
     while(pmInput.hasNext()) 
     { 
      String names = pmInput.next(); 
      System.out.println(names); 
     } 

     // be polite and close the file 
     pmInput.close(); 
    } 
} 
+1

個人名字,姓氏和yearinfo都在一行嗎? –

+0

嗨Moncadad,是的,每個姓名,年份和年份信息都在同一行上。對不起,如果我沒有提到這一點。 – user1649816

回答

1

我推薦使用nextLine()超過next()。從此,你會再使用StringstartsWith(String stringsequence)方法,該方法返回一個布爾值來獲得所有的值開始與您所選擇的信:

while(pmInput.hasNextLine()) 
     { 

      String names = pmInput.nextLine(); 
      System.out.println(names); 
      if(names.startsWith("g")) { 
       //the name begins with letter g do whatever 
      } 
     } 

你可以看看絃樂這裏更多的方法:http://docs.oracle.com/javase/7/docs/api/java/lang/String.html

+0

謝謝大衛,我會試試看。 – user1649816

0

由於您的要求狀態是查看姓氏的第一個字母,所以在閱讀每行時標記每行(在檢查用戶輸入是否爲姓氏的第一個字母時)會更容易。假設該行的順序如上所述,姓將是標記#2(數組的索引1)。

public class PrimeMinisters 
    { 
     public static void main(String[] args) throws FileNotFoundException 
     { 
      // ask the user for the first letter 
      Scanner keyboard = new Scanner(System.in); 
      System.out.print("What is the first letter? "); 
      String input = keyboard.next().toLowerCase(); 
      char firstLetter = input.charAt(0); 

      // open the data file 
      File pmFile = new File ("OZPMS.txt"); 
      // create a scanner from the file 
      Scanner pmInput = new Scanner (pmFile); 

      // read one line of data at a time, processing each line 
      while(pmInput.hasNextLine()) 
      { 
       String names = pmInput.nextLine(); 

       // Break line into tokens. This is assuming that there are only 
       // 3 strings per line in the following order (personal name, surname, yearinfo) 
       // 
       String[] info = names.split("\\s"); 

       // Check 2nd string in line (since you are looking for the first character in 
       // the surname and not the personal name. 
       // 
       if(info[1].startsWith(input)) 
       { 
         System.out.println(info[0] + "\t" + info[1] + "\t" + info[2]); 
       } 
      } 

      // be polite and close the file 
      pmInput.close(); 
     } 
    }