hello can you help me what method to be use in searching integers in string
for example if the user will input this string having integers.

"jemz 123"

how can i add the existing integers and what method to be use...please help me...hoping for your positive responds...

You want to extract digits from a string? Well, the most obvious would be a for loop, from 0 to length of string. In the body of the loop, you check each character of the string for digitude. You could write a method to do this (ie,
return (c>='0' && c <='9'); where c is the character you're checking)

or you could use the Character.isDigit() method, if you believe in code reuse.

I'm not sure what you want to do with the integers - maybe save them to a string?
(use a StringBuffer, append() the integers as you find them, then put the contents of the StringBuffer into a simple String). It's not obvious whether you want all of the ints in the string in one big collection, or if you'd rather have each cluster as a separate string, but that's easy enough to sort out.

Or, come to think of it, you could be clever and split on non-Digits ("\\D+"), which would give you an array of Strings, each consisting of a string of digits.
For example:

String str = "jemz 123";
		String strArray[] = str.split("\\D+");

In this case, you have an array of one member, "123" - but this will also handle "jemz 123 jon 456" or any string of mixed chars and ints.

if your format would always be the same use tokenizer to separate the strings than use isDigit() to determine if its numbers then convert and save in a integer variable. else if the numbers could appear randomly withing the string you could use a loop and go through each character and do the determination of a number followed by saving.
note that if Ur string could also contain a negative num you will have to cater for that as well. il recomend using one of the above ideas for random strings with random nums something like

as2w3ad

You want to extract digits from a string? Well, the most obvious would be a for loop, from 0 to length of string. In the body of the loop, you check each character of the string for digitude. You could write a method to do this (ie,
return (c>='0' && c <='9'); where c is the character you're checking)

or you could use the Character.isDigit() method, if you believe in code reuse.

I'm not sure what you want to do with the integers - maybe save them to a string?
(use a StringBuffer, append() the integers as you find them, then put the contents of the StringBuffer into a simple String). It's not obvious whether you want all of the ints in the string in one big collection, or if you'd rather have each cluster as a separate string, but that's easy enough to sort out.

hello sir thank you for the reply, so the method can be use is the Character.isDigit()

the purpose sir i am going to sort the numbers in the string because i am going to add the numbers or integers in the string...so by the given example

System.out.println("Input String:");

jemz 123 --->user input

The sum of all numbers in the String:6

or if the user did not input integers in the string

example
jem

There are no digits found in the string...

sir i will try this first sir and i will write again if i have problem...hoping for your positive responds...

You want to extract digits from a string? Well, the most obvious would be a for loop, from 0 to length of string. In the body of the loop, you check each character of the string for digitude. You could write a method to do this (ie,
return (c>='0' && c <='9'); where c is the character you're checking)

or you could use the Character.isDigit() method, if you believe in code reuse.

I'm not sure what you want to do with the integers - maybe save them to a string?
(use a StringBuffer, append() the integers as you find them, then put the contents of the StringBuffer into a simple String). It's not obvious whether you want all of the ints in the string in one big collection, or if you'd rather have each cluster as a separate string, but that's easy enough to sort out.

hello sir,

please correct my the use of method sir...i have problem i will print found if i input like this "jem123" what's the problem with my code sir...please help me hoping for your positive responds...

import java.util.*;
	
	
	class Addingnumbers
	 {
	 	public static Scanner console = new Scanner(System.in);
	 	public static void main(String []args)
	 	{
	 		
	      String names;
	 		boolean found;
	 		
	 		
	 		System.out.println("Enter names");
	 		names= console.next();
	 		
	 		found=false;
	 		for(int i=0;i<names.length();i++)
	 		{
	 			if(names.equals(Character.isDigit(i)))
	 			{
	 				found=true;
	 			}
	 			
	 			
	 		}
	 		
	 		     if(found)

	 		     {
	 		     	 System.out.println("found numbers");
	 		     }	 		
	 		
	 		
	 		
	 		
	 		
	 	  }

Look at the sun API for the Character class. You'll see that Character.isDigit() returns a boolean. names is a String, so
if(names.equals(Character.isDigit(i)))
is comparing a String to a boolean - this is like comparing "apples" and yes.
What you want is to step through each character i in the string and check whether i is an integer. If it is, you add it to a running total. That'll add up all of the integers in the string. (Use charAt(i) to get ecah characer in turn)

class Demo
 { 
  public static void main(String Vna[])
  {
   String str;
   int count=0; 
   boolean n = false;
   for(int i=0;i<Str.length();i++)
   {
    if(str[i]== Character.isdigit(str[i])
    { 
      n= true;
    }
    if( n== true)
    count++;
  }
  System.out.println("No.of Digits in String : " + count);
char str[20]; 
  int count=0;
  for(i=0;i<str.length();i++)
  {  
    if( str[i]==Character.isdigit(str[i]))
    count++;
  }
  System.out.println(NO.Of digits in a sting = " +count);

hello thanks for helping me but how will i add the existing numbers in the string
example

jemz123

this should be the output...The sum of numbers in a string: 6

can you help me...hoping for your positive responds...

Look at the sun API for the Character class. You'll see that Character.isDigit() returns a boolean. names is a String, so
if(names.equals(Character.isDigit(i)))
is comparing a String to a boolean - this is like comparing "apples" and yes.
What you want is to step through each character i in the string and check whether i is an integer. If it is, you add it to a running total. That'll add up all of the integers in the string. (Use charAt(i) to get ecah characer in turn)

hello sir thank you for the reply, okay i will try charAt, i will write again if i have doubt...thank you sir...

class Demo
 { 
  public static void main(String Vna[])
  {
   String str;
   int count=0; 
   boolean n = false;
   for(int i=0;i<Str.length();i++)
   {
    if(str[i]== Character.isdigit(str[i])
    { 
      n= true;
    }
    if( n== true)
    count++;
  }
  System.out.println("No.of Digits in String : " + count);
 }
}
class Demo
 { 
  public static void main(String Vna[])
  {
   String str;
   int count=0; 
   boolean n = false;
   for(int i=0;i<Str.length();i++)
   {
    if(str[i]== Character.isdigit(str[i])
    { 
      n= true;
    }
    if( n== true)
    count++;
  }
  System.out.println("No.of Digits in String : " + count);
 }
}

i get an error with this code...

how can i add the numbers?

if(str[i]== Character.isdigit(str[i])

You're thinking in C. Java Strings are not arrays of char, so you can't get into them that way.

if the character is a digit convert to a integer and add to a temp value initialized to 0 so if there is no numbers in the string it will give an output of zero and in the end just print out the temp value. :)

if the character is a digit convert to a integer and add to a temp value initialized to 0 so if there is no numbers in the string it will give an output of zero and in the end just print out the temp value. :)

hello sir i am confuse of what you said, can you give a little code for me so that i can understand and to have idea...hoping for your positive responds...

i get an error with this code...

What is the error?

What is the error?

here is the error sir,

array required, but java.lang.String found
if(names==Character.isDigit(names))

sir i want to check if the user input string have numbers on it and get the sum of all number...

example:

jemz123

the sum is: 6

but before that sir i want to display those numbers....here's my code sir please correct me if i am wrong...

import java.util.*;
	
	
	class Addingnumbers
	 {
	 	public static Scanner console = new Scanner(System.in);
	 	public static void main(String []args)
	 	{
	 		
	        String names;
	 		boolean found;
	 		
	 		
	 		System.out.println("Enter names");
	 		names= console.next();
	 		
	 		found=false;
	 		for(int i=0;i<names.length();i++)
	 		{
	 			if(Character.isDigit(names.charAt(i)))
	 			{
	 			  System.out.println(i);
	 			}
	 			
	 			
	 		}
	 		
	 		     
	 		
	 		
	 	  }		 		
	 		
}

the output is this
Enter names
jemz123
4
5
6
please help me on this it will not display the numbers 123..hoping for your positive repsonds...

Walk through your for loop, lines 18 to 26. What is is that you're printing if charAt(i) is a digit? Don't tell me what you want to be printing, tell me what you're printing.

Walk through your for loop, lines 18 to 26. What is is that you're printing if charAt(i) is a digit? Don't tell me what you want to be printing, tell me what you're printing.

hello sir,

i think im printing the index?..sir what am i suppossed to do for my forloop?

That's right, that's what you're doing. I'd hate to give you the answer, you've already got it right in front of you. Look at your code, the answer is there.

That's right, that's what you're doing. I'd hate to give you the answer, you've already got it right in front of you. Look at your code, the answer is there.

sir please don't hate me, please give me idea...hoping for your positive responds...

No hate here, sir. It's just that you've already given the correct answer. You don't want the index, you want the character.

No hate here, sir. It's just that you've already given the correct answer. You don't want the index, you want the character.

sir I think i got it please correct me if i am wrong with the this....

class Addingnumbers
	 {
	 	public static Scanner console = new Scanner(System.in);
	 	public static void main(String []args)
	 	{
	 		
	        String names;
	 		boolean found;
	 		
	 		
	 		System.out.println("Enter names");
	 		names= console.next();
	 		
	 		found=false;
	 		for(int i=0;i<names.length();i++)
	 		{
	 			if(Character.isDigit(names.charAt(i)))
	 			{
	 			  System.out.println(names.charAt(i));
	 			}
	 			
	 			
	 		}
 
       }
   }

No hate here, sir. It's just that you've already given the correct answer. You don't want the index, you want the character.

sir i already get the numbers in the string, but i don't know how to get the sum of it..

Enter names
jem123
1
2
3
The sum of all numbers: 150

why is the sum is 150?it must be 6...please help me hoping for your positive responds...

here's the code...

String names;
	 		boolean found;
	 		int sum=0;
	 		
	 		
	 		System.out.println("Enter names");
	 		names= console.next();
	 		
	 		found=false;
	 		for(int i=0;i<names.length();i++)
	 		{
	 			if(Character.isDigit(names.charAt(i)))
	 			{
	 			  System.out.println(names.charAt(i));
	 			}
	 			
	 			
	 		}
	 		
	 		
	 		for(int i=0;i<names.length();i++)
	 		{
	 			if(Character.isDigit(names.charAt(i)))
	 			{
	 			  sum = sum + names.charAt(i);
	 			}
	 			
	 			
	 		}
	 		
	 		   System.out.println("The sum of all numbers: " + sum); 
	 	 		
	 	  }
	}

sum = sum + names.charAt(i);

Your code sums the characters not their decimal values.
What is the numberic value of the character: '1'? A clue: it is not integer 1

Find a table of ASCII characters and see what the value of characters is?
What is the value of '2'?

declare an integer variable lets say temp and initialize to 0.use the same loop you use to print the numbers in the screen. this time add another line after the line which prints the numeric characters which should convert that same character you just printed to an integer and add to temp.finally after the loop just force print the temp value to the screen. easy enuf.

in your case temp is sum. and you need to convert else when your adding it adds asci value of the character since the compiler understands your adding characters.

if you look and understand what your code currently is doing properly. you need just 2-3 small lines.

Your code sums the characters not their decimal values.
What is the numberic value of the character: '1'? A clue: it is not integer 1

Find a table of ASCII characters and see what the value of characters is?
What is the value of '2'?

sir i see the value of 2 is STX?..in this link http://www.robelle.com/smugbook/ascii.html

so what should i do now sir?...hoping for your positive responds...

Yes the table at your link shows the values for bytes and how they are represented.

Look down several rows to see the value for the character '2'. It shows that the decimal value for '2' is 50.
Look back at the characters you were adding together: '123' they have the values: 49, 50 & 51. Their sum is 150.

Char	Octal	Dec	Hex	Description
2	62	50	32	Two

declare an integer variable lets say temp and initialize to 0.use the same loop you use to print the numbers in the screen. this time add another line after the line which prints the numeric characters which should convert that same character you just printed to an integer and add to temp.finally after the loop just force print the temp value to the screen. easy enuf.

in your case temp is sum. and you need to convert else when your adding it adds asci value of the character since the compiler understands your adding characters.

if you look and understand what your code currently is doing properly. you need just 2-3 small lines.

sir how do i convert that same character that i printed into integer?..hoping for your positive responds...

Look at the Integer class or the String class. They both have methods to convert characters to decimal values.

Or subtract '0' from the char itself to get its decimal value.

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.