user2782378
user2782378

Reputation:

Semi colon expected?

It is telling my that a semi colon is expected in between the second "%lf" and the comma before the "x". Here is the code segment:

 public String toString() {
        return "%lf, %lf", x, y; 
    }

I am trying to return the string "(X, Y)"

Any ideas? Many thanks!

Upvotes: 1

Views: 6072

Answers (4)

Reimeus
Reimeus

Reputation: 159854

Comma-separated variables cannot be recognized as a valid Strings in Java.

It appears you are using format specifiers similar to those found in C/C++'s printf function.

Assuming that x and y are valid floating point values you can use

return String.format("%f, %f", x, y); 

to format the output String

Read: Formatter

Upvotes: 6

Joe
Joe

Reputation: 99

if you are trying to access multiple values, just use two different functions to return the values. if you want to return both values as a single string, use concatenation.

Upvotes: 1

JNL
JNL

Reputation: 4713

return "%lf, %lf", x, y; 

is not a String

return "%lf, %lf, x, y";

or

return "%lf, %lf" + x + y;

is a String.

Upvotes: 2

nanofarad
nanofarad

Reputation: 41281

I don't know what you are trying to do. You can't return multiple values as the method is declared String. Try as follows:

return "%lf, %lf"+x+y; 

for a literal contatenation.

return String.format("%f, %f", x, y); 

will use a format string.

Upvotes: 2

Related Questions