Java 如何在while循环中使用split方法进一步拆分从txt文件读入的信息?

Java 如何在while循环中使用split方法进一步拆分从txt文件读入的信息?,java,split,filereader,Java,Split,Filereader,例如,文本文件中的一行是:Richard,RL#17 我将数据分割成一个while循环,我可以成功地获得两条独立的信息 现在我该怎么做才能进一步拆分字段[0]——字符串——这样我就可以将“Richard”和“RL”分开。我又要分手了,这次是用“,” File inputFile=新文件(“personal.txt”); FileReader in=新的FileReader(inputFile); BufferedReader INFOROMFILE=新的BufferedReader(in); S

例如,文本文件中的一行是:
Richard,RL#17

我将数据分割成一个while循环,我可以成功地获得两条独立的信息

现在我该怎么做才能进一步拆分字段[0]——字符串——这样我就可以将“Richard”和“RL”分开。我又要分手了,这次是用“,”

File inputFile=新文件(“personal.txt”);
FileReader in=新的FileReader(inputFile);
BufferedReader INFOROMFILE=新的BufferedReader(in);
String line=infomfile.readLine();
int-cnt=0;
while(行!=null)
{
字符串[]字段=line.split(“#”);
int age=Integer.parseInt(字段[1]);
工人[cnt]=新员工(字段[0],年龄);
cnt++;
line=infomfile.readLine();
}
close();
对于(int i=0;i
因此,我可以成功地在中读取数据,并使用配对对象类显示数据。现在,我想知道我会进一步将“Richard,RL#17”的名字分成两个独立的过去。我可以再次使用拆分方法吗?如果可以,如何使用

我曾尝试构建另一个while循环,但我不确定如何引用字段[0]——在本例中是文本文件中的名称——并将其拆分。我试着做另一个节目,但没有成功。另一个方案如下:

     File inputFile = new File ("placeHolder.txt");
     FileReader inFile = new FileReader (inputFile);

     placeHolder[] lotsOfText = new placeHolder[10];

     int cnt = 0;

     for(int i = 0; i < lotsOfText.length; i++)
     {
        String line = inFile.readLine();
        while(line != null)
        {
         String[] field = line.split(",");
         field = line.split("#");
         lotsOfText[cnt] = new placeHolder(field[0],field[1],field[2],field[3]);


         cnt++;
        }   
     }  

     System.out.println(field[0] + " " + field[1] + " " + field[2] + " " + field[3]);

   }
File inputFile=新文件(“placeHolder.txt”);
FileReader infle=新文件读取器(inputFile);
占位符[]lotsOfText=新占位符[10];
int-cnt=0;
for(int i=0;i
尝试以下代码:-

String str1 = "Richard,RL#17";
String[] arr = str1.split("([,#])");
它可以让你们在一个阵型中一次射击

编辑:

    File inputFile = new File ("Personnel.txt");
    FileReader in = new FileReader (inputFile);
    BufferedReader inFromFile = new BufferedReader (in);
    // Create a scanner object which has convenient methods to read from file reader.
    Scanner s = new Scanner(inFromFile); 
    String [] field;
    String personLine;
    // Check if there is any data in the file
    while(s.hasNextLine()) {
        // read the entire line form the scanner of the file
        personLine = s.nextLine().trim();
        field = personLine.split("([,#])"); // you can put any number of delimiters within the square braces which seperate the data for person stored in the file
        // rest of your code logic
    } // keep reading till you finish all data

我想这就是你想要实现的

添加到Manjunath Anand的响应中: 如果要从文件中的一行中获取3个字符串“Richard”、“RL”、“17”,然后将其存储到对象中并打印,可以执行以下操作:

编辑:添加了一些异常处理并测试了代码:

public class Testing {

class Personnel {
    String name;
    String initials;
    int age;
    Personnel(String name, String initials, int age) {
        this.name = name;
        this.initials = initials;
        this.age = age;
    }
}

void readData() {
    File inputFile = new File ("Personnel.txt");
    FileReader in = null;
    BufferedReader inFromFile = null;
    ArrayList<Personnel> data = null;
    try {
        in = new FileReader (inputFile);
        inFromFile = new BufferedReader (in);

        // list that will hold all personnel objects
        data = new ArrayList<Personnel>();
        String line = inFromFile.readLine ();

        while (line != null) {
            String [] field = line.split("([,#])");
            String name = field[0]; // field[0] = "Richard"
            String initials = field[1]; // field[1] = "RL"
            int age = Integer.valueOf(field[2]);// field[2] = "17";
            data.add(new Personnel (name, initials, age));
            line = inFromFile.readLine();
        }
        inFromFile.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } 

    for(Personnel p : data) {
        System.out.println("Name:\t\t" + p.name);
        System.out.println("Initials:\t" + p.initials);
        System.out.println("Age:\t\t" + p.age);
        System.out.println();
    }
}

public static void main(String[] args) {
    Testing test = new Testing();
    test.readData();
}

}
公共类测试{
班级工作人员{
字符串名;
字符串首字母;
智力年龄;
人员(字符串名称、字符串首字母、整数){
this.name=名称;
this.initials=首字母;
这个。年龄=年龄;
}
}
void readData(){
文件输入文件=新文件(“personal.txt”);
FileReader in=null;
BufferedReader INFOROMFILE=null;
ArrayList数据=null;
试一试{
in=新文件读取器(inputFile);
INFOROMFILE=新的BufferedReader(in);
//包含所有人员对象的列表
数据=新的ArrayList();
String line=infomfile.readLine();
while(行!=null){
String[]字段=line.split(([,#])”;
字符串名称=字段[0];//字段[0]=“Richard”
字符串首字母=字段[1];//字段[1]=“RL”
int age=Integer.valueOf(字段[2]);//字段[2]=“17”;
数据。添加(新人员(姓名、首字母缩写、年龄));
line=infomfile.readLine();
}
close();
}catch(filenotfounde异常){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
} 
人员(p:数据){
System.out.println(“名称:\t\t”+p.Name);
System.out.println(“首字母:\t”+p.Initials);
System.out.println(“年龄:\t\t”+p.Age);
System.out.println();
}
}
公共静态void main(字符串[]args){
测试=新测试();
test.readData();
}
}

你好,Richard。首先,我们并不真的对堆栈溢出执行“紧急”操作;问题和答案有望在数年内提供有用的信息,很可能是在参与交流的人早已离开网站很久之后。其次,我们希望提问者描述他们尝试了什么,以及结果与期望的行为有何不同。请将您的问题包含在这些信息中。请添加您的代码,以便我们(希望)了解您对
字段[0]
的含义以及“两个独立部分”是什么。目前,很难猜到你已经有了什么。很抱歉,我会做推荐的编辑。这是我关于堆栈溢出的第一个问题,所以我对这些规则和规定还很陌生。有些时候我们都是初学者。请阅读,然后查看,以获得有关网站如何工作以及我们对用户的期望的更深入信息。最重要的可能是要记住,帖子通常应该是独立的(补充信息的外部参考是可以的,但不需要它们来理解帖子),投票是关于帖子的,而不是关于你的。现在的问题更好了,但我仍然觉得它缺少了一个解释,说明了你为了自己解决问题已经做了哪些努力。我需要从文本文件中读取信息。将有许多列出的名称遵循相同的模式,即在逗号后加上首字母,我需要一个循环以满足进一步的拆分。@RichardLuiz请检查编辑后的答案是否符合您的需要是的,看起来很棒。现在,我将如何显示这两个?显示循环仍然在输出屏幕上显示:Richard,RL。它打印“Richard,RL”,因为您正在这样做:工人[cnt]=新员工(字段[0],年龄);您仍在将字段[0]添加到人员对象中,而不是将名称和初始值[0]和名称和初始值[1]添加到人员对象中
public class Testing {

class Personnel {
    String name;
    String initials;
    int age;
    Personnel(String name, String initials, int age) {
        this.name = name;
        this.initials = initials;
        this.age = age;
    }
}

void readData() {
    File inputFile = new File ("Personnel.txt");
    FileReader in = null;
    BufferedReader inFromFile = null;
    ArrayList<Personnel> data = null;
    try {
        in = new FileReader (inputFile);
        inFromFile = new BufferedReader (in);

        // list that will hold all personnel objects
        data = new ArrayList<Personnel>();
        String line = inFromFile.readLine ();

        while (line != null) {
            String [] field = line.split("([,#])");
            String name = field[0]; // field[0] = "Richard"
            String initials = field[1]; // field[1] = "RL"
            int age = Integer.valueOf(field[2]);// field[2] = "17";
            data.add(new Personnel (name, initials, age));
            line = inFromFile.readLine();
        }
        inFromFile.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } 

    for(Personnel p : data) {
        System.out.println("Name:\t\t" + p.name);
        System.out.println("Initials:\t" + p.initials);
        System.out.println("Age:\t\t" + p.age);
        System.out.println();
    }
}

public static void main(String[] args) {
    Testing test = new Testing();
    test.readData();
}

}