Results 1 to 5 of 5
  1. #1
    Jamison5213 is offline Member
    Join Date
    Dec 2009
    Posts
    22
    Rep Power
    0

    Default 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

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  3. #3
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    7

    Default

    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.

  4. #4
    Jamison5213 is offline Member
    Join Date
    Dec 2009
    Posts
    22
    Rep Power
    0

    Default

    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

  5. #5
    Jamison5213 is offline Member
    Join Date
    Dec 2009
    Posts
    22
    Rep Power
    0

Similar Threads

  1. Java not recognizing UNIX secondary group
    By PaulBinni in forum Networking
    Replies: 1
    Last Post: 11-18-2009, 03:45 PM
  2. Netbeans not recognizing library
    By ehsen in forum NetBeans
    Replies: 16
    Last Post: 08-30-2009, 05:32 PM
  3. Replies: 20
    Last Post: 06-03-2009, 05:33 PM
  4. how to use a variable of one method in another method
    By lucasautomacao in forum New To Java
    Replies: 4
    Last Post: 10-29-2008, 02:21 PM
  5. Renaming a method/variable
    By gapper in forum Eclipse
    Replies: 0
    Last Post: 01-31-2008, 01:29 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •