Results 1 to 5 of 5
- 01-03-2010, 04:40 AM #1
Member
- Join Date
- Dec 2009
- Posts
- 22
- Rep Power
- 0
Not recognizing a variable in a method
Everything in my program would work if my program would recognize the variable called "gravity". It's these 2 lines.
Java Code:[B]mass[count] = ((140.0 * 433.59237) / gravity[count]); // calculates mass based on planet's gravity weight[count] = mass[count] * gravity[count]; // calculates weight based on mass[/B]
My program is taking the values of the gravities of all the planets in our solar system from a text file and using them to calculate the weight of an individual weighing 140 pounds. Here is the entire code:
Java Code:/** * This program calculates the weight of an individual on each planet in the solar system. * * * Jamison Hyman * 2/1/2010 */ import java.io.File; import java.util.Scanner; import java.awt.*; import java.io.PrintWriter; import java.io.IOException; import java.util.Scanner; public class WeightOnPlanetsV1 { public static void printHeadings( ) //notice that this method is void, nothing is 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("========================================================"); } // Imports Gravity values from text file. public static double [] loadGravity( ) throws IOException { Scanner in = new Scanner(new File("GravityValues.txt")); int count = 0; double [] gravity = new double [9]; while(in.hasNext()) { double temp = in.nextDouble(); gravity[count] = temp/2.45; count++; } in.close(); return gravity; } // Calculates weight of the user on all planets! public static double [] calculateWeight( ) { double [] mass = new double [9]; double [] weight = new double [9]; int count = 0; for (int i = 0; i < 9; i++) { mass[count] = ((140.0 * 433.59237) / gravity[count]); // calculates mass based on planet's gravity weight[count] = mass[count] * gravity[count]; // calculates weight based on mass count++; } return weight; } // Starts the MAIN! public static void main(String[ ] args) throws IOException { double [] gravity = loadGravity(); printHeadings(); String [] planets = {"Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune", "Pluto"}; double [] weight = calculateWeight(); for(int y = 0; y < 9 ; y++) { System.out.printf("%-11s", planets[y]); System.out.printf("%13.2f", gravity[y]); System.out.printf("%20.2f\n",weight[y]); } } // end main } // end class
-
It's a matter of scope here. Your gravity variable is declared in the main but you're trying to use it in calculateWeight() where it hasn't been declared, and so there it is invisible. So instead you should declare gravity and call loadGravity in the calculateWeight method only.
- 01-03-2010, 10:30 AM #3
Senior Member
- Join Date
- Aug 2008
- Posts
- 384
- Rep Power
- 5
Or you could make the gravity variable global :)
Than, however, you should make it static (ugly) or call the constructor of your class in the main and handle everything in there, which is better.I die a little on the inside...
Every time I get shot.
- 01-03-2010, 07:15 PM #4
Member
- Join Date
- Dec 2009
- Posts
- 22
- Rep Power
- 0
Do you mean like this? I changed it to this but it still doesn't work. Any idea what I'm doing wrong?
Java Code:/** * This program calculates the weight of an individual on each planet in the solar system. * * * Jamison Hyman * 2/1/2010 */ import java.io.File; import java.util.Scanner; import java.awt.*; import java.io.PrintWriter; import java.io.IOException; import java.util.Scanner; public class WeightOnPlanetsV1 { public static void printHeadings( ) //notice that this method is void, nothing is 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 [] loadGravity( ) throws IOException { Scanner in = new Scanner(new File("GravityValues.txt")); int count = 0; while(in.hasNext()) { double temp = in.nextDouble(); gravity[count] = temp/2.45; count++; } in.close(); return gravity; } public static double [] calculateWeight() { double [] gravity = new double [9]; loadGravity(); double [] mass = new double [9]; double [] weight = new double [9]; int count = 0; for (int i = 0; i < 9; i++) { mass[count] = ((140.0 * 433.59237) / gravity[count]); // calculates mass based on planet's gravity weight[count] = mass[count] * gravity[count]; // calculates weight based on mass count++; } return weight; } // Starts the MAIN! public static void main(String[ ] args) throws IOException { printHeadings(); String [] planets = {"Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune", "Pluto"}; calculateWeight(); double [] weight = calculateWeight(); for(int y = 0; y < 9 ; y++) { System.out.printf("%-11s", planets[y]); System.out.printf("%13.2f", gravity[y]); System.out.printf("%20.2f\n",weight[y]); } } // end main } // end class
- 01-03-2010, 08:04 PM #5
Member
- Join Date
- Dec 2009
- Posts
- 22
- Rep Power
- 0
Similar Threads
-
Java not recognizing UNIX secondary group
By PaulBinni in forum NetworkingReplies: 1Last Post: 11-18-2009, 03:45 PM -
Netbeans not recognizing library
By ehsen in forum NetBeansReplies: 16Last Post: 08-30-2009, 05:32 PM -
Help me finish an instance method which references a class variable?
By trueblue in forum New To JavaReplies: 20Last Post: 06-03-2009, 05:33 PM -
how to use a variable of one method in another method
By lucasautomacao in forum New To JavaReplies: 4Last Post: 10-29-2008, 02:21 PM -
Renaming a method/variable
By gapper in forum EclipseReplies: 0Last Post: 01-31-2008, 01:29 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks