java.util.InputMismatchException in Java

In this Program i am going to read reverse of 1 to n numbers in Java

Error Message:

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.nextInt(Scanner.java:2117)
       at java.util.Scanner.nextInt(Scanner.java:2076)
       at com.lib.RevrseOf1toNnum.main(RevrseOf1toNnum.java:11)

Error Description:

In this Program we have to give integer numbers,but i gave fractional number as input,so i got an error.  

package com.lib;

import java.util.Scanner;

public class RevrseOf1toNnum {

       public static void main(String[] args)
       {
              Scanner num = new Scanner(System.in);
              System.out.println("Enter a value:");
              int i = num.nextInt();
              for(int j=i;j>=1;j--)
              {
                     System.out.println(j+"\t");
              }
             
       }

}


Error Solution:

package com.lib;

import java.util.Scanner;

public class RevrseOf1toNnum {

       public static void main(String[] args)
       {
              Scanner num = new Scanner(System.in);
              System.out.println("Enter a value:");
              int i = num.nextInt();
              for(int j=i;j>=1;j--)
              {
                     System.out.println(j+"\t");
              }
             
       }

}


Output:
Enter a value:
5
5     
4     
3     
2     
1                     
Share this post :

Post a comment

Please share your valuable feedback and share this article in social media.

 
Support : Ranga Rajesh Kumar
Copyright © 2019. Error Solution - All Rights Reserved
Site Designed by Ranga Rajesh Kumar