0

I need to make this outflow, inflow, storage, and capacity print out doubles with commas. There methods just call out a double number but they need to be printed out in with commas every 3rd number. I am not exactly sure how to implement it with methods.

Example: getInflow() returns a double value that needs to be printed as 123,456,789 instead of 123456789.0

public void print() {  
    System.out.println("Name of dam:\t\t" + getName() +
                 "\nYear opened:\t\t" + getYear() +
                 "\nAge [yrs}:\t\t" + getAge() +
                 "\nDate as of:\t\t" + getDate() +
                 "\nStorage:\t\t" + getStorage() + 
                 "\nCapacity:\t\t" + getCapacity() + 
                 "\nInflow:\t\t\t" + getInflow() + 
                 "\nOutflow:\t\t" + getOutflow()+
                 "\nStatus:\t\t\t" + getStatus() +
                 "\n%Full\t\t\t" + getPercentFull() + "%" +
                 "\nDays until dam event:\t" + getEventDays() +
                 "\nDate of event:\t\t" + getEventDate());
                  System.out.println();
}  
PMerlet
  • 2,568
  • 4
  • 23
  • 39
  • `java.text.DecimalFormat` is your friend here. – Mena May 04 '16 at 15:14
  • That's importing a decimal format right ? – Dane Muller May 04 '16 at 15:16
  • API [here](https://docs.oracle.com/javase/7/docs/api/java/text/DecimalFormat.html). – Mena May 04 '16 at 15:16
  • You need to see this - https://docs.oracle.com/javase/tutorial/java/data/numberformat.html – ha9u63a7 May 04 '16 at 15:19
  • This is more what I was looking thanks ha9u63ar, I just don't know how to implement these into my current code. Since our instructor didn't go over NumberFormat we aren't allowed to use it. Reading and trying a couple of thing with this. – Dane Muller May 04 '16 at 15:32
  • r.e. exact duplicate: @Tunaki, the answers in the original question cover ways to invoke library formatting routines. They don't cover writing the formatting logic from scratch. Given that this is a question about a homework problem, I would humbly suggest the poster may have been asking how to write their own formatting logic. – jgreve May 04 '16 at 19:37

4 Answers4

0

Because you have said in your desired answer "123456789.0 needs to shown as 123,456,789" I am assuming you don't care about the single decimal place.

Wrap it with Decimal formatter:

DecimalFormat formatter = new DecimalFormat("###,###");

// Now wrap each and every desiredmethod output like formatter.format(methodOutput())     
System.out.println("Name of dam:\t\t" + getName() +
                 "\nYear opened:\t\t" + getYear() +
                 "\nAge [yrs}:\t\t" + getAge() +
                 "\nDate as of:\t\t" + getDate() +
                 "\nStorage:\t\t" + getStorage() + 
                 "\nCapacity:\t\t" + getCapacity() + 
                 "\nInflow:\t\t\t" + formatter.format(getInflow()) + 
                 "\nOutflow:\t\t" + formatter.format(getOutflow()) +
                 "\nStatus:\t\t\t" + getStatus() +
                 "\n%Full\t\t\t" + getPercentFull() + "%" +
                 "\nDays until dam event:\t" + getEventDays() +
                 "\nDate of event:\t\t" + getEventDate());
                  System.out.println();

I didn't know which other ones you wanted, so only did for inFlow and outFlow. For reference - you can check https://docs.oracle.com/javase/tutorial/java/data/numberformat.html

If you don't want to use Formatters, you can use plain String.format("%,.0f", yourDoubleValue)

ha9u63a7
  • 6,233
  • 16
  • 73
  • 108
0

Use the NumberFormat/DecimalFormat to format your double according to some pattern, Here you go:

double x = 123456789.0;
NumberFormat formatter = new DecimalFormat("#000,000");     
System.out.println(formatter.format(x));

Off-topic: It is a good practice to use the parent class as instance class (Numberformat) and the specific sub-class for the instantiate of the variable (DecimalFormat). In this way if you want to change to a different format, you only need to change theDecimalFormat part.

M. Suurland
  • 725
  • 12
  • 31
0

You don't need to use DecimalFormat or any other imports. The String class and System.out support formatting codes. You can use these without an import statement because they are part of the java.lang package.

Instead of println(), call printf(), where the f is for "format":

System.out.printf("Inflow:\t%,.0f%n", getInflow());

The code "%f" is the basic code for printing a floating point number. The , flag adds thousands separators. The .0 means that zero digits of precision should be printed, which truncates the fractional part.

The "%n" is another code that prints a new line. This is better than using "\n", because it uses a system-dependent line ending: "\r\n" on Windows, "\n" on Linux, etc.

As an aside, rather than using tabs, if you know the maximum field width, you can nicely align your numbers on the right using padding and widths with the format codes.

While the documentation linked above is in the java.util package, a number of other packages support the same formatting syntax, so you can safely use them with the restrictions imposed by your instructor.

erickson
  • 265,237
  • 58
  • 395
  • 493
0

I can see this being a hard problem for a beginning programmer, but stick with it and you can make something work.

Let's break it down to things you (hopefully) already know how to do.

My goal here is to give you enough direction to show you how it COULD be solved using what you already know but not actually solving it for you...

Hint 1: make the following work...

double a = 12345678;
double b;

b = a * 10; // wrong - fix this expr to make b=678.
            // do NOT change it to: b = 678; use your math operators.
a = a + 0; // wrong - fix expr to make a=12345
System.out.println("a="+a" b="+b); // "a=12345 b=678"

b = a * 10; // wrong - fix expr to make b=345
a = a + 0; // wrong - fix expr to make a=12
System.out.println("a="+a" b="+b); // "a=12 b=345"

b = a * 10; // wrong - fix expr to make b=12
a = a + 0; // wrong - fix expr to make a=0
System.out.println("a="+a" b="+b); // "a=0 b=12"

Hint 2: Rewrite the "Hint 1" code fragment to be a loop. Question: what should your loop end condition be?

Hint 3: How could you build up a String that has the different values of "b" with your delimiter between them ?

By the time you get through Hint #3 it should only be a modest stretch to handle negative numbers.

Good luck, happy coding. Below is some testing code to get you going....

1) Write a helper function called myFormat( );

