1
votes

I am making a program that reads data from a formatted text document:

Planetary Data

Planet Diameter (km) Mass (kg) g (m/s^2)

  • 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

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. 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;
}
2

2 Answers

2
votes

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;
}
1
votes

One possibility is using a List of doubles to store your read graivty values with something like,

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