0

I want to initialize Double variable before String and another Double after String, but I get a InputMismatchException.

The Simple Program:

import java.util.*;

public class testing {
    
    public static void main(String[] args) {
        
        Scanner in=new Scanner(System.in);
        double balance;
        String accountType;
        double h1;
        balance=in.nextDouble();
        accountType=in.nextLine();
        h1=in.nextDouble();
        System.out.println(balance);
        System.out.println(accountType);
        System.out.println(h1);
        in.close();
    }
}

Error Output:

Exception in thread "main" java.util.InputMismatchException

Brian Tompsett - 汤莱恩
  • 5,195
  • 62
  • 50
  • 120

1 Answers1

0

You have to check your Locale. Each Locale has different ways of writing Double values. Some write it like this 22.2 and some 22,2.

You can specify your Locale like this:

Scanner scanner = new Scanner(System.in).useLocale(Locale.US);
Renis1235
  • 377
  • 1
  • 13