Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to read in doubles from a data file and convert them to an array using Scanner?
    primarykey
    data
    text
    <p>I am basically trying to read in doubles from a data file and convert them to an array using Scanner. I have a file called "gravityV1.txt" that has a bunch of positive doubles in it:</p> <blockquote> <p>3.76</p> <p>8.87</p> <p>1.0</p> <p>3.71</p> <p>24.92</p> <p>10.44</p> <p>8.87</p> <p>11.15</p> <p>0.58 </p> </blockquote> <h3>Other Information:</h3> <p>Below is most of my Source Code for a program that calculates your weight on each planet in the solar system(including Pluto). It scans a file called "gravityV1.txt", which includes the list of doubles aforementioned. Then it is supposed to read the txt file into an array, calculate the weight (in pounds) for each planet, and display the info in a table.</p> <h3>A Few More Things</h3> <p>This program uses multiple methods (in addition to the main), and if I have written anything regarding method calling incorrectly, please call me out!</p> <p>In this case, the array needs to be long enough for each of the surface gravity measurements of the 9 planets to fit, but how would you denote the array if one did not know long one's array needed to be? </p> <h3>But Most Importantly...</h3> <p>I have done <em>extensive</em> research on this problem, and I still have run into issues. This Source Code below does not compile because of a "possible loss of precision" error. The error message also says: "required: int; found double". </p> <p>Can someone please show me why this error occurs and also how to read this file and make it into an array called gravityData?</p> <p>This is what I have so far:</p> <pre><code>import java.util.Scanner; import java.io.File; import java.io.IOException; import java.io.FileReader; import java.io.PrintWriter; import java.util.Arrays; public class WeightOnPlanetsV1 { public static void printHeadings( ) //notice method is void, nothing returned { System.out.printf("%37s\n","My Weight on the Planets"); System.out.println(); System.out.printf("%s%20s%20s\n","Planet","Gravity","Weight(lbs)"); System.out.println("=============================================="); } public static double [] loadGravityData( ) throws IOException { Scanner myFileScanner = new Scanner(new FileReader("gravity1.txt")); int count = 0; double [] gravityData = new double[myFileScanner.nextDouble()]; for(int i = 0; i &lt; gravityData.length; i++) { gravityData[i] = myFileScanner.nextDouble(); } myFileScanner.close(); return gravityData; } public static double [] calculateWeight() throws IOException { loadGravityData( ); double [] gravityData = new double[10]; double [] mass = new double [9]; double [] weight = new double [9]; int count = 0; for(int i = 0; i &lt; 9; i++) { mass[count] = ((140.0 * 433.59237) / gravityData[count]); //Calculates mass based on planet's gravity weight[count] = (mass[count] * gravityData[count]) / 10; // calculates weight based on mass, division by 10 is for some physics reason I believe count++; } return weight; } // Starts the MAIN! public static void main(String[ ] args) throws IOException { double [] gravityData = new double [9]; printHeadings(); String [] planets = {"Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune", "Pluto"}; calculateWeight(); double [] weight = calculateWeight(); for(int y = 0; y &lt; 9 ; y++) { System.out.printf("%-11s", planets[y]); System.out.printf("%13.2f", gravityData[y]); System.out.printf("%20.2f\n",weight[y]); } } // end main </code></pre> <p>I welcome any help at all! <strong>Thank you!</strong> </p> <h1>Edit:</h1> <p>Alright this is what i have now for the loadGravityData method:</p> <pre><code>{ Scanner myFileScanner = new Scanner(new FileReader("gravity1.txt")); int count = 0; double [] gravityData = new double[myFileScanner.nextInt()]; for(int i = 0; i &lt; gravityData.length; i++) { gravityData[i] = myFileScanner.nextDouble(); } myFileScanner.close(); return gravityData; } </code></pre> <p>And I get "Main.java:20: error: cannot return a value from method whose result type is void" ??</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload