Reputation: 41
I am currently taking a introduction to programming class and have reached a problem. I have been asked to convert a height measurement from inches to feet & inches.
I have gotten to the point were I think I mostly have it but I get a not a statement error when I go to compile. Here is what I have so far for this method
/**
* @param inches to feet inches
*/
public String inchToFeet(int heightInInches) {
int IN_PER_FOOT;
int feet = heightInInches - IN_PER_FOOT;
String output;
feet = feet / 12;
output = String; inchToFeet() + "\'" + IN_PER_FOOT.toString() + "\"";
return output;
}
I am also using a static final int to keep the inches per foot constrained to 12, like this.
public static final int IN_PER_FOOT = 12;
This is really the only issue I am having at the moment, the rest it just getting it along with a hourly rate to display.
Edit:
The compile error I keep getting is 'Not a Statement'. I have also removed the semicolon from before the last String but got another error as it was looking for the semicolon.
I will try your suggestion in a little Rahul, been looking at this for to long and need a break.
Upvotes: 4
Views: 24774
Reputation: 178
After Searching on StackOverflow and another website I made Ft->cm and cm->ft by combining 2-3 answers. Can come in handy
fun feetToCentimeter(feetval: String): String {
var heightInFeet = 0.0
var heightInInches = 0.0
var feet = ""
var inches = ""
if (!TextUtils.isEmpty(feetval)) {
if (feetval.contains("'")) {
feet = feetval.substring(0, feetval.indexOf("'"))
}
if (feetval.contains("\"")) {
inches = feetval.substring(feetval.indexOf("'") + 1, feetval.indexOf("\""))
}
}
try {
if (feet.trim { it <= ' ' }.isNotEmpty()) {
heightInFeet = feet.toDouble()
}
if (inches.trim { it <= ' ' }.isNotEmpty()) {
heightInInches = inches.toDouble()
}
} catch (nfe: NumberFormatException) {
}
return (((heightInFeet * 12.0) + heightInInches) * 2.54).toString()
}
fun centimeterToFeet(centemeter: String?): String {
var feetPart = 0
var inchesPart = 0
if (!TextUtils.isEmpty(centemeter)) {
val dCentimeter = java.lang.Double.valueOf(centemeter!!)
feetPart = Math.floor((dCentimeter / 2.54) / 12).toInt()
println(dCentimeter / 2.54 - feetPart * 12)
inchesPart = Math.floor((dCentimeter / 2.54) - (feetPart * 12)).toInt()
}
return String.format("%d' %d\"", feetPart, inchesPart)
}
Upvotes: 0
Reputation: 27
Try this code
int inches = 40;
int feet = (int)inches / 12;
int leftover =(int) inches % 12;
System.out.println(feet + " feet and " + leftover + " inches");
Upvotes: -2
Reputation: 172438
Your code doesnt seem to work what you want. You may simply try like this:-
int inches = 40;
int feet = inches / 12;
int leftover = inches % 12;
System.out.println(feet + " feet and " + leftover + " inches");
Upvotes: 7