使用 indexOf 从文本文件中检索 phone 数字
retrieve phone number from text file using indexOf
我有一个 text file
,其中包含 date
、time
、phone number
等详细信息。我正在尝试使用 [=18= 检索这些详细信息] indexOf
概念。但是,phone number
中的 digits
会根据调用类型发生变化。
如何改进代码以便能够从文件中检索每个 phone number
。这是我一直在尝试的:
BufferedReader reader = new BufferedReader(new FileReader(filePath));
String getIndex="";
while ((line = reader.readLine()) != null) {
line = line.replaceAll("[\s]+", " "); //remove all large spaces from the file.
/*
the dialledNo is chosen with the help of a combo box.
calculating the start and end index in order to print the dialled no.
*/
int startIndex = getIndex.indexOf(dialledNo);
int endIndex = getIndex.indexOf(" ", startIndex);
strDialedNo= (startIndex + "," + endIndex);
获取号码的代码如下:
String[] arrDialedNo = strDialedNo.split(",");
int DialedNoStart = Integer.parseInt(arrDialedNo[0]);
int DialedNoEnd = Integer.parseInt(arrDialedNo[1]);
DialedNo = line.substring(DialedNoStart, DialedNoEnd);
这是我的文本文件的样子:
0356 524 000 8861205063 12/03 18:59 00:08 01:20
0357 524 000 9902926868 12/03 20:01
0373 511 000 09886863637 13/03 11:46 01:01 02:40 S
0376 504 000 9845014967 13/03 11:46 00:11 01:20
0382 508 000 04443923200 13/03 12:04 03:11 04:80 S
0411 516 000 8884103111 13/03 16:25 01:03 01:20
这应该有效,
String[] b = line.split(" ");
String phoneNumber = null;
for (String x : b) {
boolean z = false;
if (x.length() == 10) {
char[] c = x.toCharArray();
for (char g : c) {
if (Character.isDigit(g)) {
z = true;
} else {
z = false;
break;
}
}
} else {
z = false;
}
if (z) {
phoneNumber = x;
}
}
/* Easiest way to to retrieve Phone number from Text file using Java */
package TestProject.EndToEnd;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class FileReader {
public static void main(String[] args) throws FileNotFoundException {
File file = new File("//File//Path//");
List<Object> ph = new ArrayList<Object>();
Scanner scan = new Scanner(file);
while(scan.hasNext()) {
scan.next();
if(scan.hasNextBigInteger()) {
ph.add(scan.next());
}
}
for (int i = 0; i < ph.size(); i++) {
if(ph.get(i).toString().length()==10) {
System.out.println(ph.get(i));
}
}
}
}
我有一个 text file
,其中包含 date
、time
、phone number
等详细信息。我正在尝试使用 [=18= 检索这些详细信息] indexOf
概念。但是,phone number
中的 digits
会根据调用类型发生变化。
如何改进代码以便能够从文件中检索每个 phone number
。这是我一直在尝试的:
BufferedReader reader = new BufferedReader(new FileReader(filePath));
String getIndex="";
while ((line = reader.readLine()) != null) {
line = line.replaceAll("[\s]+", " "); //remove all large spaces from the file.
/*
the dialledNo is chosen with the help of a combo box.
calculating the start and end index in order to print the dialled no.
*/
int startIndex = getIndex.indexOf(dialledNo);
int endIndex = getIndex.indexOf(" ", startIndex);
strDialedNo= (startIndex + "," + endIndex);
获取号码的代码如下:
String[] arrDialedNo = strDialedNo.split(",");
int DialedNoStart = Integer.parseInt(arrDialedNo[0]);
int DialedNoEnd = Integer.parseInt(arrDialedNo[1]);
DialedNo = line.substring(DialedNoStart, DialedNoEnd);
这是我的文本文件的样子:
0356 524 000 8861205063 12/03 18:59 00:08 01:20
0357 524 000 9902926868 12/03 20:01
0373 511 000 09886863637 13/03 11:46 01:01 02:40 S
0376 504 000 9845014967 13/03 11:46 00:11 01:20
0382 508 000 04443923200 13/03 12:04 03:11 04:80 S
0411 516 000 8884103111 13/03 16:25 01:03 01:20
这应该有效,
String[] b = line.split(" ");
String phoneNumber = null;
for (String x : b) {
boolean z = false;
if (x.length() == 10) {
char[] c = x.toCharArray();
for (char g : c) {
if (Character.isDigit(g)) {
z = true;
} else {
z = false;
break;
}
}
} else {
z = false;
}
if (z) {
phoneNumber = x;
}
}
/* Easiest way to to retrieve Phone number from Text file using Java */
package TestProject.EndToEnd;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class FileReader {
public static void main(String[] args) throws FileNotFoundException {
File file = new File("//File//Path//");
List<Object> ph = new ArrayList<Object>();
Scanner scan = new Scanner(file);
while(scan.hasNext()) {
scan.next();
if(scan.hasNextBigInteger()) {
ph.add(scan.next());
}
}
for (int i = 0; i < ph.size(); i++) {
if(ph.get(i).toString().length()==10) {
System.out.println(ph.get(i));
}
}
}
}