aspose file tools*
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 Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "DEbugging" Watch "DEbugging" New topic
Author

DEbugging

Jesus Doe
Greenhorn

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:");
x=input.nextInt();
{
if
(x>0)
System.out.printf("%d is positive, ", x);
if
(x<0)
System.out.printf("%d is negative, ", x);
if
(x==0)
System.out.printf("%d is neutral,\n", x);
{
if
(x%2 == 0)
System.out.print("even");
else
System.out.print("odd");
}
if
(x>1000)
System.out.print(" and big\n");
if
(x>=1 && x<1000)
System.out.print(" and small\n");
}


System.out.print("Enter a double value:");
round=input.nextInt();

//am only doing this to see if something simple would work, but i keep getting the same error
System.out.print(round);
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
 
 
subject: DEbugging