在数组中使用split()单独的字符串和Ints

时间:2014-11-20 17:42:55

标签: java arrays

我正在尝试使用split()方法将字符串和整数与数组分开,但无法使其正常工作。

我在第7行得到了Bounds Exounds Exception,它是surname = list[1];并且不明白为什么?

我最终要做的是拆分每个元素并得到整数的平均值,这样文件中的第一行文字就会读到Mildred Bush 45 65 45 67 65和我写的新文本行新文件将读取布什,米尔德里德:最终得分为x.xx。

我已经尝试了很长一段时间了,只是根本无法让它发挥作用。

我的代码:

public static void splitTest()
{
String forename, surname, tempStr, InputFileName, OutputFileName, nextLine;
    int sum = 0;
    float average;
    //forename = "Mildred";
    //surname = "Bush";
    nextLine = "";
    tempStr = "";

    String[] list = tempStr.split(" ");
    String[] list = new String[12];
    forename = list[0];
    surname = list[1];
    int[] scores = new int[5];
    scores[0] = Integer.parseInt(list[2]);
    scores[1] = Integer.parseInt(list[3]);
    scores[2] = Integer.parseInt(list[4]);
    scores[3] = Integer.parseInt(list[5]);
    scores[4] = Integer.parseInt(list[6]);

    FileReader fileReader = null;
    BufferedReader bufferedReader = null;
    FileOutputStream outputStream = null;
    PrintWriter printWriter = null;
    clrscr();

    // Prompt user for input filename
    System.out.println("Please enter the name of the file that is to be READ (e.g. details.txt) : ");
    InputFileName = Genio.getString();

    // Prompt user for output filename
    System.out.println("Please enter the name of the file that is to be WRITTEN TO (e.g. newDetails.txt) : ");
    OutputFileName = Genio.getString();
    try {
        // Open input file
        fileReader = new FileReader(InputFileName);
        bufferedReader = new BufferedReader(fileReader); 

        // Open output file
        outputStream = new FileOutputStream(OutputFileName);
        printWriter = new PrintWriter(outputStream);

        // Read a line
        tempStr = bufferedReader.readLine();

        // While there are lines in the input file
        while (tempStr != null) {
            String[] list;
            list = tempStr.split(" ");
            surname = list[0];
            forename = list[1];
            int[] scores = new int[5];
            scores[0] = Integer.parseInt(list[2]);
            scores[1] = Integer.parseInt(list[3]);
            scores[2] = Integer.parseInt(list[4]);
            scores[3] = Integer.parseInt(list[5]);
            scores[4] = Integer.parseInt(list[6]);

            for(int i=0; i < scores.length; i++){
                sum = sum + scores[i];

                average = (float)sum/scores.length;
                // Print it to screen
                System.out.println(tempStr);

                // Write it to the output file + a new-line
                printWriter.write(tempStr +"\n");

                // Read a line
                tempStr = bufferedReader.readLine();

                System.out.println("\n\nYOUR NEW FILE DATA IS DISPLAYED ABOVE!\n\n");
                pressKey();

                // Close the input file
                bufferedReader.close();

                // Close the output file
                printWriter.close();   
            }
        }

它所说的Genio,这是一个处理用户输入的类。谢谢!

1 个答案:

答案 0 :(得分:3)

你的问题是

String[] list = tempStr.split(" ");
String[] list = new String[12];

将其更改为

String[] list = tempStr.split(" ");

删除String[] list = new String[12];

在这里你分裂了刺痛,然后当你String[] list = new String[12];时你覆盖了数组的值!

另外

while (tempStr != null) {

是一个无限循环。你需要解决这个问题..

当您选择拆分时,在评论tempStr = "";中发布这将为您提供长度为1的字符串,您应该将其更改为tempStr = "Mildred Bush 45 65 45 67 65";


编辑:

public static void main(String [] args){
    String forename, surname, tempStr, InputFileName, OutputFileName, nextLine;
    int sum = 0;
    float average;
    nextLine = "";
    tempStr = "Mildred Bush 45 65 45 67 65";

    String[] list = tempStr.split(" ");
    forename = list[0];
    surname = list[1];
    int[] scores = new int[5];
    scores[0] = Integer.parseInt(list[2]);
    scores[1] = Integer.parseInt(list[3]);
    scores[2] = Integer.parseInt(list[4]);
    scores[3] = Integer.parseInt(list[5]);
    scores[4] = Integer.parseInt(list[6]);
    System.out.println("Forename : "+ forename);
    System.out.println("Surname : "+ surname);
    System.out.print("Scores : ");
    for(int eachInt : scores){
        sum+=eachInt;
        System.out.print(eachInt+" ");
    }
    System.out.println();
    System.out.println("Average : " + sum/scores.length);
}

输出:

Forename : Mildred
Surname : Bush
Scores : 45 65 45 67 65 
Average : 57