Java 如何从字符串中读取一个整数并将其解释为相同的整数?

Java 如何从字符串中读取一个整数并将其解释为相同的整数?,java,numbers,bufferedreader,Java,Numbers,Bufferedreader,我正在读取一个包含字符串和整数列表的文件。该文件的外观如下所示: File.txt 这里是1234 这是2568 我想把两行连续的数字相减。我正在使用BufferedReader操作读取中的行。我将字符串存储到一个数组列表中,并希望将在字符串中找到的数字彼此相减。但是我想不出一个减去它们的方法。 到目前为止,代码看起来像这样 ArrayList<String> string =new ArrayList<>(); reading =new BufferedReader

我正在读取一个包含字符串和整数列表的文件。该文件的外观如下所示:

File.txt

这里是1234 这是2568

我想把两行连续的数字相减。我正在使用BufferedReader操作读取中的行。我将字符串存储到一个数组列表中,并希望将在字符串中找到的数字彼此相减。但是我想不出一个减去它们的方法。 到目前为止,代码看起来像这样

 ArrayList<String> string =new ArrayList<>();
 reading =new BufferedReader(new FileReader("C:\\Users\\User1\\File.txt");

 while( (presentString=read.readLine())!=null){
      string.add(presentString );
 }
ArrayList string=new ArrayList();
reading=new BufferedReader(新文件读取器(“C:\\Users\\User1\\File.txt”);
而((presentString=read.readLine())!=null){
string.add(presentString);
}

有人能帮我吗?

根据我对你问题的理解,你想减去连续字符串中的数字,然后用
结果替换原始字符串。
此代码应该可以:

ArrayList<String> strings =new ArrayList<>();
BufferedReader reading =new BufferedReader(new FileReader("C:\\Users\\User1\\File.txt");
String presentString = "";
while( (presentString==read.readLine())!=null){
    strings.add(presentString);
}
for(int i=0;i<strings.size();i+=2){
    //split the string so that the number is the last string in the array
    String[] list1 = strings.get(i).split(" ");
    String[] list2 = strings.get(i+1).split(" ");

    //subtract the after parsing the strings
    int result = Integer.parseInt(list2[list2.length-1]) - Integer.parseInt(list1[list1.length-1]);
    System.out.println(list2[list2.length-1] + "-" + list1[list1.length-1] + "=" + result);

    //remove the second string and replace the string with the result
    strings.remove(i+1);
    strings.add(i+1,"This is "+result);
}
ArrayList strings=new ArrayList();
BufferedReader reading=新的BufferedReader(新文件读取器(“C:\\Users\\User1\\File.txt”);
字符串presentString=“”;
while((presentString==read.readLine())!=null){
strings.add(presentString);
}

对于(inti=0;i可以使用
java.util.regex.Matcher
和只匹配数字的正则表达式。 我已经修改了您的代码,现在它用文件中找到的所有数字填充数组
numbers
,然后您可以对它们执行任何操作:

ArrayList<Integer> numbers = new ArrayList<>();
BufferedReader reading = new BufferedReader(new FileReader("C:\\Users\\User1\\File.txt");

//pattern that matches any number of digits from 0 to 9
Pattern pattern = Pattern.compile("[0-9]+"); 

while((presentString = read.readLine()) != null) {
    Matcher matcher = pattern.matcher(presentString);
    while (matcher.find()) {
        String numberAsString = matcher.group();
        numbers.add(Integer.valueOf(numberAsString));
    }
}
ArrayList number=new ArrayList();
BufferedReader reading=新的BufferedReader(新文件读取器(“C:\\Users\\User1\\File.txt”);
//匹配0到9之间任意位数的模式
Pattern=Pattern.compile(“[0-9]+”);
而((presentString=read.readLine())!=null){
Matcher Matcher=pattern.Matcher(presentString);
while(matcher.find()){
字符串numbersString=matcher.group();
add(Integer.valueOf(numbersString));
}
}
因此,您有一个名为“file.txt”的文件,并且您想解析其中的两个整数?假设只更改数字,您可以执行以下操作:

public int getSubtractedIntegersFromFile(File file){
    try {
        String filecontents = new String(Files.readAllBytes(Paths.get(file.toURI())));
        String s1;
        int int1 = Integer.parseInt((s1 = filecontents.substring(8, filecontents.length())));
        int int2 = Integer.parseInt(filecontents.substring(17 + s1.length(), filecontents.length()));
        return int2 - int1;
    } catch (IOException e) {
        e.printStackTrace();
    }
    return 0;
}

presentString==reading.readLine()!=null
?@theOffeekid:很抱歉输入错误:)尝试
System.out.println(您试图添加的内容)
并对输出进行注释。