-1

I got a .txt file wich i load into a string with Buffered string reader. The data in the String is in this format.

20 000  5 000
50 000  6 000
60 000  7 000
80 000  8 000
90 000  9 000

I need to go trough these numbers and find the right one. I have a int variable with a number. Lets say the int variable is "24000". Then the right answer from the list would be 20 000. As this is the closest match. I Then need to extract the number after 20 000 (5 000) and write it to a separate int. I cannot change the format of the numbers, because there are like a million of them formated in a .txt file like this.

Summary : UserNumber -> Find closest match from the left side -> Store right side number to an int. Any ideas on how i can manage this?

4

2 回答 2

0

这为您提供了每行的数字:

String[] parts = null;
Long first = null;
Long second = null;

// loop begins
parts = line.split("\\s{1,2}");
first = Long.parseLong(parts[0] + parts[1]);
second = Long.parseLong(parts[2] + parts[3]);

// logic

// loop ends

请注意,括号组之间有一个空格。

于 2013-02-14T07:17:28.593 回答
0

如果分隔符的数量始终保持不变并且列表始终是排序的,则可以使用 String.split() 它将返回长度为 5 的 String[]。将 [0] 和 [1] 值解析为 Integer 会有所帮助您在比较数字并找到最接近的匹配项。[3] 和 [4] 值会为您提供所需的输出。

我写了两种方法。阅读该行后,将字符串相应地传递给这些方法

//to get the first part of the string as number
private static int checkedVal(String readString) {
    String[] splitString = readString.split(" ");
    return Integer.parseInt(splitString[0] + splitString[1]);
}
//to get the last part of the string as number
private static int correspondingVal(String readString) {
    String[] splitString = readString.split(" ");
    return Integer.parseInt(splitString[3] + splitString[4]);
}

这里的问题是错误的行/分隔符模式导致程序有缺陷。但如果你确定这不会发生,瞧!

于 2013-02-14T05:21:26.917 回答