我正在尝试做一个程序,这是我到目前为止所做的:
import java.util.Scanner;
import java.io.*;
public class WebberProject1
{
public static void main(String[] args) throws IOException
{
Scanner scanner = new Scanner(new File("portlandvip.txt"));
(scanner.hasNext())
{
String firstName = scanner.next();
String lastName = scanner.next();
System.out.println(" " + firstName + " " + lastName);
}
}
}
此程序的输出如下所示:
Loras Tyrell
5 Club
Margaery Tyrell
8 Box
Roslin Frey
2 Box
Sansa Stark
2 Club
Jon Snow
,文件如下所示:
Loras Tyrell 5 Club
Margaery Tyrell 8 Box
Roslin Frey 2 Box
Sansa Stark 2 Club
Jon Snow 5 Club
Edmure Tully 3 Box
Joffrey Baratheon 20 Court
Stannis Baratheon 4 Club
Jaime Lannister 2 Box
Cersei Lannister 1 Court
Beric Dondarrion 8 Court
Balon Greyjoy 16 Box
Olenna Tyrell 4 Court
Mace Tyrell 5 Box
Tyrion Lannister 2 Club
Sandor Clegane 2 Court
Gregor Clegane 6 Club
Samwell Tarly 3 Club
Petyr Baelish 6 Court
我的问题是,如何从程序输出中删除“5俱乐部”,“8盒”等部分?
答案 0 :(得分:0)
你可以读取整行并在空格上分割该行。打印您想要的信息。
如下所示
(scanner.hasNextLine())
{
// Split the line and return array of String
String[] line = scanner.nextLine().split("\\s+");
// get you info
// print your info
}
答案 1 :(得分:0)
在阅读名字和姓氏后,您可以使用nextLine()
(scanner.hasNext()){
String firstName = scanner.next();
String lastName = scanner.next();
System.out.println(" " + firstName + " " + lastName);
scanner.nextLine();
}