---
title: "How do we check for an integer user input in java"  
description: "How do we check for an integer user input in java"  
author: "Anonymous User"  
published: 2015-10-07  
updated: 2015-10-07  
canonical: https://www.mindstick.com/forum/33497/how-do-we-check-for-an-integer-user-input-in-java  
category: "java"  
tags: ["exception handling", "java"]  
reading_time: 2 minutes  

---

# How do we check for an integer user input in java

Hi Everyone, i am working on a java [application](https://www.mindstick.com/articles/12824/calculator-application-in-android), in which i took [integer](https://answers.mindstick.com/qa/113667/write-code-for-roman-to-integer) [input from the user](https://www.mindstick.com/forum/158952/what-are-the-different-methods-for-reading-input-from-the-user-in-python). If the user enter an integer than it is [working fine](https://answers.mindstick.com/qa/95550/why-is-the-safari-browser-not-working-fine), but in case he inputs a digit or any [decimal](https://www.mindstick.com/forum/34709/please-write-a-program-for-decimal-to-binary-conversion-in-c-sharp) number , the application throws an exception. So i want to implement a [validation](https://www.mindstick.com/articles/12234/validation-using-data-annotation-using-entity-framework) for [user input](https://www.mindstick.com/forum/159624/setting-an-enum-from-user-input), so that only integer [values](https://www.mindstick.com/forum/327/sum-textbox-values) will be accepted. I need a way to [check whether](https://www.mindstick.com/forum/157543/write-a-java-program-to-check-whether-a-string-is-a-palindrome) the input values is integer type or not, here is a part of my code :\

```
//Taking input through scannerScanner sc = new Scanner (System.in);System.out.println("Enter the number ");int input = sc.nextInt();
```

\
\
\
\
\

## Replies

### Reply by Anonymous User

Hi Steve,\
This is a very commonly used issue of validation. I think you don't need to directly [check](https://yourviews.mindstick.com/story/2248/never-forget-to-check-these-specifications-before-buying-a-mobile-phone) whether the input is integer or not. So, don't take input as integer, take this as string using **Scanner.next()** method. Now you change this to integer if it changed successfully that means it a valid integer input. Create a method which would return a boolean based on your input. Follow the below code :\

```
//Taking input through scannerScanner sc = new Scanner (System.in);System.out.println("Enter the number ");//int input = sc.nextInt(); int input = sc.next(); // First take the input as StringvaldiateInput(input); // Now validate it by passing the string value in the boolean method, if it retuns true it is a valid integer, else it's not//Try to convert the input into integer, if its an integer the line will be executed and the method returns true, if it's not it will throw exception which handled in catch block  and the method will return falseboolean validateInput(String input) {     try { 	Integer.parseInt(input);	} catch (Exception e) {	    return false;        }     return true;}
```

\
\
\


---

Original Source: https://www.mindstick.com/forum/33497/how-do-we-check-for-an-integer-user-input-in-java

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
