-1

I am trying to write a program, but my client might not type in the information in the correct format. How do I tell if a String is an int in Java (so I can throw an error message that I write and allows them to retry)? I.e., is there a method in java that will allow me to check if String str is of type int?

String input=kb.nextLine();   
if(input.(isInteger method)&&(int)input==otherint)
  do stuff inside here;
else
  do different stuff;

An answer should include the imports needed (if any) and basic method. Also, as simple as possible would be very good. If this is actually a copy of a question, post the link (I looked for a while but couldn't find anything on google or here that seemed like it would solve my problem).

YVHDroid
  • 1
  • 2
  • 1
    You have a misunderstanding about types and casting. A `String` is never an `int` and you can never cast a `String` to an `int`. What you want to do is check if the `String` contains only digits, and then parse it into an `int`. – Jesper Mar 17 '16 at 19:11
  • 1
    Seriously, did one of the answerers read the link of Tom? Just use the freaking `kb.nextInt()` ! – ctst Mar 17 '16 at 19:29
  • I tried `kb.nextInt()`, but it threw an input mismatch error when I tried to input "asdfghjkl" as a test. I was asking if there was a way to check if it is an int to stop from getting error messages- I would just `System.out.println("Error 1d.10.t: That is not an Integer; please enter a valid number.")` – YVHDroid Mar 18 '16 at 17:44

2 Answers2

1

Create a methode in a class say IntegerCheck.java:

public Boolean isInteger (String s) 
{
    try
    {
        Integer.parseInt(s);
        return true;

    } catch (Exception e) 
      {
         return false;
      }  
}

Then use it in your program:

First import the class:

import <package name>.IntegerCheck;

Inside the required function:

IntegerCheck obj=new IntegerCheck();

String input=kb.nextLine();  //Get user input using Scanner or any other method, make sure the return type is string.

if(obj.isInteger(input))
{
  //Success.
}
else
{
  //throw an error message that I write and allows them to retry
}
padippist
  • 932
  • 13
  • 29
0
package main.java;
public class Test {
    public static boolean isNumeric(String s){
        // 11 Because might be negative, but 32bit integer overflow is 
        // 10 characters long.
        if (s.length() > 11) return false;
        int i = 0;
        if (s.startsWith("-")) i++;
        for(; i < s.length(); i++){
            if (!Character.isDigit(s.charAt(i))) return false;
        }
        return true;
    }
    public static void main(String[] args){
        System.out.println(Test.isNumeric("100"));
        System.out.println(Test.isNumeric("aa"));
        System.out.println(Test.isNumeric("12a"));
        System.out.println(Test.isNumeric("1002"));
        System.out.println(Test.isNumeric("-11232"));
        System.out.println(Test.isNumeric("999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999"));
        System.out.println(Test.isNumeric("4294967294"));
    }
}

Edit I forgot to include negative numbers. Edit Rough way to handle integer overflow.

Mind you this will only work with integers. Regular expressions seem a bit overkill for this, but aren't a bad solution. You should not use exceptions to control program flow as per this question: Why not use exceptions as regular flow of control?

Community
  • 1
  • 1
Stephen Carman
  • 949
  • 7
  • 24
  • 2
    You also ignore cases like `999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999` which isn't an `int` – ctst Mar 17 '16 at 19:35
  • @ctst I added a rough way to deal with it. Good point. – Stephen Carman Mar 17 '16 at 19:43