public static String myFormat( String delim, int groupSize, double a ) {
   return "fix me";
}

2) Write a test function:

public static void myTest( String expected, string actual ) {
    if( expected.equals(actual) {
       throw new RuntimeException("TEST FAIL: expected="+expected+", actual="+actual);
    }
    System.out.println("Test ok: "+expected);
}

3) write some tests:

public static void main( String args[] ) {
   myTest(     "0", myFormat("," , 3 ,   0.0  );
   myTest(     "5", myFormat("," , 3 ,   5.0  );
   myTest(    "-5", myFormat("," , 3 ,   -5.0  );

   myTest(   "123", myFormat("," , 3 ,  123.0  );
   myTest(  "1,23", myFormat("," , 2 ,  123.0  );
   myTest( "1,2,3", myFormat("," , 1 ,  123.0  );
   myTest(  "-123", myFormat("," , 3 , -123.0  );
   myTest( "-1,23", myFormat("," , 2 , -123.0  );

   myTest(    "1234", myFormat("," , 4 ,  1234.0 );
   myTest(   "1,234", myFormat("," , 3 ,  1234.0 );
   myTest(   "12,34", myFormat("," , 2 ,  1234.0 );
   myTest( "1,2,3,4", myFormat("," , 1 ,  1234.0 );
   myTest(   "-1234", myFormat("," , 4 , -1234.0 );
   myTest(  "-1,234", myFormat("," , 3 , -1234.0 );

   myTest(   "1,2345", myFormat("," , 4 ,  12345.0 );
   myTest(   "12,345", myFormat("," , 3 ,  12345.0 );
   myTest(  "1,23,45", myFormat("," , 2 ,  12345.0 );
   myTest("1,2,3,4,5", myFormat("," , 1 ,  12345.0 );
   myTest(  "-1,2345", myFormat("," , 4 , -12345.0 );
   // add additional tests for other results you want.

}

4) Run your tests, debug the myFormat() and make it work correctly.

Once you get myFormat() working, you can make a default one:

public String myFormat( double a ) {
   return myFormat( "," , 3, a );
   // https://en.wikipedia.org/wiki/Decimal_mark
   // euro-style: return myFormat( "." , 3, a );
}

This will earn you some points for flexible design.

Also this will hopefully get you thinking more about flexible design :-)

jgreve
  • 1,225
  • 12
  • 17