我被卡住了,需要您的幫助(是的,它是作業),我正在嘗試做的是讓我的代碼讀取文本文件中的內容並輸出用特定詞語來表達這些詞。例如,我希望它輸出以字母「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();
}
}
個人名字,姓氏和yearinfo都在一行嗎? –
嗨Moncadad,是的,每個姓名,年份和年份信息都在同一行上。對不起,如果我沒有提到這一點。 – user1649816