Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Nov 2008
    Posts
    7
    Thanks
    5
    Thanked 0 Times in 0 Posts

    beginner problem - using methods does not detect variable.

    hello I have been taking a course in java and doing horrible. Well, not horrible, I do the assigments, but ask tons of questions.

    I am writing a program to return the conversion of meters to other units, I have the code part working, and was working on asthetics, I needed to break down into methods, thats where the problem came in.

    I am using meters (double) and when I try to pass it off to another method it wont recognize the variable. Can someone please help?

    thanks
    Code:
    import java.util.Scanner; //Needed for Scanner Class
    
    /**
    
    	my name and infor here
    
    */
    
    public class meterProblem
    {
    	public static void main(String [] args)
    	{
    		double meters; // A number entered by the user
    		int number;	//selection by the user		
    
    
    
    	//Create Scanner object keyboard input
    	Scanner keyboard = new Scanner(System.in);
    
    	//Get number from user.
    	System.out.println("Enter a distance in meters: ");
    	meters = keyboard.nextDouble();	 
    	
    
    	if (meters < 0)
    	{
    		System.out.println("Number has to be greater than 0, please enter meter: ");
    			meters = keyboard.nextDouble();
    	}
    
    
    
    	//Get Selection from user.
    	System.out.print("Enter your choice:  ");
    	System.out.println("\n1. Convert to kilometers.\n" + "2. Convert to inches.\n" +
    			   "3. Convert to feet.\n" + "4. Quit the program.");
    		number = keyboard.nextInt();
    			
    	
    	//Determine number entered.
    	switch (number)
    	{
    	
    		case 1:
    			showKiloMeters();
    			break;
    	
    		case 2:
    			showInches();
    			break;
    
    		case 3:
    			showFeet();
    			break;
    			
    		case 4:	
    			System.out.println("thanks, goodbye.");
    			application.shutdown();		
    
    		default:
    			System.out.println("That's not 1,2,3, or 4.");
    			break;
    		}
    	}	
    	public static void showKiloMeters()
    	{
    		
    		double kiloMeters = 0.00;
    			kiloMeters = (meters * 0.001);
    			
    			System.out.print(meters + " meters is " + kiloMeters + " Kilometers.");
    			
    	}
    	public static void showInches()
    	{
    		double showInches = 0.00;
    			showInches = (meters * 39.37);
    	
    			System.out.print(meters + " meters is " + showInches + " inches.");
    		
    	}
    	public static void showFeet()
    	{
    		double feet = 0.00;
    			feet = (meters * 3.281);
    
    			System.out.print(meters + " meters is " + feet + " feet.");
    			
    	}
    }
    Last edited by jlopez3203; 11-07-2008 at 07:31 PM.

  • #2
    Gox
    Gox is offline
    Regular Coder Gox's Avatar
    Join Date
    May 2006
    Location
    Ontario, Canada
    Posts
    392
    Thanks
    2
    Thanked 20 Times in 20 Posts
    Could you make things a little easier on those wishing to help by showing us exactly what line(s) of code have an issue and the error(s), if any, that you receive?

    From the looks of things though, my guess is you're having issues with your showKiloMeters, showInches and showFeet methods. If this is correct it's because your "meters" variable is declared inside of your "main". This means that the "meters" variable is not visible to any code outside of your main method (i.e. showKiloMeters, showInches and showFeet). For further reading google something along the lines of "java variable scope".

    To fix your issue I see a couple possibilities.
    1. Make meters a class variable so that all methods in the class can see and access it.
    Code:
    public class meterProblem
    {
       //If declared here, your show methods should be able to access meters
       double meters; // A number entered by the user
    
    public static void main(String [] args)
    {
        int number; //selection by the user 
    ...
    2. Leave the meters variable declared in "main", but pass it to your show methods so that they can access it
    Code:
    ...
    case 1:
    showKiloMeters(meters);
    break;
    
    case 2:
    showInches(meters);
    break;
    
    case 3:
    showFeet(meters);
    break;
    ...
    public static void showKiloMeters(double meters)
    {...}
    
    public static void showInches(double meters)
    {...}
    
    public static void showFeet(double meters)
    {...}
    Last edited by Gox; 11-07-2008 at 03:25 AM.

  • Users who have thanked Gox for this post:

    jlopez3203 (11-07-2008)

  • #3
    Senior Coder shyam's Avatar
    Join Date
    Jul 2005
    Posts
    1,563
    Thanks
    2
    Thanked 163 Times in 160 Posts
    Quote Originally Posted by Gox View Post
    1. Make meters a class variable so that all methods in the class can see and access it.
    Code:
    public class meterProblem
    {
       //If declared here, your show methods should be able to access meters
       double meters; // A number entered by the user
    
    public static void main(String [] args)
    {
        int number; //selection by the user 
    ...
    since all the methods are static if the meters variable has to be accessible by all the methods its gotta be static too
    You never have to change anything you got up in the middle of the night to write. -- Saul Bellow

  • Users who have thanked shyam for this post:

    jlopez3203 (11-07-2008)

  • #4
    Senior Coder shyam's Avatar
    Join Date
    Jul 2005
    Posts
    1,563
    Thanks
    2
    Thanked 163 Times in 160 Posts
    Quote Originally Posted by jlopez3203 View Post
    Can someone please help?
    not everyone is patient enough like Gox to go over your unformatted code...you'll do yourself a great favour if you start using &#91;code&#93;&#91;/code&#93; tags
    You never have to change anything you got up in the middle of the night to write. -- Saul Bellow

  • #5
    New to the CF scene
    Join Date
    Nov 2008
    Posts
    7
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Its my first post, and Im not posting literate yet, I did try to read the rules and post guidelines and abide by them, but all you guys rock, thanks, I will try your suggestions and my posting will get cleaner with time.

    thanks again.

    Now i have separated them into separate methods, but Im suppose to create a menu loop that shows the options to chose from.

    1. convert to kilometers
    2. convert to inches
    3. convert to feet
    4. quit the program.

    Where would I add a loop like that into the problem, the beginning, the end, the middle? would it be considered an infinite loop?

    Code:
    import java.util.Scanner; //needed for scanner class
    
    /**
    	
    	Java Thursday Evening
    
    */
    
    public class convertingMetersMethod
    {
    	
    
    	public static void main(String [] args)
    	{
    
    		int number;	//number entered by user
    		double meters;
    
    
    
    	// create a scanner object for keyboard input
    	Scanner keyboard = new Scanner(System.in);
    
    
    
    	//enter meters
    	System.out.println("Enter a distance in meters: ");
    	meters = keyboard.nextDouble();
    
    
    
    	// get a selection from menu
    	System.out.print("Enter your choice: ");
    	System.out.println("\n1. Convert to kilometers\n2. Convert to inches\n" +
    							 "3. Convert to feet\n4. Quit the program");
    	number = keyboard.nextInt();
    	
    
    	//determine number entered
    	switch (number)
    	
    	{
    	
    		case 1:
    			showKilometers(meters);
    			break;
    
    		case 2:
    			showInches(meters);
    			break;
    
    		case 3:
    			showFeet(meters);
    			break;
    
    		case 4:
    			System.out.println("Thanks.  Good day!");
    			System.exit(0);
    
    		default:
    			System.out.println("That's not a valid option, try again: ");
    			number = keyboard.nextInt();
    	}
        }
    	
    	public static void showKilometers(double meters)
    	{
    		double kiloMeters;
    			kiloMeters = meters * 0.001;
    			System.out.println(meters + " meters is " + kiloMeters + " kilometers.");
    			
    				}
    	
    	public static void showInches(double meters)
    	{
    		double inches;
    			inches = meters * 39.37;
    			System.out.println(meters + " meters is " + inches + " inches.");
    			
    	}
    	
    	public static void showFeet(double meters)
    	{
    		double feet;
    			feet = meters * 3.281;
    			System.out.println(meters + " meters is " + feet + " feet.");
    			
    	}
    }
    Last edited by jlopez3203; 11-07-2008 at 11:58 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
    •