Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Oct 2007
    Posts
    73
    Thanks
    26
    Thanked 0 Times in 0 Posts

    Scanner nextDouble - decimal comma or point

    I'm just starting with Java and the following is not quite clear to me.
    I wrote this piece of very basic code and it only let's me input numbers with a decimal comma, not with a decimal point.

    I thought all computer input had to be with a decimal point? Or is this because of locale settings on my pc?
    (only tested in Eclips SDK)

    Code:
    import java.util.Scanner;
    public class ingave {
    	private static Scanner myScannerThree;
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		myScannerThree = new Scanner(System.in);
    		double amount;
    		double getal;
    		
    		System.out.print("Give me a number: ");
    		getal = myScannerThree.nextDouble();
    		amount = getal + 2.95;
    		
    		System.out.print("You entered ");
    		System.out.print(getal);
    		System.out.print(". The total amount is now: ");
    		System.out.println(amount);
    	}
    
    }
    Last edited by friz; 10-31-2012 at 02:24 AM.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Yeppers, Scanner is locale aware, so the number format is based on your system locale.
    You can change it using the scanner's useLocale(Locale) method.

  • Users who have thanked Fou-Lu for this post:

    friz (10-31-2012)

  • #3
    New Coder
    Join Date
    Oct 2007
    Posts
    73
    Thanks
    26
    Thanked 0 Times in 0 Posts
    Thank you!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •