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

hello sir, oh!, okay sir that's why the sum is 150!...thank you for this idea...

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.

okay sir i will find first the method to convert the charaters to decimal values...

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.

sir, i have problem, i can't find out what is the method to be use...

If you can't find a method that takes a char, try subtracting '0' from the numeric char to get its decimal value.
For example: '0' - '0' = 0 & '8' - '0' = 8

If you can't find a method that takes a char, try subtracting '0' from the numeric char to get its decimal value.
For example: '0' - '0' = 0 & '8' - '0' = 8

okay sir, i will try this sir i think i am confuse now...sir is toString method to be use isn't it?

What is it you want to do? Why does the toString() method seem useful?

What is it you want to do? Why does the toString() method seem useful?

because i found this sir Converts the numeric value of this instance to its equivalent string representation. (Overrides ValueType.ToString.)in this link http://msdn.microsoft.com/en-us/library/system.decimal.tostring.aspx


sir i initalized b=0;
but why is it -6 is the sum sir?

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));
	 			  found=true;
	 			}
	 			
	 			
	 		}
	 		
	 		
	 		 for(int i=0;i<names.length();i++)
	 		 {
	 		 
	 			if(Character.isDigit(names.charAt(i)))
	 			{
	 			   b= '0' - names.charAt(i);
	 			   
	 			   sum =  sum +b;
	 			  
	 			}
	 			
	 		 }	
	 		
	 		 System.out.println("The sum of all integers: " + sum); 
	 		  
	 		
	 	  }
	 		
	 			 		
}

subtracting '0' from the numeric char

What does your code do?
'0' - names.charAt(i);

subtracting '0' from the numeric char

What does your code do?
'0' - names.charAt(i);

i just followed what you said sir....please correct me if i am wrong sir...

subtracting '0' from the numeric char

What does your code do?
'0' - names.charAt(i);

hello sir i already get the right answer...this is how i do sir...

b = names.charAt(i) -  '0';

i get the sum: 6

thank you sir...but i have problem sir if i input space inbetween jemz and 123 the sum is zero?why this happening sir?...hoping for your positive responds...

jemz - Two questions for you. Do you understand the difference between a char and a String? Do you understand why subtracting zero from names.charAt(i) gets you a value you can use?
Serious questions, and it's okay if you don't know, but if you don't know someone will either explain or point you to a link, because you really should understand them as you move forward.

Now, as to your question about why you get a zero when you insert a space, I'm betting it's because console.next() uses whitespace as a delimiter.
That's a guess on my part, since console.next() is a new input method to me, but that's the most reasonable hypothesis I can find.

jemz - Two questions for you. Do you understand the difference between a char and a String? Do you understand why subtracting zero from names.charAt(i) gets you a value you can use?
Serious questions, and it's okay if you don't know, but if you don't know someone will either explain or point you to a link, because you really should understand them as you move forward.

Now, as to your question about why you get a zero when you insert a space, I'm betting it's because console.next() uses whitespace as a delimiter.
That's a guess on my part, since console.next() is a new input method to me, but that's the most reasonable hypothesis I can find.

hello sir, correct me if i am wrong..string is composed of characters or groups of character...

sir about the whitespace that i can get zero.. can you help me how can i get rid of the white space if i input jemz whitespace 123 there should be an output withe 6...hoping for your positive responds...

do some research about the line where it says console.next(). ans some question like :

1) what does it do.
2) is it a function
3) can it take any parameters. if yes how many and what can it take
4) what is a delimiter
5) can you use something else instead
6) is google a good friend? ***** (5 star ques)

why this happening

There must be a problem with your code.
What happens in your program when it finds a space?

Remember I asked you to do some experiments with using the Scanner class's methods. Go back and try some more.
You need to understand how it works.

Hi..
What you really want??
actually its a code to count digits in a string..
do u wanna sum of digits in a string??
i think it will works..

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

i think it will works

It would be better if it you had tested it and corrected any problems in it so that it did work.
Otherwise your showing a bad example of code to the OP.

Hi..
What you really want??
actually its a code to count digits in a string..
do u wanna sum of digits in a string??
i think it will works..

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

hello,

thanks for the reply, i think it will generate an error,array expected!..

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

There must be a problem with your code.
What happens in your program when it finds a space?

Remember I asked you to do some experiments with using the Scanner class's methods. Go back and try some more.
You need to understand how it works.

sir thank you for the reply,

okay sir i will try it....but sir ahm can you explain why did i get the correct sum if i subtract it to '0';..because i am confuse...hoping for your positive responds...

