gpt4 book ai didi

java - 提取整数部分值,它只是给定文件中的 ID

转载 作者:行者123 更新时间:2023-12-02 03:52:47 26 4
gpt4 key购买 nike

我编写了一个程序来提取文件中的所有整数值并找到重复的整数。但我只想要那些类似于 ID="****.."/id="****.." 的整数值。我不想考虑 "dependsOnPresenceOf" 值,无论它是什么。

我的文件是:例如

  <line id="24867948" dependsOnPresenceOf="7417840">
<element text="Card Balance " id="18829409" dependsOnPresenceOf="28696224" />
<line id="2597826922" dependsOnPresenceOf="200114712343">
<methodElement fixedWidth="17" precededBySpace="false" id="418710522">
<line id="24867948" dependsOnPresenceOf="10565536">
<element text=" Cert. Number:" id="23917950" dependsOnPresenceOf="10565536" />
<line id="24867948" dependsOnPresenceOf="10565536">
<element text=" Cert. Number:" id="23917950" dependsOnPresenceOf="10565536" />

我的程序如下,我编写的程序仅提取整数值:

public class DuplicateIDPicker {

protected static final Logger logger = Logger
.getLogger(com.aspire.pos.DuplicateIDPicker.class);

public static String finalContent = "";

public static void main(String[] args) throws FileNotFoundException {

String content = "";
/* Set location of the file as format below */
String path = "D://TASK/DuplicateFinder/OriginalFile/";
/* Set file name to be evaluate with extension */
String fileName = "SSLItems.bpt";

File f = new File(path.concat(fileName));

try {
content = readFile(f);
String extractedInteger = content.replaceAll("\\D+", " ");
String[] arrayOfID = findAllIDInArray(extractedInteger);

System.out.println("***********************");

HashSet<String> set = new HashSet<String>();
HashSet<String> newSet = new HashSet<String>();
System.out.println("Duplicate ID's found :");
for (String arrayElement : arrayOfID) {
if (!set.add(arrayElement)) {
// System.out.println("Duplicate Element is : "+arrayElement);
newSet.add(arrayElement);
}
}

System.out.println("-----------------------");
/* here are all Duplicate Id */
System.out.println(newSet);

} catch (IOException e) {
logger.error(e.getMessage());
}

}

@SuppressWarnings("resource")
public static String readFile(File f) throws IOException {
String data = "";

FileReader fr = new FileReader(f);
BufferedReader br = new BufferedReader(fr);

while ((data = br.readLine()) != null) {
// Print the content on the console
finalContent = finalContent + data;
}

return finalContent;
}

public static String[] findAllIDInArray(String str) {
String[] value = str.split(" ");
return value;
}
}

最佳答案

您可以执行 content.replaceAll("dependsOnPresenceOf=\"\\d+\"", ""); 删除这些不需要的字符串

关于java - 提取整数部分值,它只是给定文件中的 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35740866/

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