gpt4 book ai didi

java - 如何使用定界符忽略大于 1 的 float ?

转载 作者:行者123 更新时间:2023-11-30 06:12:35 38 4
gpt4 key购买 nike

所以我正在读取以下内容的两列数据 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 的 float 。我'我已经尝试将文件作为扫描仪读取并使用定界符,但我不知道如何获取 token 后面的整数。请帮我。

这是我的引用代码:

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();
}

}
}

最佳答案

你可以使用 "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
}

希望这对您有所帮助。

关于java - 如何使用定界符忽略大于 1 的 float ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32706558/

38 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com