# conversion between feet and meters

• 10-10-2010, 05:39 PM
Latanyar
conversion between feet and meters
I am writing a class that contains the following two methods

public static double footToMeter(double foot)
public static double meterToFoot(double meter)

This is what I have

public class conversionsBetweenFeetAndMeters
{
public static void main(String[] args)
{
// assigning variables
double meter;
double foot;
double x;
double y;

//Header for meters to foot
System.out.println("Meters Feet");

//for loop to call meter method and print out table
for(x = 1; x <=10; x++)
{
meter = footToMeter(x);
System.out.printf("5.1f%20.3f" , x, foot);
}

//Header for foot to meters
System.out.println("Feet Meters");

//for loop to call foot method and print out table
for(y = 20; y <= 65; y = y+5)
{
foot = meterToFoot(y);
System.out.printf("5.1f%20.3f" , y, meter);
}
}

//Converts from feet to meters
public static double footToMeter(double foot)
{
return 0.305 * foot;
}

//Converts from meters to feet
public static double meterToFoot(double meter)
{
return 3.281 * meter;
}
}

I am keep getting this syntax error message: variable foot might not have been initialized.
• 10-10-2010, 05:46 PM
Maulin Patel
In Java, class instance variables and static variables have default values: null for all object types, false for boolean primitive and 0 for numeric primitives. But local variables inside a method have no defaults

There is rule that local variables must be first initialized before they are first read.

so u might have to declare like this:

double meter =0, foot =0;
• 10-10-2010, 07:04 PM
Latanyar
Quote:

Originally Posted by Maulin Patel
In Java, class instance variables and static variables have default values: null for all object types, false for boolean primitive and 0 for numeric primitives. But local variables inside a method have no defaults

There is rule that local variables must be first initialized before they are first read.

so u might have to declare like this:

double meter =0, foot =0;

ok. now when I run the program my formula is incorrect

Code:

```public class conversionsBetweenFeetAndMeters {     public static void main(String[] args)     {         // assigning variables         double meter = 0;         double foot =  0;         double x;         double y;                 //Header for meters to foot         System.out.println("  Feet                Meters");                 //for loop to call meter method and print out table         for(x = 1; x <=10; x++)         {             meter = footToMeter(x);             System.out.printf("%5.1f%20.3f" , x, foot);             System.out.println();         }                 //Header for foot to meters         System.out.println("Meters              Feet");                 //for loop to call foot method and print out table         for(y = 20; y <= 65; y = y+5)         {             foot = meterToFoot(y);             System.out.printf("%5.1f%20.3f" , y, meter);             System.out.println();         }     }         //Converts from feet to meters     public static double footToMeter(double foot)     {       return 0.305 * foot;      }         //Converts from meters to feet     public static double meterToFoot(double meter)     {       return 3.281 * meter;     } }```

Code:

``` Feet                Meters   1.0              0.000   2.0              0.000   3.0              0.000   4.0              0.000   5.0              0.000   6.0              0.000   7.0              0.000   8.0              0.000   9.0              0.000  10.0              0.000 Meters              Feet  20.0              3.050  25.0              3.050  30.0              3.050  35.0              3.050  40.0              3.050  45.0              3.050  50.0              3.050  55.0              3.050  60.0              3.050  65.0              3.050```

It should be

Code:

```feet                meters 1.0                0.305 2.0                0.61    and etc...all the way to 10 and meters            feet 20.0              65.574 25.0              81.967    and etc...all the way down to 65```
Moderator Edit: Code tags added
• 10-10-2010, 07:25 PM
Fubarable
Here:
Code:

```        for(x = 1; x <=10; x++)         {             meter = footToMeter(x);             System.out.printf("%5.1f%20.3f" , x, foot);             System.out.println();         }```
You're printing out foot result, but your code never changes foot, but rather it sets x and meter.

Also, when posting code here, please use code tags so that your code will retain its formatting and thus will be readable -- after all, your goal is to get as many people to read your post and understand your code as possible, right?

To do this, highlight your pasted code (please be sure that it is already formatted when you paste it into the forum; the code tags don't magically format unformatted code) and then press the code button, and your code will have tags.

Another way to do this is to manually place the tags into your code by placing the tag [cod&#101;] above your pasted code and the tag [/cod&#101;] below your pasted code like so:

Code:

```[cod&#101;]   // your code goes here   // notice how the top and bottom tags are different [/cod&#101;]```
Much luck!
• 10-10-2010, 07:29 PM
eRaaaa
.....

meter = footToMeter(x);
System.out.printf("%5.1f%20.3f" , x, foot);
and
foot = meterToFoot(y);
System.out.printf("%5.1f%20.3f" , y, meter);

--->

meter = footToMeter(x);
System.out.printf("%5.1f%20.3f" , x, meter);
and
foot = meterToFoot(y);
System.out.printf("%5.1f%20.3f" , y, foot);
• 10-10-2010, 07:47 PM
Latanyar
Quote:

Originally Posted by Fubarable
Here:
Code:

```        for(x = 1; x <=10; x++)         {             meter = footToMeter(x);             System.out.printf("%5.1f%20.3f" , x, foot);             System.out.println();         }```
You're printing out foot result, but your code never changes foot, but rather it sets x and meter.

Also, when posting code here, please use code tags so that your code will retain its formatting and thus will be readable -- after all, your goal is to get as many people to read your post and understand your code as possible, right?

To do this, highlight your pasted code (please be sure that it is already formatted when you paste it into the forum; the code tags don't magically format unformatted code) and then press the code button, and your code will have tags.

Another way to do this is to manually place the tags into your code by placing the tag
Code:

` above your pasted code and the tag`
below your pasted code like so:

Code:

```[code]   // your code goes here   // notice how the top and bottom tags are different```
[/code]

Much luck!

Code:

```public class conversionsBetweenFeetAndMeters {     public static void main(String[] args)     {         // assigning variables         double meter = 0;         double foot =  0;         double x;         double y;                 //Header for meters to foot         System.out.println("  Feet                Meters");                 //for loop to call meter method and print out table         for(x = 1; x <=10; x++)         {             meter = footToMeter(x);             System.out.printf("%5.1f%20.3f" , x, meter);             System.out.println();         }                 //Header for foot to meters         System.out.println("Meters              Feet");                 //for loop to call foot method and print out table         for(y = 20; y <= 65; y = y+5)         {             foot = meterToFoot(y);             System.out.printf("%5.1f%20.3f" , y, foot);             System.out.println();         }     }         //Converts from feet to meters     public static double footToMeter(double foot)     {       return 0.305 * foot;      }         //Converts from meters to feet     public static double meterToFoot(double meter)     {       return 3.281 * meter;     } }```

Thank you and did I place the code tags correctly?
• 10-10-2010, 07:49 PM
Latanyar
Thank you guys.
• 10-10-2010, 08:20 PM
Fubarable
Quote:

Originally Posted by Latanyar
Thank you and did I place the code tags correctly?

Perfectly. Is all working now?