Several posts back I explained how '1' + '2' + '3' was equal to 150.
There are approximate 128 different characters I can type on my keyboard. The computer assigns each one of them a value so it can keep track of them. One of the characters I can type is '1'. It is assigned a decimal value of 49. If you want to convert the character '1' to what is the "normal" decimal value of one, you subtract '0' from it. Looking at the decimal values '1' = 49 and '0' = 48.
So '1' - '0' = 1

jemz - yes, that line from veena is an error. He's treating Java strings as if they were C strings - in C, a string is just an array of char, but in Java it's a class. In the line you quoted, where he says "str" you need str.charAt(i) instead.

However, that's not the only error there. isDigit() returns a boolean value - true or false. The comparison to a char is useless, what he's saying is "if the character at this position in the string is True, do something". If you're not sure what type is returned by a function, you can look it up at the sun API: http://download.oracle.com/docs/cd/E17409_01/javase/6/docs/api/
or you can often get there by searching for "java charAt" or whatever method you're curious about. If you look at the first result for "java charAt", you'll see you're at the page for String. Scroll down into the methods, and you'll see the first entry tells you about charAt. The first column there is the return type - that's what you get back from this method. Search out "isDigit" and you'll see that it returns a boolean, so trying to compare those two return values is pointless.

Several posts back I explained how '1' + '2' + '3' was equal to 150.
There are approximate 128 different characters I can type on my keyboard. The computer assigns each one of them a value so it can keep track of them. One of the characters I can type is '1'. It is assigned a decimal value of 49. If you want to convert the character '1' to what is the "normal" decimal value of one, you subtract '0' from it. Looking at the decimal values '1' = 49 and '0' = 48.
So '1' - '0' = 1

thank your sir for enlighten my mind and helping me....ahm sir i tried console.nextLine() inorder to get rid of whitespace but sir i did not solve it sir the sum always be zero if i have whitespace...please help me sir where i am going to put the console.nextLint() method.hoping for your positive responds...

Several posts back I explained how '1' + '2' + '3' was equal to 150.
There are approximate 128 different characters I can type on my keyboard. The computer assigns each one of them a value so it can keep track of them. One of the characters I can type is '1'. It is assigned a decimal value of 49. If you want to convert the character '1' to what is the "normal" decimal value of one, you subtract '0' from it. Looking at the decimal values '1' = 49 and '0' = 48.
So '1' - '0' = 1

sir i already solve it sir please correct me if i am wrong...instead of console.next() i change it console.nextLine() did i make it right sir?...hoping for your positive responds...

System.out.println("Enter names");
	 	    names= console.nextLine();
	 	    		  
	 		
	 		
	 		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)))
	 			{
	 			  
	 			   b =  names.charAt(i) - '0';
	 			    
	 			   sum =  sum + b;
	 			   
	 			}
	 			
	 		 }	
	 		
	 		 System.out.println("The sum of all integers: " + sum);

jemz - yes, that line from veena is an error. He's treating Java strings as if they were C strings - in C, a string is just an array of char, but in Java it's a class. In the line you quoted, where he says "str" you need str.charAt(i) instead.

However, that's not the only error there. isDigit() returns a boolean value - true or false. The comparison to a char is useless, what he's saying is "if the character at this position in the string is True, do something". If you're not sure what type is returned by a function, you can look it up at the sun API: http://download.oracle.com/docs/cd/E17409_01/javase/6/docs/api/
or you can often get there by searching for "java charAt" or whatever method you're curious about. If you look at the first result for "java charAt", you'll see you're at the page for String. Scroll down into the methods, and you'll see the first entry tells you about charAt. The first column there is the return type - that's what you get back from this method. Search out "isDigit" and you'll see that it returns a boolean, so trying to compare those two return values is pointless.

sir thank you for the reply,


sir here is my code 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)
	 	{
	 		
	 		
	 		int sum=0;
	 		int b;
	 		String names;
	 		
	 	    System.out.println("Enter names");
	 	    names= console.nextLine();
	 	    console.close();
	 	    		  
	 			
	 		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)))
	 			{
	 			  
	 			   b =  names.charAt(i) - '0';
	 			    
	 			   sum =  sum + b;
	 			   
	 			}
	 			
	 		 }	
	 		
	 		 System.out.println("The sum of all integers: " + sum);   	
	 	  }			
	 		
	 }

Several posts back I explained how '1' + '2' + '3' was equal to 150.
There are approximate 128 different characters I can type on my keyboard. The computer assigns each one of them a value so it can keep track of them. One of the characters I can type is '1'. It is assigned a decimal value of 49. If you want to convert the character '1' to what is the "normal" decimal value of one, you subtract '0' from it. Looking at the decimal values '1' = 49 and '0' = 48.
So '1' - '0' = 1

