PDA

View Full Version : beginner problem - using methods does not detect variable.


jlopez3203
11-07-2008, 02:09 AM
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

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.");

}
}

Gox
11-07-2008, 03:17 AM
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.

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

...
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)
{...}

shyam
11-07-2008, 04:30 PM
1. Make meters a class variable so that all methods in the class can see and access it.

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 :rolleyes:

shyam
11-07-2008, 04:34 PM
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 [code][/code] tags

jlopez3203
11-07-2008, 07:27 PM
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?


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.");

}
}