简体   繁体   English

有没有办法使用Scanner对象在文本文档中搜索特定数据?

[英]Is there a way to use a Scanner object to search a text document for specific data?

I am making a program that reads data from a formatted text document: 我正在制作一个程序,从格式化的文本文档中读取数据:

Planetary Data 行星数据

Planet Diameter (km) Mass (kg) g (m/s^2) 行星直径(km)质量(kg)g(m / s ^ 2)

  • Mercury 4880 3.30E+23 3.70 水星4880 3.30E + 23 3.70
  • Venus 12104 4.87E+24 8.87 维纳斯12104 4.87E + 24 8.87
  • Earth 12756 5.97E+24 9.79 地球12756 5.97E + 24 9.79
  • Mars 6794 6.24E+23 3.61 火星6794 6.24E + 23 3.61
  • Jupiter 142984 1.90E+27 24.80 木星142984 1.90E + 27 24.80
  • Saturn 120536 5.68E+26 10.43 土星120536 5.68E + 26 10.43
  • Uranus 51118 8.68E+25 8.86 天王星51118 8.68E + 25 8.86
  • Neptune 49352 1.02E+26 11.17 海王星49352 1.02E + 26 11.17

In a text file it's a little more pretty but basically it is a four column document with info one number falling into each column. 在一个文本文件中它更漂亮,但基本上它是一个四列文档,信息中有一个数字落入每一列。 I am using the scanner class to try to read only the last column of numbers, but can't find an easy way to do it with out using a ton of nextLine() calls. 我正在使用扫描仪类尝试只读取最后一列数字,但是无法通过大量的nextLine()调用找到一种简单的方法。 Is there a better way to search just for those doubles, and pull them into an array with a scanner? 有没有更好的方法来搜索那些双打,并用扫描仪将它们拉入阵列?

This code will compile but I get a mismatch error as soon as I run it. 这段代码会编译,但是一旦运行它就会出现不匹配错误。

public static double [] getGravity()throws IOException{
    Scanner inFile = new Scanner(new File("PlanetaryData.txt"));
    double [] getGrav = new double[8];
    for(int i = 0; i < 8; i++){
        getGrav[i] = inFile.nextDouble();
        getGrav[i] /= 10;
        System.out.println(getGrav[i]);
    }
    return getGrav;
}

Assuming your text file structure as below: 假设您的文本文件结构如下:

Mercury 4880 3.30E+23 3.70
Venus 12104 4.87E+24 8.87
Earth 12756 5.97E+24 9.79
Mars 6794 6.24E+23 3.61
Jupiter 142984 1.90E+27 24.80
Saturn 120536 5.68E+26 10.43
Uranus 51118 8.68E+25 8.86
Neptune 49352 1.02E+26 11.17

You may try this: 你可以试试这个:

public static double [] getGravity()throws IOException{
    Scanner inFile = new Scanner(new File("PlanetaryData.txt"));
    double [] getGrav = new double[8];
    for(int i = 0; i < 8; i++){
        getGrav[i] = inFile.nextLine().split("\\s+")[3];
        System.out.println(getGrav[i]);
    }
    return getGrav;
}

One possibility is using a List of doubles to store your read graivty values with something like, 一种可能性是使用一个双打列表来存储您的读取的graivty值,例如,

List<Double> al = new ArrayList<>();
Scanner scanner = null;
try {
  scanner = new Scanner(new File("file.txt"));
  while (scanner.hasNextLine()) {
    String str = scanner.nextLine();
    if (str == null) {
      continue;
    }
    double gravity = Double.parseDouble(
        str.substring(1 + str.lastIndexOf(' ')).trim());
    al.add(gravity);
  }
} catch (Exception e) {
  e.printStackTrace();
} finally {
  if (scanner != null) {
    scanner.close();
  }
}
System.out.println(al);

However, then you have no direct link between planet name and value. 但是,行星名称和价值之间没有直接联系。 Another option is using a Map<String, Double> to associate the planet name and value like, 另一种选择是使用Map<String, Double>来关联行星名称和值,如:

Map<String, Double> map = new HashMap<>();
Scanner scanner = null;
try {
  scanner = new Scanner(new File("file.txt"));
  while (scanner.hasNextLine()) {
    String str = scanner.nextLine();
    if (str == null) {
      continue;
    }
    String planet = str.substring(0, str.indexOf(' '));
    double gravity = Double.parseDouble(
        str.substring(1 + str.lastIndexOf(' ')).trim());
    map.put(planet, gravity);
  }
} catch (Exception e) {
  e.printStackTrace();
} finally {
  if (scanner != null) {
    scanner.close();
  }
}
System.out.println(map);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用扫描仪对象搜索文档? - Searching a document with a Scanner object? 这样可以使用扫描仪吗? - Is it possible to use a scanner this way? 有什么办法可以在不同的类中使用 Scanner 对象吗? - Is there any way I can use a Scanner object in a different class? 搜索对象特定属性的最佳方法 - Best way to search for specific attribute of Object 使用扫描仪选择特定文本 - Select Specific Text Using Scanner 有没有办法使用二进制搜索来获得特定结果 - Is there a way to use binary search to get a specific outcome 在特定文档上搜索 lucene - search lucene on a specific document Android-是否有任何功能或方法可以在HTML Webview文本中搜索特定文本 - Android - is there any Function or Way to Search a specific text in an HTML Webview text 有没有办法使用scanner().nextLine 方法来label 一个带有数字的特定名称,如果它们具有相同的名称,则未来的行 - Is there a way to use the scanner().nextLine method to label a specific name with a number the future lines if they have the same name 在 Java 中,使用 Scanner,有没有办法在 CSV 文件中找到特定的字符串,将其用作列 header 并返回其下的所有值? - In Java, using Scanner, Is there a way to find a specific String in a CSV file, use it as a column header and return all values under it?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM