-2

I'm doing a Java exercise, and I need to display a persons height in both inches and cm, as well as their weight in both pounds and kg.. Here's what I came up with, but I'm getting tons of errors.

Height = 74; // inches
Weight = 180; // pounds

System.out.println( "He's " + Height + " inches or + (Height * 2.54) cm tall "." );

I got the 2.54 by just googling what the conversion was for inches to cm. I basically did the same thing for the weight (see below)

System.out.println( "He's " + Weight + " pounds or + (Weight * 2.20) kg heavy "." ); 

My goal is to get it to display:

He's 74 inches (or 187.96 cm) tall. 
He's 180 pounds (or 81.6466266 kg) heavy.

Any help would be great, and sorry for such a basic question!

Frakcool
  • 10,915
  • 9
  • 50
  • 89
Nikolai
  • 3
  • 3
  • 1
    We can't really tell you anything about the errors you're getting if you don't tell us what those errors are. – David Nov 24 '15 at 16:52
  • What is the problem you are facing? What is stopping you from writing/finishing your code? BTW "*sorry for such a basic question!*" but what is your question? – Pshemo Nov 24 '15 at 16:53
  • Normally variables and fields are written in [lowerCamelCase](https://en.wikipedia.org/wiki/CamelCase) in Java. – Emz Nov 24 '15 at 16:53
  • The quotes are wrongly placed ` or + ` should be ` or " +` – Joop Eggen Nov 24 '15 at 16:53
  • try this: `System.out.println( "He's " + Weight + " pounds or + (" + Weight * 2.20 + ") kg heavy "." );` – Arthur Eirich Nov 24 '15 at 16:54

7 Answers7

4

There are a couple of glaring syntax errors here:

System.out.println( "He's " + Height + " inches or + (Height * 2.54) cm tall "." );

Notice how the syntax highlighting on this page points them out. (Your IDE really should be doing that too.) You close a string at the end, then have a random . character and open another string which you never close.

You can fix the syntax errors by removing a quote:

System.out.println( "He's " + Height + " inches or + (Height * 2.54) cm tall ." );

However, this doesn't yet give you the output you want. Because this is just a string:

" inches or + (Height * 2.54) cm tall ."

Java isn't going to perform that calculation, as far as Java is concerned this is just text. You need to separate the string just like you already are for your other use of the variable:

System.out.println( "He's " + Height + " inches or " + (Height * 2.54) + " cm tall." );
David
  • 208,112
  • 36
  • 198
  • 279
  • I've been using notepad, I guess it's time to get an IDE. Thank you for the suggestions, I'll try this! – Nikolai Nov 24 '15 at 17:06
1

You're writing text instead of a variable's value:

System.out.println( "He's " + Height + " inches or + (Height * 2.54) cm tall "." );

Change it to:

System.out.println( "He's " + Height + " inches or (" + (Height * 2.54) + " cm) tall.");

The same goes for the weight line:

System.out.println( "He's " + Weight + " pounds or + (Weight * 2.20) kg heavy "." );

Should be:

System.out.println( "He's " + Weight + " pounds or (" + (Weight * 2.20) + " kg) heavy."); 

And please follow Java naming conventions:

Except for variables, all instance, class, and class constants are in mixed case with a lowercase first letter. Internal words start with capital letters. Variable names should not start with underscore _ or dollar sign $ characters, even though both are allowed.

From the above: variable names should start with a lower case

You should also read How to concatenate characters in Java

Community
  • 1
  • 1
Frakcool
  • 10,915
  • 9
  • 50
  • 89
  • Thank you for these suggestions, I will correct the naming conventions for my variables to lower case. – Nikolai Nov 24 '15 at 17:08
  • Yep, don't worry. Classes names should start with upper case :) This makes it easier to read ^^. Also if any of the answers solved your question(s) then please click the tick next to that answer to accept it ;) – Frakcool Nov 24 '15 at 17:10
  • @Nikolai oh and btw next time you post here at SO be sure you [take the tour](http://stackoverflow.com/tour), learn how to create a [Runnable Example](http://stackoverflow.com/help/mcve) which we can copy-paste and see the same issue that you see (Helps a lot with larger or not so easy to find errors) and also learn [How to ask a good question](http://stackoverflow.com/help/how-to-ask) so you don't get down voted as in this question :) and check last edit to my answer (about concatenation) – Frakcool Nov 24 '15 at 17:17
  • Yes, I'm disappointed in my first post on SO being so terrible. I'm a bit embarrassed. I will make sure to take the tour and all of your suggestions. Thank you again. – Nikolai Nov 24 '15 at 17:24
  • We all start that way, you'll get better with the time :) Good Luck. And also please stop changing accepted answer between users... It's annoying for all of us – Frakcool Nov 24 '15 at 17:27
1

This might answer your question, it is a bit vague, so I am mostly guessing.

System.out.println( "He's " + Height + " inches or (" + Height * 2.54 + ") cm tall." );

You had formatted your println pretty badly. It is the same for the later one, I suggest having a look at it and figuring that one out by yourself.

Also I have no clue of what unit of measurement you are using for your Weight and Height, I recommend a double for this scenario. If you want more precision you have to look up on BigDecimal.

Normally variables and fields are written in lowerCamelCase in Java.

Emz
  • 1,280
  • 1
  • 14
  • 29
1
System.out.println( "He's " + Height + " inches or ("+ (Height * 2.54)+" ) cm tall." );

Remember that you must do the conversion (without Strings quotes) before concatenate with text. The final dot is not needed.

avilbol
  • 145
  • 1
  • 2
  • 7
1

First off, you need to define what datatype your variables are. You can't have "Height", you need to have "int Height".

It's also easier if you set variables for both height and weight in their respective units. So you should have a variable for height in inches, and then a variable for height in centimeters.

Here's my solution:

public static void main(String[] args) {
    int inHeight = 74;
    double cmHeight = inHeight * 2.54;
    int lbWeight = 180;
    double kgWeight = lbWeight / 2.2;

    System.out.println("He's " + inHeight + " inches (or " + cmHeight + " cm) tall.");
    System.out.println("He's " + lbWeight + " pounds (or " + kgWeight + " kg) heavy.");

}

Note: two of the variables are doubles, so if you want to correctly format those to two decimal places, you'll have to use "printf" instead of "println".

Nerdy
  • 172
  • 4
  • 14
0

try this:

System.out.println( "He's " + Height + " inches or " + (Height * 2.54) + " cm tall "." );
Jörn Buitink
  • 2,906
  • 2
  • 22
  • 33
0
Double Height = 74D; // inches
Double Weight = 180D; // pounds

System.out.println( "He's " + Height + " inches or " + (Height * 2.54)  + " cm tall "." );
System.out.println( "He's " + Weight + " pounds or " + (Weight * 2.20) + " kg heavy "." );
Rafik BELDI
  • 4,140
  • 4
  • 24
  • 38