-1

I'm working on a simple mortgage calculator right now cause I'm new to coding and not very good at it I'm trying to get ask the user how big the loan is so I'm trying to user Number.format for currency so I can set it up as a currency

What I'm trying right now looks like this

NumberFormat percent = NumberFormat.getPercentInstance();
NumberFormat currency = NumberFormat.getCurrencyInstance();
Scanner scanner = new Scanner(System.in);


System.out.print("How big is your loan: ");
int loanSize = Integer.parseInt(currency.format(scanner.nextInt()));`

This is returning

Exception in thread "main" java.lang.NumberFormatException: For input string: "$1,000,000.00"
    at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:67)
    at java.base/java.lang.Integer.parseInt(Integer.java:588)
    at java.base/java.lang.Integer.parseInt(Integer.java:685)
    at Main.main(Main.java:23)`

What I excepted is that it would take the users input which starts as a string and parse it to an integer then as the user was putting in the input (Ex: 1000000) it would format it to look like a currency (If there are any problems with how im asking sorry this is my first time asking a question on stack overflow)

1
  • Why are you parsing a string into an integer, converting that to another string (that isn't numeric), and then trying to parse that back into an integer? Don't write code like this. Commented Apr 10 at 8:30

2 Answers 2

2
NumberFormat percent = NumberFormat.getPercentInstance(); 
NumberFormat currency = NumberFormat.getCurrencyInstance(); 
Scanner scanner = new Scanner(System.in); 

System.out.print("How big is your loan: "); 
int loanSize = scanner.nextInt(); 

System.out.println("Your loan amount is: " + currency.format(loanSize));

You're trying to format a number as currency and then parse it back as an integer, which doesn't work because currency formatting adds symbols and commas that can't be parsed as integers.

Let me explain what's happening:

  1. scanner.nextInt() reads an integer from user input

  2. currency.format() converts that integer to a formatted currency string (e.g., "$1,000.00")

  3. Integer.parseInt() tries to convert that currency string back to an integer, which fails because it can't parse "$1,000.00" as an integer

Sign up to request clarification or add additional context in comments.

Comments

0

Still seems, no input was mentioned, conversion from String to Number.

String loanText;

loanText = currency.format(1_000_000L);
System.out.printf("You can loan upto %s.%n", loanText);

loanText = currency.format(1_000_000.02);
System.out.printf("You can loan upto %s.%n", loanText);

try {
    Number loan;
  
    loanText = "$1,000,000.00";
    loan = currency.parse(loanText);
    System.out.printf("Loan: %s (%s).%n", loan, loan.getClass().getSimpleName());

    loanText = "$1,000,000.02";
    loan = currency.parse(loanText);
    System.out.printf("Loan: %s (%s).%n", loan, loan.getClass().getSimpleName());
} catch (ParseException ex) {
    System.out.println(ex);
}

Depending on the decimals you'll get a long or double for the Number. The Number interface provides conversion between numerical classes like loan.doubleValue().

You can loan upto $1,000,000.00.
You can loan upto $1,000,000.02.
Loan: 1000000 (Long).
Loan: 1000000.02 (Double).

The above was for Locale.US. For Locale.GERMANY (for instance):

  loanText = "1.000.000,00\u00a0€";
  loanText = "1.000.000,02\u00a0€";

Here the currency comes after the number, separated by a non-breaking space, so no line wrapping may occur.

Requiring to enter a fixed currecny is a PITA.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.