Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using strings and variables in println statements

Tags:

java

I am writing a program that finds a user's ideal weight by asking for their height in inches and feet. So far everything looks alright except the final statement, in which I return to the user the average weight for a male and female, and also tell them the range of their weight between 15%. Here is the code that gives me a problem:

System.out.println("For a male, the height " + foot "' " +
                   inches + "/""" + " and it's ideal weight is " + 
                   mWeight + "lbs. Anything between " + minMaleLb +
                   "lbs and " + maxMaleLb + "lbs is okay.");
System.out.println("For a female, the height " + foot "' " +
                   inches + "/""" + " and it's ideal weight is " + 
                   fWeight + "lbs. Anything between " + minFeMaleLb +
                   "lbs and " + maxFeMaleLb + "lbs is okay.");

And here are the errors I receive while compiling:

File: C:\Users\###\Java\IdealWeight.java  [line: 26]
Error: C:\Users\###\Java\IdealWeight.java:26: ')' expected
File: C:\Users\###\Java\IdealWeight.java  [line: 27]
Error: C:\Users\###\Java\IdealWeight.java:27: not a statement
File: C:\Users\###\Java\IdealWeight.java  [line: 27]
Error: C:\Users\###\Java\IdealWeight.java:27: ';' expected
File: C:\Users\###\Java\IdealWeight.java  [line: 29]
Error: C:\Users\###\Java\IdealWeight.java:29: not a statement
File: C:\Users\###\Java\IdealWeight.java  [line: 29]
Error: C:\Users\###\Java\IdealWeight.java:29: ';' expected
File: C:\Users\###\Java\IdealWeight.java  [line: 31]
Error: C:\Users\###\Java\IdealWeight.java:31: ')' expected
File: C:\Users\###\Java\IdealWeight.java  [line: 32]
Error: C:\Users\###\Java\IdealWeight.java:32: not a statement
File: C:\Users\###\Java\IdealWeight.java  [line: 32]
Error: C:\Users\###\Java\IdealWeight.java:32: ';' expected
File: C:\Users\###\Java\IdealWeight.java  [line: 34]
Error: C:\Users\###\Java\IdealWeight.java:34: not a statement
File: C:\Users\###\Java\IdealWeight.java  [line: 34]
Error: C:\Users\###\Java\IdealWeight.java:34: ';' expected
File: C:\Users\###\Java\Weight.java  [line: 30]
Error: C:\Users\###\Java\Weight.java:30: reached end of file while parsing
File: C:\Users\###\Java\Weight.java  [line: 34]
Error: C:\Users\###\Java\Weight.java:34: reached end of file while parsing

Can someone please explain to me what is wrong with the code, and how would I go about fixing this?

like image 324
mooles Avatar asked Nov 28 '22 00:11

mooles


2 Answers

The problem is that you're escaping your quotes wrong; that should be

... + "\"" + ...

for the quotemark.

EDIT: And as JB Nizet pointed out in a comment, you're missing the + after foot.

Also, you can simplify that expression a bit more with

... + "\" and the ideal weight is " + ...

rather than concatenating two separate strings.

like image 99
fluffy Avatar answered Nov 30 '22 23:11

fluffy


With all those concatenations, I'd sugest using System.out.format(format, args);

System.out.format("For a male, the height %d' %d\" and its ideal weight is %dlbs. Anything between %dlbs and %dlbs is okay.",
    foot, inches, mWeight, minMaleLb, maxMaleLb);

You also have a spelling error:

and it's ideal weight is

should be

and its ideal weight is

"its" has no apostrophe (ie not "it's") when used in the possessive form

like image 29
Bohemian Avatar answered Nov 30 '22 22:11

Bohemian