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
11 188025
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 - String input = ....
-
try {
-
int x = Integer.parseInt(input);
-
System.out.println(x);
-
}
-
catch(NumberFormatException nFE) {
-
System.out.println("Not an Integer");
-
}
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
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. - for each character in the string
-
{
-
if(character is a digit)
-
{
-
while(next character is a digit);
-
print the substring of all digits
-
}
-
}
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
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 - public class Numbers {
-
public static void main(String[] args) {
-
String str = "java5.02ds77dfsff";
-
char[] all = str.toCharArray();
-
String numbers = "";
-
for(int i = 0; i < all.length;i++) {
-
if(Character.isDigit(all[i])) {
-
numbers = numbers + all[i];
-
}
-
}
-
System.out.println(numbers);
-
-
}
-
}
Thanks a lot I find out my mistakes i will change it
Thank you for your guidens.
public static boolean isNumeric(String aStringValue) {
Pattern pattern = Pattern.compile( "\\d+" );
Matcher matcher = pattern.matcher(aStringValue);
return matcher.matches();
}
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);
}
}
}
hi,
you can do like this way also - String st=input
-
-
Integer i=new Integer(st)
-
-
if(i instance of Integer){
-
int j=i.intValue();
-
System.out.println(j);}
That won't even compile. Please only suggest things when you are sure you know what you are talking about.
The best solution is to use a Pattern like kadsoft suggested before.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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, ...
|
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...
|
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...
|
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...
|
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
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
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...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
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...
|
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,...
|
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...
|
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...
| |