Results 1 to 4 of 4
  1. #1
    zetaXX is offline Member
    Join Date
    Sep 2013
    Posts
    1
    Rep Power
    0

    Default Problem with decimals on java

    Hi! Im new programming with java and I was doing a simple code, it works fine, but at the moment I try to use a decimal number in the console I get an error (tried both . and ,)
    here is my code
    Java Code:
    import java.util.*;
    
    
    
    public class Ejercicio3 {
    	
    	public static void main(String[] args) {
    		double pago;
    		double extras;
    		int horas = 1;
    		double precio;
    		Scanner id1= new Scanner(System.in);
    		Scanner id2= new Scanner(System.in);
    
    	    
            do{
            	System.out.println("Escriba el número de horas: ");
        	    horas=id1.nextInt();
        		System.out.println("Escriba el precio de las horas: ");
        	    precio=id1.nextInt();
        	    if(horas> 40){
        	    	extras = horas - 40;
        	    	extras = extras * (precio+(precio/2));
        	    	pago = extras + (horas * precio);
        	    }else{
        	    	pago = horas * precio;
        	    	
        	    }
        	    System.out.println(pago + (" euros"));
    	    }while(horas!= 0);
    	    	
    	    }
    	}
    and there is the error I get when I try to write a decimal:

    Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Unknown Source)
    at java.util.Scanner.next(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at Ejercicio3.main(Ejercicio3.java:21)

    Btw sorry because of variables language :S

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,051
    Blog Entries
    7
    Rep Power
    23

    Default Re: Problem with decimals on java

    Your program is reading ints (nextInt()) while it should be reading doubles (nextDouble()).

    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

  3. #3
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,063
    Rep Power
    8

    Default Re: Problem with decimals on java

    Yes fetching an decimal with nextInt() is not going to work; nextInt() returns only integral values, as in numbers without a decimal. Check out the API documentation to learn which method you should be using:

    Scanner (Java Platform SE 7 )

    and you should study up on data types:

    Primitive Data Types (The Java™ Tutorials > Learning the Java Language > Language Basics)

    edit: being ninja'd by Jos; its a privilege, not a right.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  4. #4
    Aries. is offline Member
    Join Date
    Jan 2013
    Posts
    22
    Rep Power
    0

    Default Re: Problem with decimals on java

    Quote Originally Posted by zetaXX View Post
    Java Code:
    import java.util.*;
    
    
    
    public class Ejercicio3 {
    	
    	public static void main(String[] args) {
    		double pago;
    		double extras;
    		int horas = 1;
    		double precio;
    		Scanner id1= new Scanner(System.in);
    		Scanner id2= new Scanner(System.in);
    
    	    
            do{
            	System.out.println("Escriba el número de horas: ");
        	    horas=id1.[COLOR="#FF0000"]nextInt()[/COLOR];
        		System.out.println("Escriba el precio de las horas: ");
        	    precio=id1.[COLOR="#FF0000"]nextInt()[/COLOR];
        	    if(horas> 40){
        	    	extras = horas - 40;
        	    	extras = extras * (precio+(precio/2));
        	    	pago = extras + (horas * precio);
        	    }else{
        	    	pago = horas * precio;
        	    	
        	    }
        	    System.out.println(pago + (" euros"));
    	    }while(horas!= 0);
    	    	
    	    }
    	}
    Use (nextDouble()), see the highlighted text.


    I wonder why you were using 2 scanner.

    Hello.

Similar Threads

  1. How do I enter decimals in Java
    By teohzj123 in forum New To Java
    Replies: 6
    Last Post: 08-25-2013, 03:49 PM
  2. How to subtract numbers with decimals with Java?
    By coding in forum New To Java
    Replies: 3
    Last Post: 01-27-2013, 02:47 AM
  3. Java does not read the decimals?
    By Josep_16 in forum New To Java
    Replies: 4
    Last Post: 08-08-2011, 09:23 AM
  4. Decimals in java help!!
    By Gold in forum New To Java
    Replies: 3
    Last Post: 12-10-2009, 12:25 AM
  5. Replies: 1
    Last Post: 07-02-2008, 01:58 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
  •