如何读取String中的整数解释是一样的?

时间:2016-01-29 16:21:50

标签: java numbers 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 );
 }

有人可以帮帮我吗?

3 个答案:

答案 0 :(得分:0)

根据我对您的问题的理解,您想要减去连续字符串中的数字,然后用result替换原始字符串。这段代码应该这样做:

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);
}

答案 1 :(得分:0)

您可以使用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));
    }
}

答案 2 :(得分:0)

所以你有一个名为“File.txt”的文件,你想要解析它里面的两个整数?假设唯一改变的是数字,您可以执行以下操作:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char *strins(char s1[], char s2[], int insert_pos, int sizeOfS1, int sizeOfS2);

int main(void){
    char zk1[1000];
    char zk2[1000];
    char *zk3;
    int pos=0;
    int n1, n2;

    printf("plz enter the first string : ");
    scanf("%999[^\n]%*c", zk1);//gets shouldn't use. (already obsolete)
    n1=sizeof(zk1);//strlen(zk1);
    printf("plz enter the second string : ");
    scanf("%999[^\n]%*c", zk2);
    n2=sizeof(zk2);//sizeof(int) --> sizeof(char) but it's 1 by standard
    printf("plz enter the position");
    scanf("%d", &pos);
    zk3 = strins(zk1, zk2, pos, n1, n2);
    puts(zk3);
    free(zk3);
    return 0;
}

char *strins(char zk1[], char zk2[], int pos, int n1, int n2){
    char *zk3 = malloc(n1 + n2);//+1 when use stelen for null-terminate
    int ctr=0;

    for(int i = 0; i < pos; i++){//pos isn't checking of valid
        zk3[ctr++] = zk1[i];
    }
    for(int i = 0; zk2[i]; i++){//loop Until the '\0'
        zk3[ctr++] = zk2[i];
    }
    for(int i = pos; zk1[i]; i++){
        zk3[ctr++] = zk1[i];
    }
    zk3[ctr] = '\0';

    return zk3;
}