# Matrix 2x2 Construction and toString()

Page 1 of 1

## 2 Replies - 294 Views - Last Post: 09 October 2012 - 01:16 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=294852&amp;s=38886e7cdb6c58880e5d51dd04848fb6&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 lsutigers43

Reputation: 0
• Posts: 2
• Joined: 09-October 12

# Matrix 2x2 Construction and toString()

Posted 09 October 2012 - 01:05 AM

I have a quick question regarding using the toString method.

My code:

```package mat2by2;

public class Mat2By2
{
//instance variables of Mat2By2 Objects
private double bottomLeft;
private double bottomRight;
private double topLeft;
private double topRight;

//creates a 2x2 matrix whose entries are all 0
public Mat2By2(){}

//creates a 2x2 matrix with the specified parameters
public Mat2By2(double tL, double tR, double bL, double bR)
{

topLeft = tL;
topRight = tR;
bottomLeft = bL;
bottomRight = bR;

}

{
return new Mat2By2(topLeft + m.topLeft, topRight + m.topRight, bottomLeft + m.bottomLeft, bottomRight + m.bottomRight);
}

@Override
public String toString()
{
return String.format("{{%d,%d},{%d,%d}}",topLeft, topRight, bottomLeft, bottomRight);
}

public static void main(String[] args)
{
Mat2By2 m1 = new Mat2By2(1,2,3,4);
Mat2By2 m2 = new Mat2By2(2,3,4,1);

System.out.println(m2);

}
}

```

I'm getting several formatting errors when I'm trying to print out the values of the Matrix.

Quote

Exception in thread "main" java.util.IllegalFormatConversionException: d != java.lang.Double
at java.util.Formatter\$FormatSpecifier.failConversion(Formatter.java:3999)
at java.util.Formatter\$FormatSpecifier.printInteger(Formatter.java:2709)
at java.util.Formatter\$FormatSpecifier.print(Formatter.java:2661)
at java.util.Formatter.format(Formatter.java:2433)
at java.util.Formatter.format(Formatter.java:2367)
at java.lang.String.format(String.java:2769)
at mat2by2.Mat2By2.toString(Mat2By2.java:35)
at java.lang.String.valueOf(String.java:2826)
at java.io.PrintStream.println(PrintStream.java:771)
at mat2by2.Mat2By2.main(Mat2By2.java:43)
Java Result: 1

I have followed every example I could find, but I am obviously missing something or this is a special case that requires a few additions.

The sample run given should look like this:

Quote

m1 + m2 = {{1.000000,2.000000},{3.000000,4.000000}}

I have tried a plethora of variations to the toString() Class, and I also varied how I implemented the class in the tester application.

Any help would be appreciated.

Is This A Good Question/Topic? 0

## Replies To: Matrix 2x2 Construction and toString()

### #2 lsutigers43

Reputation: 0
• Posts: 2
• Joined: 09-October 12

## Re: Matrix 2x2 Construction and toString()

Posted 09 October 2012 - 01:11 AM

2 Minutes after posting I realize %f is the format specifier, not \$d.

Fixed the problem.

### #3 CasiOo

• D.I.C Lover

Reputation: 996
• Posts: 2,212
• Joined: 05-April 11

## Re: Matrix 2x2 Construction and toString()

Posted 09 October 2012 - 01:16 AM

%d are for decimal integers, you will have to use f for doubles

```return String.format("{{%f,%f},{%f,%f}}",topLeft, topRight, bottomLeft, bottomRight);

```