Results 1 to 7 of 7
  1. #1
    Juukamen is offline Member
    Join Date
    Oct 2011
    Location
    Tromsų
    Posts
    58
    Rep Power
    0

    Default scanner double problem

    Running this code and i'm getting InputMismatchException, the variable is declared as an double and i'm using nextDouble() so why is it an mismatch ?
    Java Code:
    import java.util.Scanner;
    
    public class day2 {
    
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		int i = 4;
    		double d = 4.0;
    		String s = "HackerRank";
    		
    		Scanner scan = new Scanner(System.in);
    		
            /* Declare second integer, double, and String variables. */
    		int intI;
    		double doubleD;
    		String stringS;
    		
            /* Read and save an integer, double, and String to your variables.*/
            // Note: If you have trouble reading the entire String, please go back and review the Tutorial closely.
    		System.out.println("Write an int");
    		intI = scan.nextInt();
    		
    		System.out.println("Write and double");
    		doubleD = scan.nextDouble();
    		
    		System.out.println("Write a String");
    		stringS = scan.nextLine();
            
            /* Print the sum of both integer variables on a new line. */
    		System.out.println("The sum of int = " + (i + intI) );
    		
            /* Print the sum of the double variables on a new line. */
    		System.out.println("The sum of the doubles is = "+ ( d + doubleD ) );
    
    		// Concatenate and print the String variables on a new line;
    		//the 's' variable above should be printed first.
    		System.out.printf("%s%s%n", s, stringS);
    		
    		scan.close();
    
    	}
    
    }
    Write and double
    2.222

    Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Scanner.java:864)
    at java.util.Scanner.next(Scanner.java:1485)
    at java.util.Scanner.nextDouble(Scanner.java:2413)
    at day2.main(day2.java:24)

    Process finished with exit code 1

    nextDouble
    public double nextDouble()
    Scans the next token of the input as a double. This method will throw InputMismatchException if the next token cannot be translated into a valid double value. If the translation is successful, the scanner advances past the input that matched.
    If the next token matches the Float regular expression defined above then the token is converted into a double value as if by removing all locale specific prefixes, group separators, and locale specific suffixes, then mapping non-ASCII digits into ASCII digits via Character.digit, prepending a negative sign (-) if the locale specific negative prefixes and suffixes were present, and passing the resulting string to Double.parseDouble. If the token matches the localized NaN or infinity strings, then either "Nan" or "Infinity" is passed to Double.parseDouble as appropriate.

    Returns:
    the double scanned from the input
    Throws:
    InputMismatchException - if the next token does not match the Float regular expression, or is out of range
    NoSuchElementException - if the input is exhausted
    IllegalStateException - if this scanner is closed

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    25

    Default Re: scanner double problem

    Is that the full output, as I don't see the bit where it prompts for an int.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    Juukamen is offline Member
    Join Date
    Oct 2011
    Location
    Tromsų
    Posts
    58
    Rep Power
    0

    Default Re: scanner double problem

    here is the full output.

    "C:\Program Files\Java\jdk1.8.0_151\bin\java" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2017.3.2\lib\idea_rt.jar=18751:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2017.3.2\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_151\jre\lib\charsets.jar;C:\Pr ogram Files\Java\jdk1.8.0_151\jre\lib\deploy.jar;C:\Prog ram Files\Java\jdk1.8.0_151\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_151\jre\lib\ext\cldrdata.jar;C :\Program Files\Java\jdk1.8.0_151\jre\lib\ext\dnsns.jar;C:\P rogram Files\Java\jdk1.8.0_151\jre\lib\ext\jaccess.jar;C: \Program Files\Java\jdk1.8.0_151\jre\lib\ext\jfxrt.jar;C:\P rogram Files\Java\jdk1.8.0_151\jre\lib\ext\localedata.jar ;C:\Program Files\Java\jdk1.8.0_151\jre\lib\ext\nashorn.jar;C: \Program Files\Java\jdk1.8.0_151\jre\lib\ext\sunec.jar;C:\P rogram Files\Java\jdk1.8.0_151\jre\lib\ext\sunjce_provide r.jar;C:\Program Files\Java\jdk1.8.0_151\jre\lib\ext\sunmscapi.jar; C:\Program Files\Java\jdk1.8.0_151\jre\lib\ext\sunpkcs11.jar; C:\Program Files\Java\jdk1.8.0_151\jre\lib\ext\zipfs.jar;C:\P rogram Files\Java\jdk1.8.0_151\jre\lib\javaws.jar;C:\Prog ram Files\Java\jdk1.8.0_151\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_151\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_151\jre\lib\jfxswt.jar;C:\Prog ram Files\Java\jdk1.8.0_151\jre\lib\jsse.jar;C:\Progra m Files\Java\jdk1.8.0_151\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_151\jre\lib\plugin.jar;C:\Prog ram Files\Java\jdk1.8.0_151\jre\lib\resources.jar;C:\P rogram Files\Java\jdk1.8.0_151\jre\lib\rt.jar;C:\Users\Ne tti\eclipse-workspace\HackerRank\out\production\Day2" day2
    Write an int
    2
    Write and double
    2.222
    Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Scanner.java:864)
    at java.util.Scanner.next(Scanner.java:1485)
    at java.util.Scanner.nextDouble(Scanner.java:2413)
    at day2.main(day2.java:24)

    Process finished with exit code 1
    just for fun, tries this code on https://www.jdoodle.com/online-java-compiler

    Java Code:
    import java.util.Scanner;
    
    public class day2 {
    
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		int i = 4;
    		double d = 4.0;
    		String s = "HackerRank";
    		
    		Scanner scan = new Scanner(System.in);
    		
            /* Declare second integer, double, and String variables. */
    		int intI;
    		double doubleD;
    		String stringS;
    		
            /* Read and save an integer, double, and String to your variables.*/
            // Note: If you have trouble reading the entire String, please go back and review the Tutorial closely.
    		System.out.println("Write an int");
    		intI = scan.nextInt();
    		
    		System.out.println("Write an double");
    		doubleD = scan.nextDouble();
    		
    		System.out.println("Write a String");
    		stringS = scan.nextLine();
            
            /* Print the sum of both integer variables on a new line. */
    		System.out.println("The sum of int = " + (i + intI) );
    		
            /* Print the sum of the double variables on a new line. */
    		System.out.println("The sum of the doubles is = "+ ( d + doubleD ) );
    
    		// Concatenate and print the String variables on a new line;
    		//the 's' variable above should be printed first.
    		System.out.printf(stringS);
    		
    		scan.close();
    
    	}
    }

    the inndata are: 2 - 2.23 - Test

    output

    Write an int
    Write an double
    Write a String
    The sum of int = 6
    The sum of the doubles is = 6.23
    need to work on the string but here the double is working perfectly.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    25

    Default Re: scanner double problem

    What's your locale?
    COuld it be that your double input ought to be '2,222', instead of '2.222'?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    Juukamen is offline Member
    Join Date
    Oct 2011
    Location
    Tromsų
    Posts
    58
    Rep Power
    0

    Default Re: scanner double problem

    Yes, it's working with '2,222' instead of '2.222'
    been wondering on why it's like that ?

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    25

    Default Re: scanner double problem

    Your computer's default locale is set to one that uses commas instead of stops for decimal points.
    From the Scanner API doc:
    "
    An instance of this class is capable of scanning numbers in the standard formats as well as in the formats of the scanner's locale. A scanner's initial locale is the value returned by the Locale.getDefault(Locale.Category.FORMAT) method; it may be changed via the useLocale(java.util.Locale) method.
    "

    So you can find out what is being used by default, and you can change what locale the Scanner is going to use.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    Juukamen is offline Member
    Join Date
    Oct 2011
    Location
    Tromsų
    Posts
    58
    Rep Power
    0

    Default Re: scanner double problem

    thanks for the replies. got the task done and some new things learned.. again :D

Similar Threads

  1. Problem with scanner?
    By Modulus in forum New To Java
    Replies: 4
    Last Post: 02-03-2013, 04:17 AM
  2. problem with scanner
    By fiction in forum New To Java
    Replies: 3
    Last Post: 02-03-2013, 12:32 AM
  3. Replies: 1
    Last Post: 09-20-2012, 09:54 AM
  4. Double with Scanner
    By jjitss in forum New To Java
    Replies: 8
    Last Post: 02-11-2012, 08:05 PM
  5. Replies: 2
    Last Post: 01-10-2012, 01:10 PM

Posting Permissions

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