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 :-)