Jump to content

Need help with US metric system problem (JAVA)

I need to write a method that is passed an integer and prints a message with the number of feet and inches. If the number of feet or inches is 0, don’t print the amount. USE IF/ELSE STATEMENTS.

I have this so far: 

	public static void printFeetAndInches(int x) {
		int fi =  x/12;
		String str = Integer.toString(fi);
		if (str.indexOf(0) != 0) {
			System.out.println(x + "\"=" + fi + "\"");
		} else {
			System.out.println(x + "\"=" + x + "\"");
		}
	}
	

Problem is that if pass 58 for example I get 58"=4' instead of 58"=4'10"

Also, I'm not american so if you find some error in the conversion please let me know

Link to post
Share on other sites

Consider that an int variable does not contain decimal point, so you could never get any sort of decimal points from it..

Is this just converting inches to Feet+inches? At first I thought it converted meters to feet+inches.. but nvm.

Think about how you should convert the unit of measurement before displaying it to the user. It's probably best to start off by calculating the metric measurement to inches and then see if you have enough inches to get a foot or more feet out of it. Then you just have to calculate the remainder back to inches.

 

Also, work with double variables in this case.

 

 

E.g. your user fill in 78

Now you just have to subtract all the feet from these inches, so divide it by 12 indeed. That gives you 6.562 feet. So 6 feet and some remainder.

The way I would look at this, is simply convert these feet into inches (so 6 * 12) and subtract that from the original inches amount. Easiest way to get the 6 from the decimal number is placing it into an int.

That gets you 72 inches, so with 78 - 72 that leaves you with 6 inches.

 

So in 'pseudocode' that would look a little bit like this:

public void ConvertAndPrint(int inchesInput)
{
    int amountOfFeet = inchesInput / 12;
    double amountOfInches = inches - (amountOfFeet * 12);
    System.out.println("Your total is: " + amountOfFeet " feet and " + amountOfInches + " inches.");    
}

The way I would deal with printing this is a little something like this:

string outputMessage = "Your total is: ";
if(amountOfFeet > 0)
{
	outputMessage += amountOfFeet + "\""
}
if(amountOfInches > 0)
{
	outputMessage += amountofInches + "\'"
}

It just kind of depends on how you want to display this. Perhaps you want an else here saying something like "the number you filled in is too small for any amount of inches/feet.

 

Keep in mind I didn't test this code, it's more of a proof of concept. Be sure to run some tests to see if this properly converts the numbers for you.

If anything was unclear, or if I misunderstood your question, be sure to press the reply/quote button and I would hope to be able to help you again!

image.png

"We're all in this together, might as well be friends" Tom, Toonami.

 

mini eLiXiVy: my open source 65% mechanical PCB, a build log, PCB anatomy and discussing open source licenses: https://linustechtips.com/topic/1366493-elixivy-a-65-mechanical-keyboard-build-log-pcb-anatomy-and-how-i-open-sourced-this-project/

 

mini_cardboard: a 4% keyboard build log and how keyboards workhttps://linustechtips.com/topic/1328547-mini_cardboard-a-4-keyboard-build-log-and-how-keyboards-work/

Link to post
Share on other sites

54 minutes ago, Minibois said:

Consider that an int variable does not contain decimal point, so you could never get any sort of decimal points from it..

Is this just converting inches to Feet+inches? At first I thought it converted meters to feet+inches.. but nvm.

Think about how you should convert the unit of measurement before displaying it to the user. It's probably best to start off by calculating the metric measurement to inches and then see if you have enough inches to get a foot or more feet out of it. Then you just have to calculate the remainder back to inches.

 

Also, work with double variables in this case.

 

 

E.g. your user fill in 78

Now you just have to subtract all the feet from these inches, so divide it by 12 indeed. That gives you 6.562 feet. So 6 feet and some remainder.

The way I would look at this, is simply convert these feet into inches (so 6 * 12) and subtract that from the original inches amount. Easiest way to get the 6 from the decimal number is placing it into an int.

That gets you 72 inches, so with 78 - 72 that leaves you with 6 inches.

 

So in 'pseudocode' that would look a little bit like this:


public void ConvertAndPrint(int inchesInput)
{
    int amountOfFeet = inchesInput / 12;
    double amountOfInches = inches - (amountOfFeet * 12);
    System.out.println("Your total is: " + amountOfFeet " feet and " + amountOfInches + " inches.");    
}

The way I would deal with printing this is a little something like this:


string outputMessage = "Your total is: ";
if(amountOfFeet > 0)
{
	outputMessage += amountOfFeet + "\""
}
if(amountOfInches > 0)
{
	outputMessage += amountofInches + "\'"
}

It just kind of depends on how you want to display this. Perhaps you want an else here saying something like "the number you filled in is too small for any amount of inches/feet.

 

Keep in mind I didn't test this code, it's more of a proof of concept. Be sure to run some tests to see if this properly converts the numbers for you.

If anything was unclear, or if I misunderstood your question, be sure to press the reply/quote button and I would hope to be able to help you again!

image.png

Thanks. I never thought of getting it by declaring feets and inches as their own variables. I can't believe I was stuck at that for so long lol

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×