java.lang.IllegalStateException in Java

In this Program I am going to read 1 to n numbers in Java.
Error Message: java.lang.IllegalStateException


Error Description:
In this program actually we have to give numbers as input,but i gave string as input so i got an error.  

package com.lib;

import java.util.Scanner;

public class Print1toN
{
       public static void main(String[] args)
       {
      
              Scanner num = new Scanner(System.in);
              System.out.println("enter n value:");
              //during run time i have given string input, so we will get run time error here.
              int n = num.nextInt();
              for(double a=1.0;a<=n;a++)
              {
                     System.out.println(a);
              }num.close();
       }
}

Error Solution:
package com.lib;

import java.util.Scanner;

public class Print1toN
{
       public static void main(String[] args)
       {
      
              Scanner num = new Scanner(System.in);
              System.out.println("enter n value:");
              //during run time i have given string input, so we will get run time error here.
              int n = num.nextInt();
              for(double a=1.0;a<=n;a++)
              {
                     System.out.println(a);
              }num.close();
       }
}



Output:
enter n value:
5
1.0
2.0
3.0
4.0
5.0
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