# Thread: conversion between feet and meters

1. Member
Join Date
Oct 2010
Posts
25
Rep Power
0

## 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;

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);
}

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.

2. Member
Join Date
Oct 2010
Location
Posts
2
Rep Power
0
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;

3. Member
Join Date
Oct 2010
Posts
25
Rep Power
0
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

Java Code:
```public class conversionsBetweenFeetAndMeters
{
public static void main(String[] args)
{
// assigning variables
double meter = 0;
double foot =  0;
double x;
double y;

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();
}

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;
}
}```

Java 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

Java 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```
Last edited by Fubarable; 10-10-2010 at 07:24 PM. Reason: Moderator Edit: Code tags added

4. Here:
Java 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:

Java Code:
```[cod&#101;]
// notice how the top and bottom tags are different
[/cod&#101;]```
Much luck!

5. Senior Member
Join Date
Oct 2010
Location
Germany
Posts
787
Rep Power
8
.....

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

6. Member
Join Date
Oct 2010
Posts
25
Rep Power
0
Originally Posted by Fubarable
Here:
Java 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
Java Code:
` above your pasted code and the tag`
below your pasted code like so:

Java Code:
```[code]
// notice how the top and bottom tags are different```
[/code]

Much luck!
Java Code:
```public class conversionsBetweenFeetAndMeters
{
public static void main(String[] args)
{
// assigning variables
double meter = 0;
double foot =  0;
double x;
double y;

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();
}

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?

7. Member
Join Date
Oct 2010
Posts
25
Rep Power
0
Thank you guys.

8. Originally Posted by Latanyar
Thank you and did I place the code tags correctly?
Perfectly. Is all working now?

#### Posting Permissions

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