将字符串数组解析为FIle

时间:2013-12-02 08:04:25

标签: java arrays parsing

好吧,我试图制作一个程序,从文件中读取信息,然后将信息写入另一个文件。

我从2列读取第一列是整数(Team#)第二列是字符串(成员名称)

1 Sam
3 Bob
6 Jill
3 Mike
1 Terra
1 Juice
6 Tom
6 Lucy
3 Dude

然后我必须进入第三个实例并输出个人的名字,所以它看起来像这样

Team    Member
1       Juice
3       Dude
6       Lucy

我遇到了尝试将文本读入数组以便输出的问题,我正在尝试使用Parse String

我的代码

package team;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;


public class Team {

public static void main(String[] args) throws FileNotFoundException {


    //Output Please wait
    System.out.println("Team Leader Started. Please wait....");


    //Add Read and Write File locations
    File inFile = new File("C:\\Users\\Christ\\Documents\\NetBeansProjects\\Team\\src\\team\\read.txt");
    File outFile = new File("C:\\Users\\Christ\\Documents\\NetBeansProjects\\Team\\src\\team\\outputfile.txt");

    // Initialise Arrays
    int[] team = new int[99];

    String[] name = new String[99];


    // Scanner
    Scanner sc = new Scanner(inFile);
    sc.nextLine();  // move to second line assuming file is not empty.

    // While Loop
    while(sc.hasNextLine()) {
        String s = sc.nextLine().trim();
        String[] splitStr = s.split(" ");
        team[Integer.parseInt(splitStr[0])-1] += Integer.parseInt(splitStr[1]);
        name[String.parseString(splitStr[0])-1]++;
    }


    PrintWriter outFileWriter = new PrintWriter(outFile);

    outFileWriter.println("Team Name");



   // For loop 
    for(int i=0;i<99;i++) {

    // Team    
    int t=i+1;

    // Member
    String m = name[i];

    // Output to File
    outFileWriter.println(t + " "+" "+ " " + m);
    }
    outFileWriter.close();


//Output Completed file, reference output file for sucees
System.out.println("Team Leader Completed Successfully");

}
}

有人可以告诉我哪里出错了吗?我不希望最终结果只是能够输出团队编号,然后将成员名称输出到我的输出文件。

请帮助^ _ ^

2 个答案:

答案 0 :(得分:1)

我会使用Map<Integer, ArrayList<String>>将您的团队成员存储到相应的团队中。

它允许您存储键/值对并维护每个团队的动态成员列表。

public static void main(String [] args) throws FileNotFoundException{
    System.out.println("Team Leader Started. Please wait....");

    Map<Integer, ArrayList<String>> map = new HashMap<Integer, ArrayList<String>>();

    //Add Read and Write File locations
    File inFile = new File("C:\\Users\\Christ\\Documents\\NetBeansProjects\\Team\\src\\team\\read.txt");
    File outFile = new File("C:\\Users\\Christ\\Documents\\NetBeansProjects\\Team\\src\\team\\outputfile.txt");

    // Scanner
    Scanner sc = new Scanner(inFile);

    // While Loop
    while(sc.hasNextLine()) {
        String s = sc.nextLine();
        String[] splitStr = s.split(" ");
        Integer id = Integer.parseInt(splitStr[0]);
        String name = splitStr[1];
        List<String> list = map.get(id);
        if(list == null)
            map.put(id, new ArrayList<>(Arrays.asList(name)));
        else {
            list.add(name);
        }
    }
    PrintWriter outFileWriter = new PrintWriter(outFile);
    outFileWriter.println("Team Name");
    // For loop 
    for(Map.Entry<Integer, ArrayList<String>> entry : map.entrySet()){
        outFileWriter.write(entry.getKey()+"\t"+entry.getValue().toString()+"\n");
    }       
    outFileWriter.close();
    //Output Completed file, reference output file for sucees
    System.out.println("Team Leader Completed Successfully");

}

输出:

Team Name
1   [Sam, Terra, Juice]
3   [Bob, Mike, Dude]
6   [Jill, Tom, Lucy]

答案 1 :(得分:0)

如果您知道输入人数,那么在输入文件中添加一行表示长度将允许数组大小为最小要求,这也可以解决100多人的问题

像:  #9 1山姆 3鲍勃 6吉尔 3迈克 1 Terra 1汁 6汤姆 6露西 3老兄