sir i think i made it sir....please correct me if i am wrong...thank you also sir for explaining me...more power to you....your a good teacher...

import java.util.*;
	
	
	class Addingnumbers
	 {
	 	public static Scanner console = new Scanner(System.in);
	 	public static void main(String []args)
	 	{
	 		
	 		int sum=0;
	 		int b;
	 		String names;
	 		
	 		System.out.println("Enter names");
	 	        names= console.nextLine();
	 	        console.close();
	 	    		  
	 		
	 		
	 		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)))
	 			{
	 			   b =  names.charAt(i) - '0';   
	 			   sum =  sum + b;
	 			   
	 			}
	 		 }	
	 		
	 		 System.out.println("The sum of all integers: " + sum); 
	 	  }		
	 		 		 		
	}

Several posts back I explained how '1' + '2' + '3' was equal to 150.
There are approximate 128 different characters I can type on my keyboard. The computer assigns each one of them a value so it can keep track of them. One of the characters I can type is '1'. It is assigned a decimal value of 49. If you want to convert the character '1' to what is the "normal" decimal value of one, you subtract '0' from it. Looking at the decimal values '1' = 49 and '0' = 48.
So '1' - '0' = 1

hello sir, is it okay to ask what compiler did you use?so that i may be comfortable more to program and to practice...hoping for your positive responds...

if(Character.isDigit(names.charAt(i)))

Bingo. Since isDigit() returns a boolean, this will execute the code if you're looking at a digit. If you enter "jemz123" you should see
1
2
3

on successive lines. (We'll talk about formatting output another day)

You can tighten up the code a little if you want. You have two loops which go through the same String twice, at 20-26 and 28-36. If you combine the two, it'll be better style. (Easier to read, easier to understand, harder to break) Might even run faster, but we won't go into optimizing code just now.

if(Character.isDigit(names.charAt(i)))

Bingo. Since isDigit() returns a boolean, this will execute the code if you're looking at a digit. If you enter "jemz123" you should see
1
2
3

on successive lines. (We'll talk about formatting output another day)

You can tighten up the code a little if you want. You have two loops which go through the same String twice, at 20-26 and 28-36. If you combine the two, it'll be better style. (Easier to read, easier to understand, harder to break) Might even run faster, but we won't go into optimizing code just now.

is this what you mean sir?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)
	 	{
	 		int sum=0;
	 		int b;
	 		String names;
	 		
	 		System.out.println("Enter names");
	 	    names= console.nextLine();
	 	    console.close();
	 	    		   
                       for(int i=0;i<names.length();i++)
	 		{
	 			if(Character.isDigit(names.charAt(i)))
	 			{
	 		           System.out.println(names.charAt(i));
	 			   b =  names.charAt(i) - '0';  
	 			   sum =  sum + b;
	 			   
	 			}
	 			
	 				
	 		}
	 		
	 		 System.out.println("The sum of all integers: " + sum); 
                }

   }

There must be a problem with your code.
What happens in your program when it finds a space?

Remember I asked you to do some experiments with using the Scanner class's methods. Go back and try some more.
You need to understand how it works.

sir can you teach me another way to convert character into their decimal value

with out subtracting zero by it self...i want to know another way ...please help me hoping for your positive responds...

try Character.getNumericValue() - it's a static method of che Character class, so you have to call it that way. (like you do with isDigit())

Integer.parseInt() is what I use when I'm dealing with strings, but in this case you'd have to convert the char to String - not a huge deal, but you might as well work with things as you find them.

try Character.getNumericValue() - it's a static method of che Character class, so you have to call it that way. (like you do with isDigit())

Integer.parseInt() is what I use when I'm dealing with strings, but in this case you'd have to convert the char to String - not a huge deal, but you might as well work with things as you find them.

sir i need help , i have error on this..hoping for your positive responds...

System.out.println("Enter names");
	 	    names = console.nextLine();
	 	   
	 		
	 		
	 	for(int i=0;i<names.length();i++)
	 	{
	 		if(Character.getNumericValue(Character.toString(names.charAt(i))))
	 			{
	 		           System.out.println(names.charAt(i));
	 			   b =  names.charAt(i) - '0';  
	 			   sum =  sum + b;
	 			   
	 			}	       
	 		  	
	 				
	 		}
	 		
	 		 System.out.println("The sum of all integers: " + sum); 
	 			

	 		
	 	  }
	 		
	 			
	 }
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.