如何使用分隔符忽略大于1的浮点数?

tra*_*gel 2 java delimiter

所以我正在阅读以下的两列数据txt文件:

20 0.15

30 0.10

40 0.05

50 0.20

60 0.10

70 0.10

80 0.30

我想把第二列放入一个数组({0.15,0.10,0.05,0.2,0.1,0.1,0.3}),但我不知道如何解析大于1的浮点数.我试过在扫描仪中读取文件并使用分隔符,但我不知道如何获取进行令牌的整数.请帮我.

这是我的代码供参考:

import java.io.PrintWriter;
import java.util.Scanner;
import java.io.*;

class OneStandard {
    public static void main(String[] args) throws IOException {

        Scanner input1 = new Scanner(new File("ClaimProportion.txt"));//reads in claim dataset txt file

        Scanner input2 = new Scanner(new File("ClaimProportion.txt"));

        Scanner input3 = new Scanner(new File("ClaimProportion.txt"));


        //this while loop counts the number of lines in the file
        while (input1.hasNextLine()) {
            NumClaim++;
            input1.nextLine();
        }
            System.out.println("There are "+NumClaim+" different claim sizes in this dataset.");
            int[] ClaimSize = new int[NumClaim];

            System.out.println("      ");
            System.out.println("The different Claim sizes are:");

            //This for loop put the first column into an array
        for (int i=0; i<NumClaim;i++){
            ClaimSize[i] = input2.nextInt();
            System.out.println(ClaimSize[i]);
            input2.nextLine();
        }


        double[] ProportionSize = new double[NumClaim];
        //this for loop is trying to put the second column into an array
        for(int j=0; j<NumClaim; j++){
            input3.skip("20");
            ProportionSize[j] = input3.nextDouble();
            System.out.println(ProportionSize[j]);
            input3.nextLine();
        }

    }
}
Run Code Online (Sandbox Code Playgroud)

小智 5

你可以使用"YourString".split("regex"); 例:

String input = "20 0.15";

String[] items = input.split(" "); // split the string whose delimiter is a " "

float floatNum = Float.parseFloat(items[1]); // get the float column and parse

if (floatNum > 1){
    // number is greater than 1
} else {
    // number is less than 1
}
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助.