Java:使用文本文件重新排列令牌和删除字符

Java:使用文本文件重新排列令牌和删除字符,java,regex,loops,java.util.scanner,Java,Regex,Loops,Java.util.scanner,我正在尝试获取一个文本文件,该文件包含一个随年龄变化的人名和姓氏列表,并将其重新排列,以便控制台输出将从46 Richman,Mary a.转到Mary a.Richman 46。然而,在我尝试这样做的过程中,我遇到了一些问题(如下所示),我不明白为什么会发生这些问题(之前的情况更糟) 我真的很感谢你的帮助 文本文件: 75 Fresco, Al 67 Dwyer, Barb 55 Turner, Paige 108 Peace, Warren 46 Richman, Mary A. 37 Wa

我正在尝试获取一个文本文件,该文件包含一个随年龄变化的人名和姓氏列表,并将其重新排列,以便控制台输出将从
46 Richman,Mary a.
转到
Mary a.Richman 46
。然而,在我尝试这样做的过程中,我遇到了一些问题(如下所示),我不明白为什么会发生这些问题(之前的情况更糟)

我真的很感谢你的帮助

文本文件:

75 Fresco, Al
67 Dwyer, Barb
55 Turner, Paige
108 Peace, Warren
46 Richman, Mary A.
37 Ware, Crystal
83 Carr, Dusty
15 Sledd, Bob
64 Sutton, Oliver
70 Mellow, Marsha
29 Case, Justin
35 Time, Justin
8 Shorts, Jim
20 Morris, Hugh
25 Vader, Ella
76 Bird, Earl E.
我的代码:

import java.io.*;
import java.util.*;

public class Ex2 {
    public static void main(String[] args) throws FileNotFoundException {
        Scanner input = new Scanner(new File("people.txt"));
        while (input.hasNext()) { // Input == people.txt
            String line = input.next().replace(",", "");
            String firstName = input.next();
            String lastName = input.next();
            int age = input.nextInt();

            System.out.println(firstName + lastName + age);

        }
    }
}
控制台输出错误:(它是如何抛出未知源错误的?)

目标控制台输出:

Al Fresco 75
Barb Dwyer 67
Paige Turner 55
Warren Peace 108
Mary A. Richman 46
Crystal Ware 37
Dusty Carr 83
Bob Sledd 15
Oliver Sutton 64
Marsha Mellow 70
Justin Case 29
Justin Time 35
Jim Shorts 8
Hugh Morris 20
Ella Vader 25
Earl E. Bird 76

通过使用
input.nextLine()
实际阅读,可以避免此问题并简化逻辑,如下面带注释的代码所示:

while (input.hasNextLine()) {
      String line = input.nextLine();//read next line

      line = line.replace(",", "");//replace , 
      line = line.replace(".", "");//replace .

      String[] data = line.split(" ");//split with space and collect to array

      //now, write the output derived from the split array
      System.out.println(data[2] + " " + data[1] + " " + data[0]);
}

通过使用
input.nextLine()
实际阅读,可以避免此问题并简化逻辑,如下面带注释的代码所示:

while (input.hasNextLine()) {
      String line = input.nextLine();//read next line

      line = line.replace(",", "");//replace , 
      line = line.replace(".", "");//replace .

      String[] data = line.split(" ");//split with space and collect to array

      //now, write the output derived from the split array
      System.out.println(data[2] + " " + data[1] + " " + data[0]);
}

这将确保名字包含中间的首字母

while (input.hasNext()) 
{
    String[] line = input.nextLine().replace(",", "").split("\\s+");
    String age = line[0];
    String lastName = line[1];
    String firstName = "";
    //take the rest of the input and add it to the last name
    for(int i = 2; 2 < line.length && i < line.length; i++)
        firstName += line[i] + " ";

    System.out.println(firstName + lastName + " " + age);

}
while(input.hasNext())
{
String[]line=input.nextLine().replace(“,”,“).split(\\s+”);
字符串年龄=行[0];
字符串lastName=行[1];
字符串firstName=“”;
//获取其余输入并将其添加到姓氏
对于(int i=2;2
这将确保名字包含中间的首字母

while (input.hasNext()) 
{
    String[] line = input.nextLine().replace(",", "").split("\\s+");
    String age = line[0];
    String lastName = line[1];
    String firstName = "";
    //take the rest of the input and add it to the last name
    for(int i = 2; 2 < line.length && i < line.length; i++)
        firstName += line[i] + " ";

    System.out.println(firstName + lastName + " " + age);

}
while(input.hasNext())
{
String[]line=input.nextLine().replace(“,”,“).split(\\s+”);
字符串年龄=行[0];
字符串lastName=行[1];
字符串firstName=“”;
//获取其余输入并将其添加到姓氏
对于(int i=2;2
使用input.nextLine().replace(“,”,”)你真的应该把整行内容分成空格,然后根据需要把每一部分都取出来使用input.nextLine().replace(“,”,”)你真的应该把整行分割成空格,然后根据需要把每一部分都分割掉。我注意到一件事,我不明白的是,如何才能得到中间的首字母及其要移动的句点?
line.replace(“.”,”)
将如上图所示执行此操作。我注意到一件事,但我不知道如何获得中间首字母及其移动周期?
行。替换(“.”,”)
将如上图所示执行此操作