Hello! Im a java noob and I need to create a program that determines whether the input is an integer or not , can anyone help me out? thanks in advance! btw I have to use Scanner from java.util. , I have no idea on how to check if its an integer or not. Before, I was using C and in C there's a function called isnum() but since Im new at java I have no idea what to do.
any help is greatly appreciated :)

Let's assume that you use Scanner to read input: Scanner.readLine() I think is the method and you store it into a String variable. There is a method that turns the String into an int:

String s = "10";
int i = Integer.parseInt(s);

The int i variable has the int value 10. If s is not an int number then the Integer.parseInt(s); will throw an Exception:

try {
 String s = "a";

 int i = Integer.parseInt(s); //this line will throw a NumberFormatException. It will also throw it if you use Integer.parseInt to turn into an int the String: "2.35" which is a float number

 System.out.println("The s is the int number: " + s); 

} catch (NumberFormatException nfe) {
 System.out.println("The s is not a number, s: " + s);
}

For more information:
Integer
Double
Float

Ill give it a try THANKS!!!

can anyone give me another solution?

You can for-loop the String using the charAt method and checking each time if the char taken is one of those: '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '.' . Also you should check if the '.' appears more than once

Sorry for the second post but here is the API for the charAt: String

thanks agen

can anyone give me another solution?

Why would they? You haven't explained what you have tried, what problems you encountered, your thoughts on potential solutions, or anything demonstrating any effort on your part. Programming involves thinking for yourself and "give me another solution" certainly doesn't imply that you have done any of that.

OK First of all you have to find out what scanner class does. I suggest you check java specifications. best place you check is java specifications.
from here you can get all the information about any class of interface from java API. in your case you have to find java.utils.Scanner class actually I found it for you. but if you wanna do more java programming I suggest you learn how to find the information you need from the specifications. anyway here is Scanner class: Scanner . there is also examples and guidelines about this class. I think Scanner.hasNextInt() function could do it for you. give it a try.

thank you and sorry.. my bad ^_^

To everyone that replyed, THANKS!! while I was looking at the Scanner class link that yilmazhuseyin posted, I saw a the method hasNextInt() thats what I needed. THANK YOU everyone =)

hello...i hav a doubt abt above replys...suppose i hv entered an expression( e.g. 12 + 3*5 -7) into an string variable .now if i want to check the string from starting index that it contains an integer or not ,then what is the idea...plezz help me out . i m makin a calculator which inputs a expression n gives the result.
thank you in advance...!!

Hi try this

import java.util.regex.Pattern;
import java.util.regex.Matcher;

class TestInt 
{
	public static void main(String[] args) {
		//test isInt
		isInt("asd2f");
		isInt("1233");

	}//end of main

	private static boolean isInt(String data){
		 Pattern p = Pattern.compile("[0-9]*");
		 Matcher m = p.matcher(data);
		 boolean result = m.matches();
		 if (result) {
			System.out.println("Hi Kundan input is an Integer");
		 }//end of if
		 else {
			System.out.println("Sorry");
		 }//end of else
		 return result;
	}//end of method isInt
}//end of class

== All the best

Can you do this w/o using try-catch...? Only if-else?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.