[英]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.