Manh Khôi Duong
Manh Khôi Duong

Reputation: 173

Using setText for more strings

I'm currently working on an Android app. This is my code:

FieldSolution.setText("y =","(Double.toString(m))","x + ", "(Double.toString(b))");

I'm trying to print "y = mx + b" whereas m and b are doubles. Somehow I'm getting exceptions.

Where lies my mistake?

Upvotes: 1

Views: 1011

Answers (2)

Reimeus
Reimeus

Reputation: 159754

fieldSolution.setText("y =" + Double.toString(m) + " x + " + Double.toString(b));

or simply

fieldSolution.setText("y =" + m + " x + " + b);

Aside: Use Java naming conventions for variable names

Upvotes: 2

Ted Hopp
Ted Hopp

Reputation: 234795

You can use String.format:

FieldSolution.setText(String.format("y = %fx + %f", m, b));

You can use modifiers on the %f format specifier to control precision and width of the output. You can also, if appropriate, supply a locale as an argument to format().

Upvotes: 1

Related Questions