1

我被困住了,需要你的帮助(是的,这是作业),我想做的是让我的代码读取文本文件中的内容并按特定单词输出单词。例如,我希望它输出所有以字母“g”开头的单词。

如果我没有解释清楚,这是一个伪代码:

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();
    }
}
4

2 回答 2

1

我建议使用nextLine()over next()。然后,您将使用返回布尔值的String'startsWith(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
            }
        }

您可以在此处查看更多 String 方法:http: //docs.oracle.com/javase/7/docs/api/java/lang/String.html

于 2012-09-05T18:11:42.540 回答
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();
        }
    }
于 2012-09-06T08:08:10.230 回答