【问题标题】:Using split() separate strings and Ints in an array在数组中使用 split() 分隔字符串和 Ints
【发布时间】:2014-11-20 17:42:54
【问题描述】:

我正在尝试使用 split() 方法将字符串和整数从数组中分离出来,但无法正常工作。

我在第 7 行得到了 Index Of Bounds Exception,它是 surname = list[1];,但不知道为什么?

我最终要做的是拆分每个元素并获取整数的平均值,因此文件中的第一行文本将显示为 Mildred Bush 45 65 45 67 65 和我写入的新文本行新文件将显示为 Bush,Mildred:最终得分为 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,这是一个处理用户输入的类。谢谢!

【问题讨论】:

    标签: java arrays


    【解决方案1】:

    你的问题是

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

    改成

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

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

    在这里,您正在拆分 sting,然后当您执行 String[] list = new String[12]; 时,您正在覆盖数组的值!

    还有

    while (tempStr != null) {
    

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

    正如在 cmets 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
    

    【讨论】:

    • 不起作用,因为 tempStr 被声明为空字符串。所以如果用空格分割,结果数组的长度只有1。
    • @La-comadreja 我认为这只是为了描述问题,实际上并非如此。
    • 我试过了,但我仍然收到错误。另一件事,我解析整数的地方,应该是在开始还是在 while 循环中?
    • 您希望它在 while 循环中或之后。通常,代码从上到下执行,您需要在解析所有值之前输入和分配值。我建议通过调试运行代码并单步执行每一行以查看它是如何工作的。
    • 谢谢!非常感谢!
    猜你喜欢
    • 2020-09-26
    • 1970-01-01
    • 2017-10-02
    • 2013-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-17
    相关资源
    最近更新 更多