0

I'm trying to write an application that inputs a dollar amount to be printed on a check, I'm having trouble figuring out how to print out the number in a check protected with leading **** asterisks.

Here's the code I have so far

import java.text.DecimalFormat;
import  java.util.Scanner;
import java.text.NumberFormat;
import java.util.Locale;
public class CheckProtection
{
    private static double maxAmount = 1000;
    public static void main (String args [])
    {

        //System.out.printf(checkFormatter.format(check));
        //DecimalFormat checker = new DecimalFormat("******.**");
        //System.out.println(checker);
        boolean validEntry = false;
        while (validEntry == false)
        {
            Scanner userEntry = new Scanner(System.in);
            System.out.println("Enter The check amount under a $1000.00 and greater that 0.");
            if (userEntry.hasNextDouble() == true)
            {
                double check = userEntry.nextDouble();
                if (check > 0)
                {
                    if(check < maxAmount)
                    {
                        validEntry = true;

                        NumberFormat checkFormatter = NumberFormat.getCurrencyInstance();
                        checkFormatter.format(check);
                        System.out.printf("%5s",+ check);
                    }
                }
            }
        }








    }
Paul VanDyke
  • 31
  • 1
  • 1
  • 6

1 Answers1

0

Edit: With the sample inputs and expected output given in the comment, my previous answer was not correct.

Given each of those inputs, this update should produce the expected output:

String value = "$" + String.format("%.2f", check);
System.out.println(value);
String check_fmt = ("*********" + value).substring(value.length());
System.out.println(check_fmt);

Note: borrowed ingenuity from this answer

old answer: Not sure if it could be done with printf(), but String.format works,

String result = String.format("%6.2f", check); // 6 spaces width, 2 spaces precision, 'f' converts the double
System.out.println(result.replace(" ", "*")); 

Note: used 6 spaces width = 3 spaces for whole dollar + 2 spaces for cents + 1 space for decimal

Community
  • 1
  • 1