2012-09-05 35 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(); 
     } 
    }