1

I'm taking my java class, and I'm working on a Tsubo calculator for my assignment. I don't usually ask questions on stack overflow so forgive me if this seems basic. I've done some searching here and tried some of the solutions but none have worked in my case. I'm going to copy just part of my conversion below

```
        System.out.println("You have chosen to convert square feet to Tsubo");
        System.out.println("Please enter the total sqft you are looking to convert");

        sqftInput = keyboard.nextInt();

         // Double is converted to string so out put remains an object of the same data type

        sqftResult = sqftInput / TSUBO;
        DecimalFormat sqftFormatted = new DecimalFormat("#####.00");


        sqftResultAsString = Double.toString(sqftFormatted);
        System.out.println(sqftInput + " is equal to :" + sqftResultAsString  + " Tsubo"); ` 

When I do this, it tells me I can't format a double that the type is not applicable to arguments for DecimalFormat.

When I change it to look like this (offending line is commented out)

' System.out.println("You have chosen to convert square feet to Tsubo"); System.out.println("Please enter the total sqft you are looking to convert");

        sqftInput = keyboard.nextInt();

        // Double is converted to string so out put remains an object of the same data type
        sqftResult = sqftInput / TSUBO;
        DecimalFormat sqftFormatted = new DecimalFormat("#####.00");


        //sqftResultAsString = Double.toString(sqftFormatted);
        System.out.println(sqftInput + " is equal to :" + sqftFormatted  + " Tsubo");

The program compiles and runs, and when I use 5238 as an input number, then my output looks like this --> 5238.0 is equal to :java.text.DecimalFormat@674dc Tsubo

instead of actually displaying the answer which is 325.07.

I've also tried using some regex formatting using the stringFormat but i wind up getting illegalformatting exception.

Long story short, What I'm trying to achieve is an output that is formatted to 2 decimal places, and then converted to a string that the system outputs as an answer. What am I missing? Is this Decimal Format automatically converting this to a string object?

Jordan
  • 17
  • 6
  • could you please point to the line where you pass your calculation result to the `DecimalFormat` object? – Timothy Truckle Jul 23 '17 at 17:51
  • Possible duplicate of [How to round a number to n decimal places in Java](https://stackoverflow.com/questions/153724/how-to-round-a-number-to-n-decimal-places-in-java) –  Jul 23 '17 at 19:45
  • and this is exactly why I avoid stackoverflow. If other answers are described in a manner that is not understood or unclear, you are pretty much on your own and are dissuaded from asking questions for fear of being downvoted and more – Jordan Jul 24 '17 at 16:38

1 Answers1

1

DecimalFormat is used to produce a formatted String. If you print it directly, it will show its (kind of, it's called idenity hashcode) memory address cause that is what the DecimalFormat.toString() does.

Instead, you should use it to produce your output string and print that directly.

As follows:

 sqftResult = sqftInput / TSUBO;
        DecimalFormat sqftFormatted = new DecimalFormat("#####.00");
        String out =  sqftFormatted.format(sqftResult); // or whatever you want to print

        //sqftResultAsString = Double.toString(sqftFormatted);
        System.out.println(sqftInput + " is equal to :" + out  + " Tsubo");

See docs for more details

Davide Spataro
  • 6,761
  • 1
  • 21
  • 36
  • So i'm assuming(if you've worked with python) that it's printing the object address instead and thats why you have to add a __str__ method to the class to produce the result. So its already turning it into a string object and printing the location? that worked perfectly, thanks! I couldn't figure out what was wrong. Originally i was trying to format it earlier with strong.format("%.2f); that was a nightmare – Jordan Jul 23 '17 at 18:02
  • I tried, it made my head spin and I couldnt figure it out. (that's why I'm taking this class in school lol) – Jordan Jul 23 '17 at 18:04
  • When printing an object like that, javais automatically calling the tostriing method. If it is not overridden the default one is called which prints the hashcode. Default format does not override the toString :) – Davide Spataro Jul 23 '17 at 18:16
  • I know I could have just used the printf method to output it that way, but I wanted to actually store it in a variable so I can use the actual value later if i needed to expand the program, which i think we are going to do in class. (we're supposed to make additions and expand our program weekly with new things) – Jordan Jul 23 '17 at 18:32