wood burning stoves 2.0*
The moose likes Java in General and the fly likes DEbugging Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "DEbugging" Watch "DEbugging" New topic


Jesus Doe

Joined: Apr 14, 2006
Posts: 4
I compile my program and its goes fine, when trying to display the last system.out.print I instead get and error message any help?

Enter an integer value:3
3 is positive, odd and small
Enter a double value:3.456
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:819)
at java.util.Scanner.next(Scanner.java:1431)
at java.util.Scanner.nextInt(Scanner.java:2040)
at java.util.Scanner.nextInt(Scanner.java:2000)
at HW1_fabian.main(HW1_fabian.java:45)
Press any key to continue . . .

//Jesus Fabian
//CIN 220039753

import java.util.Scanner;

public class HW1_fabian
public static void main(String args[])

int x;
double round;
Scanner input=new Scanner(System.in);

System.out.print("Enter an integer value:");
System.out.printf("%d is positive, ", x);
System.out.printf("%d is negative, ", x);
System.out.printf("%d is neutral,\n", x);
(x%2 == 0)
System.out.print(" and big\n");
(x>=1 && x<1000)
System.out.print(" and small\n");

System.out.print("Enter a double value:");

//am only doing this to see if something simple would work, but i keep getting the same error
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
The error comes because the nextInt() method of Scanner class throws an exception if it encounters a number that can not be intepreted an an integer. You have to use nextDouble(), nextFloat(), or nextBigDecimal() to read a floating-point number using Scanner class.

Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
It is sorta covered in the JavaRanch Style Guide.
subject: DEbugging
Similar Threads
Displaying the largest and smallest of three integers
variable counter might not have been initialized
cant get output right
specifier format?
I think I'm stuck!!