Reputation: 759
I have a code which read the content of a certain file. The file begin like this :
J-549 J-628 J-379 J-073 J-980 vs J-548 J-034 J-127 J-625 J-667\
J-152 J-681 J-922 J-079 J-103 vs J-409 J-552 J-253 J-286 J-711\
J-934 J-367 J-549 J-169 J-569 vs J-407 J-429 J-445 J-935 J-578\
I would like to store each integer in an array of size 270 (number of line) x 10 (integer at each line).
I am not using the correct regex. Here is a piece of my code :
String strLine;
int[] id = new int[10];//list ID of each line
//Read File Line By Line
while ((strLine = br.readLine()) != null) {
// Print the content on the console
strLine = strLine.replaceAll("J-", "");
strLine = strLine.replaceAll(" vs ", " ");
String[] teamString = strLine.split(" ");
for(int i=0;i<id.length;i++) {
System.out.print(id[i] + " ");
}
I was thinking of removing the "J-" and " vs " but it seems to be a bad idea. The console prints :
549 628 379 073 980 548 034 127 625 667\
152 681 922 079 103 409 552 253 286 711\
934 367 549 169 569 407 429 445 935 578\
Does someone can help me to solve my problem. Thanks!
Upvotes: 0
Views: 88
Reputation: 4071
Simply Use regex of \d+
to capture the number terms and retrieve all them to a List or array whatever you want.
Matcher m = Pattern.compile("\\d+").matcher(strLine);
while (m.find()) {
// print the number or store it in array or whatever
System.out.println(m.group());
}
Upvotes: 1
Reputation: 13652
Instead of replacing all the characters you don't want, you could just use a Regex to match those which you do want.
Pattern idPattern = Pattern.compile("J-(\\d+)");
String strLine;
int[] id = new int[10]; // list ID of each line
// read file line by line
while ((strLine = br.readLine()) != null) {
Matcher lineMatcher = idPattern.matcher(strLine);
// find and parse every ID on this line
for (int i = 0; matcher.find() && i < id.length; i++) {
String idStr = matcher.group(1); // Gets the capture group 1 "(\\d+)" - group 0 is the entire match "J-(\\d+)"
int id = Integer.parseInt(idStr, 10);
id[i] = id;
System.out.print(id + " ");
}
System.out.println();
}
The regex J-(\d+)
matches a part of string which starts with "J-" and ends in one or more numbers. The parentheses around the number create a capture group, which we can access directly instead of having to replace "J-".
If you're sure that you want to parse the IDs as integers, notice that when parsed, "073" becomes 73. Not sure if that makes a difference for you. Also, if there may be more than 10 IDs on one line, use an ArrayList
and add the IDs there instead of placing them in a fixed-size array.
Upvotes: 6
Reputation: 4420
for replacing \ use escaping char \
String s[] ="J-549 J-628 J-379 J-073 J-980 vs J-548 J-034 J-127 J-625 J-667\\"
.replace("\\", "")
.replace("J-", "")
.replace(" vs ", " ")
.split(" ");
//System.out.println(s);
int[] id = new int[10];
for(int i=0; i<s.length; i++) {
id[i] = Integer.valueOf(s[i]);
}
for(int i=0; i<id.length; i++) {
System.out.println(id[i]);
}
Result:
549
628
379
73
980
548
34
127
625
667
Upvotes: 1