Car price calculator Im writing this simple program for school and using NumberFormat on all the variables seems very repetitive (Note that we were asked to use the NumberFormat method). Is there any way to clean this code so it doesn't look so cluttered?
import java.util.Scanner;
import java.text.NumberFormat;

public class CarPrice {

    public static void main(String[] args) {

        double priceInitial;
        double HST;
        double PDI_CHARGE = 0.02;

        String makeModel;

        Scanner scan = new Scanner(System.in);

        System.out.print(&quot;makeModel?: &quot;);
        makeModel = scan.nextLine().toUpperCase();

        do {
            System.out.print(&quot;Price?: &quot;);
            priceInitial = scan.nextDouble();
            if (priceInitial &lt; 0) {
                System.out.println(&quot;Enter A Value  Above 0&quot;);
            } else {
                System.out.println(priceInitial);
                break;
            }
        } while (true);

        do {
            System.out.print(&quot;HST?: &quot;);
            HST = scan.nextDouble();
            if (HST &lt; 0) {
                System.out.println(&quot;Enter A Value  Above 0&quot;);
            } else {
                System.out.println(HST);
                break;
            }
        } while (true);

        double tax = priceInitial * (HST / 100) + priceInitial;

        double PDI = (tax + priceInitial) * PDI_CHARGE;
        double AfterPDI = PDI + tax;

        String PDI_Print = NumberFormat.getCurrencyInstance().format(PDI);
        String tax_Print = NumberFormat.getCurrencyInstance().format(tax);
        String priceInitial_Print = NumberFormat.getCurrencyInstance().format(priceInitial);
        String tax_Amount = NumberFormat.getPercentInstance().format(HST / 100);
        String AfterPDI_Print = NumberFormat.getCurrencyInstance().format(AfterPDI);

        System.out.println(&quot;Congrats! Your &quot; + makeModel + &quot; is &quot; + priceInitial_Print +
                &quot;\n Your total after &quot; + tax_Amount + &quot; HST is &quot; + tax_Print +
                &quot;\n with a PDI fee of &quot; + PDI_Print +
                &quot;\n making your total &quot; + AfterPDI_Print);


    }


}
The most important approach to reduce duplicated code is to put the repetitive code into a parameterized method. In case you use an integrated Development Environment (IDE) like eclipse, IntelliJ or alike they have an automated Refactoring to assist you with that. (lookup your IDEs online help how to invoke it).
Here is how I do this step by step
The initial step is to identify the duplicated  parts. In your case the two while/do loops are the first candidates. They are similar that means that there are variables and literals that differ. We need to make them looking the same so that we can replace all of them with the call to the new method to be created.

To be able to do that we need to provide a unique variable scope for both parts. This is done by enclosing each of the loop in braces {}
 {
     do {
         System.out.print(&quot;Price?: &quot;);
         priceInitial = scan.nextDouble();
         if (priceInitial &lt; 0) {
             System.out.println(&quot;Enter A Value  Above 0&quot;);
         } else {
             System.out.println(priceInitial);
             break;
         }
     } while (true);
 }
 {
     do {
         System.out.print(&quot;HST?: &quot;);
         HST = scan.nextDouble();
         if (HST &lt; 0) {
             System.out.println(&quot;Enter A Value  Above 0&quot;);
         } else {
             System.out.println(HST);
             break;
         }
     } while (true);
 }


Inside this braces we declare and use new variables, that have the same names in both places (it is important that the new variables are declared in each block individually and not reused in the lower block!):
 {
     String message = &quot;Price?: &quot;;
     double userInput = 0;
     do {
         System.out.print(message);
         userInput = scan.nextDouble();
         if (userInput &lt; 0) {
             System.out.println(&quot;Enter A Value  Above 0&quot;);
         } else {
             System.out.println(userInput);
             break;
         }
     } while (true);
     priceInitial = userInput;
 }
 {
     String message = &quot;HST?: &quot;;
     double userInput = 0;
     do {
         System.out.print(message);
         userInput = scan.nextDouble();
         if (userInput &lt; 0) {
             System.out.println(&quot;Enter A Value  Above 0&quot;);
         } else {
             System.out.println(userInput);
             break;
         }
     } while (true);
     HST = userInput;
 }


Now we select all the lines in the first part that look exactly the same as in the second part. Then you can apply the extract method refactoring of the IDE. The result should look like this:
 {
     String message = &quot;Price?: &quot;;
     // replaces by the IDE
     double userInput = extracted(scan, message);
     priceInitial = userInput;
 }


  // new method created by the IDE behind main()
 private static double extracted(Scanner scan, String message) {
     double userInput = 0;
     do {
         System.out.print(message);
         userInput = scan.nextDouble();
         if (userInput &lt; 0) {
             System.out.println(&quot;Enter A Value  Above 0&quot;);
         } else {
             System.out.println(userInput);
             break;
         }
     } while (true);
     return userInput;
 }

Of cause you should give it a better name then what the IDE suggested.

In case your IDE did not replace the other occurrence of the selected code itself  you copy the method call from the first block the other one and delete the obsolete code:
  {
      String message = &quot;Price?: &quot;;
      double userInput = extracted(scan, message);
      priceInitial = userInput;
  }
  {
      String message = &quot;HST?: &quot;;
      double userInput = extracted(scan, message);
      HST = userInput;
  }




Then inline the new variables in both blocks
 {
     priceInitial = extracted(scan, &quot;Price?: &quot;);
 }
 {
     HST = extracted(scan, &quot;HST?: &quot;);
 }


And finally remove the blocks too.
 priceInitial = extracted(scan, &quot;Price?: &quot;);
 HST = extracted(scan, &quot;HST?: &quot;);



Why do I suggest this rather complicated approach?

Each single step is a very small change to your code.
You don't need to think about the signature of the new method beside giving it a good name.
After each step you can compile and run the code to check if it still does what it should. (having Unittest will be handy...)