364,083 Members | 5989 Browsing Online
Community for Developers & IT Professionals
Bytes IT Community

check if the input is integer

sang
P: 83
Hi

I am writing a application program that checks if the input is integer, if it is integer print the integer value only. i have no idea about that please any one help me.

Thanks
Sang
Sep 28 '06 #1
Share this Question
Share on Google+
8 Replies


r035198x
10K+
P: 10,083
Hi

I am writing a application program that checks if the input is integer, if it is integer print the integer value only. i have no idea about that please any one help me.

Thanks
Sang
Use Exception handling
as in
Expand|Select|Wrap|Line Numbers
  1. String input = ....
  2. try {
  3.     int x = Integer.parseInt(input);
  4.     System.out.println(x);
  5. }
  6. catch(NumberFormatException nFE) {
  7.     System.out.println("Not an Integer");
  8. }
Sep 28 '06 #2

sang
P: 83
Thanks for your reply.

This coding is print if the input is integer otherwise not printed. I am trying to get only integer values i am not able to do this by using the isNumber() but i am got the error.

so,please give me the code for

if the input is like this "There is 3 apples in 1 tree" the output is only 1 & 3

that is only print the integer.

Thanks
Sang
Sep 28 '06 #3

D_C
100+
P: 293
D_C
If you are going to search for a number within a string, then print the number, you don't even need to convert it to an integer. If you are using negatives, be sure to modify the if statement to accept minus sign as well as digit.
Expand|Select|Wrap|Line Numbers
  1. for each character in the string
  2. {
  3.   if(character is a digit)
  4.   {
  5.     while(next character is a digit); 
  6.     print the substring of all digits
  7.   }
  8. }
Sep 28 '06 #4

sang
P: 83
Thanks for your advice but i am not able to do that because i am new to java for that reason i am strugle in this.

The following code is copmlied but it is not correct please correct it and then sent to me

import java.io.*;
class str {
public boolean isNumeric(String input){
try {
char[] ch=charArray(input);
for( int i=0 ; i<input.length() ; i++)
{
if(Character.isNumber)
{
System.out.println(i);
}
else { return false;}
}
}catch(NumberFormatException nfe) {
System.out.println(nfe);
}
}
public static void main(String arg[]) {
int c = str.isNumeric("java 123");
System.out.println(c);
}
}

Thanks
Sang
Sep 29 '06 #5

r035198x
10K+
P: 10,083
Thanks for your advice but i am not able to do that because i am new to java for that reason i am strugle in this.

The following code is copmlied but it is not correct please correct it and then sent to me

import java.io.*;
class str {
public boolean isNumeric(String input){
try {
char[] ch=charArray(input);
for( int i=0 ; i<input.length() ; i++)
{
if(Character.isNumber)
{
System.out.println(i);
}
else { return false;}
}
}catch(NumberFormatException nfe) {
System.out.println(nfe);
}
}
public static void main(String arg[]) {
int c = str.isNumeric("java 123");
System.out.println(c);
}
}

Thanks
Sang
An initial attempt would be

Expand|Select|Wrap|Line Numbers
  1. public class Numbers {
  2.     public static void main(String[] args) {
  3.         String str = "java5.02ds77dfsff";
  4.         char[] all = str.toCharArray();
  5.         String numbers = "";
  6.         for(int i = 0; i < all.length;i++) {
  7.             if(Character.isDigit(all[i])) {
  8.                 numbers = numbers + all[i];
  9.             }
  10.         }
  11.         System.out.println(numbers);
  12.  
  13.     }
  14. }
Sep 29 '06 #6

sang
P: 83
Thanks a lot I find out my mistakes i will change it

Thank you for your guidens.
Sep 29 '06 #7

kadsoft
P: n/a
kadsoft
public static boolean isNumeric(String aStringValue) {
Pattern pattern = Pattern.compile( "\\d+" );

Matcher matcher = pattern.matcher(aStringValue);
return matcher.matches();
}
Sep 28 '10 #8

abhishek kush
P: 1
import java.io.*;
class abhi
{
public static void main(String args[])throws IOException
{
DataInputStream d=new DataInputStream(System.in);
int i,as,l;
String na;
na=d.readLine();
l=na.length();
char ch;
for(i=0;i<l;i++)
{
ch=na.charAt(i);
as=(int)ch;
if(as>=48&&as<=57)
System.out.println(ch);
}
}
}
Nov 5 '11 #9

Post your reply

Help answer this question



Didn't find the answer to your Java question?

You can also browse similar questions: Java isint java isinteger java java integer java isinteger java isnumber