473,473 Members | 1,577 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

check if the input is integer

83 New Member
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
11 188029
r035198x
13,262 MVP
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
83 New Member
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
293 Contributor
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
83 New Member
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
13,262 MVP
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
83 New Member
Thanks a lot I find out my mistakes i will change it

Thank you for your guidens.
Sep 29 '06 #7
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
1 New Member
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
venkat4u
1 New Member
hi,
you can do like this way also

Expand|Select|Wrap|Line Numbers
  1. String st=input
  2.  
  3. Integer i=new Integer(st)
  4.  
  5. if(i instance of Integer){
  6. int j=i.intValue();
  7. System.out.println(j);}
Mar 13 '13 #10
r035198x
13,262 MVP
That won't even compile. Please only suggest things when you are sure you know what you are talking about.
Mar 14 '13 #11
Karidev
1 New Member
The best solution is to use a Pattern like kadsoft suggested before.
Oct 12 '16 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: Lord Merlin | last post by:
Sorry for the dumb question, but what is the function in ASP to see if an object / string is an integer? I want to check the contents of a form, and if it's an integer, i.e a number, do...
2
by: Dknight | last post by:
Hi, all! I have a problem, how can I check a form data before sending it to server. I have the example below, but first data is sent to server, but after JavaScript checks the data :( I need...
2
by: CSharp | last post by:
Is it possible to find if lower 1st bit and 2nd bit in an 32-bit integer is set using regular expression in C# Example 1. Input Strings - 0, 1, 2 Result - Fai 2. Input Strings - 3, 7, ...
1
by: redpayne | last post by:
Okay, I finally got this program to run according to what the book had us build it as. Now prof wants case 2 and case 3 to prompt again for input, check input to see if it is the correct type, then...
1
by: caveman | last post by:
Hiya, Is there anyway you can check the input of a text box? For example if you had a textbox which submits articles to the site and you want to make sure you dont get any rude language... Is...
0
by: lini | last post by:
Hello, I am writing some code in the scenario which can be described as follow: + program A which writes to standard output (e.g. cout >> whatever). + program B which has GUI and also listens to...
1
idsanjeev
by: idsanjeev | last post by:
how can check the input data is character or numeric if input data accept the numeric and input data is character then error message
8
idsanjeev
by: idsanjeev | last post by:
how can check the input data is character or numeric if input data accept the numeric and input data is character then error message
15
by: ab12 | last post by:
I am writing in C. I want to reverse the input integer using recursion so if the input is 456, the output should be 654. it only needs to work with positive numbers. I've already written this...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.