首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在读取文件时搜索大于9的数字

在读取文件时搜索大于9的数字
EN

Stack Overflow用户
提问于 2021-04-21 23:13:06
回答 2查看 76关注 0票数 1
代码语言:javascript
复制
String str = JOptionPane.showInputDialog("Search for a number from 0-9");
    int intNum = Integer.parseInt(str);
    try {
        File file = new File("numbers.txt");
        FileReader fr = new FileReader(file);
        BufferedReader br = new BufferedReader(fr);
        int num;
        int count = 0;
        int position = 1;
        while ((num = br.read()) != -1) {
            if (Character.getNumericValue(num) == intNum) {
                System.out.println(intNum + " occurred in " + position + " digit");
                count++;
            }
            position++;
        }
        JOptionPane.showMessageDialog(null,
                intNum + " was found " + count + " times in " + position + " digits", "Result",
                JOptionPane.INFORMATION_MESSAGE);
        br.close();
        fr.close();
    } catch (FileNotFoundException e) {
        System.out.println("An error occurred.");
        e.printStackTrace();
    }
}

numbers.txt是一个包含数千个数字的文本文件。使用这段代码,我只能搜索0-9的值,有没有方法可以搜索数字10、11、12.

Clarification: I想要搜索以获得另一个数字中的一个数字(例如,在1_45_6_45_2中有2次出现45 )

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-04-21 23:30:32

用于查找大于9的数字

使用BufferedReader是非常好的:

代码语言:javascript
复制
try (BufferedReader br = new BufferedReader(new FileReader("your/file/path"))) {
  String s;
  while ((s = br.readLine()) != null) {
    String[] s1 = s.split(" ");
    for (int i = 0; i < s1.length; i++) {
      if (s1[i].matches("\\d+")) {
        int num = Integer.parseInt(s1[i]);
        if (num > 9) {
          System.out.println("Found number bigger than 9 (" + num + ")");
        }
      }
    }
  }
} catch (Exception e) {
  //log
}

br.readLine()读取下一行。用空格分隔行,然后检查行中的每个单词是否有一个数字。如果单词是一个数字(使用regex \\d+验证),则解析,然后检查它是否大于9。

再说一遍,Scanner确实使这项工作变得容易得多:

代码语言:javascript
复制
Scanner scanner;
try {
    scanner = new Scanner(new File("your/file/path"));
    while(scanner.hasNextLine()) {
        while(scanner.hasNextInt()) {
            int cur = scanner.nextInt();
            if(cur > 9) {
                System.out.println("Found number bigger than 9 ("+cur+")");
            }
        }
    }
} catch (FileNotFoundException e) {
    e.printStackTrace();
}

用于在另一个数字中搜索一个数字

代码语言:javascript
复制
try(BufferedReader br = new BufferedReader(new FileReader("your/file/path"))){
    String s;
    Scanner sc = new Scanner(System.in);
    int lookFor = Integer.parseInt(sc.nextLine());
    String read = br.readLine();
    int len = String.valueOf(lookFor).length();
    int found = 0;
    for(int i = 0; i+len < read.length(); i++) {
        String cur = read.substring(i, i+len);
        if(Integer.parseInt(cur)==lookFor) {
            found++;
        }
    }
}
System.out.println("Found: "+found);

虽然您提到文本文件包含数十亿位数字,但我想指出的是,String可以容纳的最大字符是Integer.MAX_VALUE。如果你得到一个OutOfMemoryException,那是因为有太多的数字。

票数 1
EN

Stack Overflow用户

发布于 2021-04-21 23:18:40

而不是仅仅使用BufferedReader,您可以使用一些更花哨的方法来读取整个整数,而不仅仅是单个字符,例如扫描仪。

这里有一个Scanner.nextInt()方法:

https://www.programiz.com/java-programming/scanner

(您还可以逐行读取并解析整数。)

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67204636

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档