[英]How to read a textfile in java?
How to select numbers from a line by line text file that has both text and numbers? 如何从具有文本和数字的逐行文本文件中选择数字?
For example: 例如:
[10] begin0-1-selp-2-yelp-25-jelp-21-hi-35-ou
I want to only have 0 1 2 25 21 35
printed out without the [10]
. 我只想打印
0 1 2 25 21 35
而没有[10]
。 But I keep getting 10012252135
. 但是我一直得到
10012252135
。
This is my code 这是我的代码
try {
Scanner scan = new Scanner(file);
while (scan.hasNextLine()) {
String i = scan.nextLine();
String final_string = "";
for (int j = 0; j < i.length(); j++) {
char myChar = i.charAt(j);
if (Character.isDigit(myChar)) {
final_string = final_string.concat(Character.toString(myChar));
}
}
System.out.println(final_string);
}
scan.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
String tt = "[10] begin0-1-selp-2-yelp-25-jelp-21-hi-35-ou";
tt = tt.replaceAll("\\[.*\\]",""); // IT get rid of any [AnyNumber]
tt = tt.replaceAll("\\D+",""); // It get rid of any char that is not a letter
System.out.println(tt);
I made a regex I can't make it one line but the output is the disered. 我做了一个正则表达式,我不能使其一行,但输出是不正确的。 OutPut
产量
012252135
I like the answer from Reaz Murshed, but just in case you have multiple occurenses of number enclosed in "[]" you might filter those by remembering if you are currently in just an enclosed scope or not: 我喜欢Reaz Murshed的答案,但是如果万一您在“ []”中包含多个数字,可以通过记住您当前是否处于封闭范围内来对它们进行过滤:
char NON_NUMERIC_SCOPE_START = '[';
char NON_NUMERIC_SCOPE_END = ']';
try {
Scanner scan = new Scanner(file);
while (scan.hasNextLine()) {
String i = scan.nextLine();
String final_string = "";
boolean possibleNumericScope = true;
for (int j = 0; j < i.length(); j++) {
char myChar = i.charAt(j);
if (myChar == NON_NUMERIC_SCOPE_START) {
possibleNumericScope = false;
} else if (myChar == NON_NUMERIC_SCOPE_END && !possibleNumericScope) {
possibleNumericScope = true;
} else if (Character.isDigit(myChar) && possibleNumericScope) {
final_string = final_string.concat(Character.toString(myChar));
}
}
System.out.println(final_string);
}
scan.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
I think the first portion is the line number which can be easily ommited by splitting your String
by space. 我认为第一部分是行号,可以通过按空格分隔
String
来轻松省略该行号。 Please try the following code. 请尝试以下代码。
String i = "[10] begin0-1-selp-2-yelp-25-jelp-21-hi-35-ou";
String final_string = "";
// Split the String with space to remove the first portion which
// might be indicating the line number or something like that.
String[] splittedArray = i.split(" ");
// Then just run the loop in the second item of the split
// `String` array.
int contCount = 0;
for (int j = 0; j < splittedArray[1].length(); j++) {
char myChar = splittedArray[1].charAt(j);
if (Character.isDigit(myChar)) {
contCount = 0;
final_string = final_string.concat(Character.toString(myChar));
} else {
if (contCount == 0)
final_string = final_string + " ";
contCount++;
}
}
System.out.println(final_string);
I added a line to get rid of [line-number] before the loop: 我在循环之前添加了一行以摆脱[line-number]:
i = i.substring(i.indexOf("]")+1);
for (int j = 0; j < i.length(); j++) {
This should do the trick. 这应该可以解决问题。
If you always have the [10], then you can just edit your final_string to be everything but the first two numbers. 如果您始终使用[10],则可以将final_string修改为除前两个数字之外的所有内容。 Change System.out.println(final_string) to System.out.println(final_string.substring(2)).
将System.out.println(final_string)更改为System.out.println(final_string.substring(2))。 Then, if you need spaces, type final_string += " ";
然后,如果需要空格,请键入final_string + =“”; in your if statement of the for loop.
在for循环的if语句中。
Try this: 尝试这个:
String y = "[133] begin0-1-selp-2-yelp-25-jelp-21-hi-35-ou";
String result = y.replaceAll("\\[[0-9]*\\]|[a-zA-Z-]*", "");
Or 要么
String y = "[133] begin0-1-selp-2-yelp-25-jelp-21-hi-35-ou";
String result = y.replaceAll("\\[[\\d]*\\]|[^\\d]", "");
Many ways to do this :) 许多方法可以做到这一点:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.