i have error on this

What happens?

What happens?

hello sir,

can you teach me the other way sir by not using this names.charAT(i)-'0';
hoping for your positive responds...sir by the way can i ask what is the best compiler to use in java....

What happens?

hello sir, i have problem on this..please help me...

import java.util.*;
	
	
	class AddingnumbersAnotherway
	 {
	 	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");
	 	        int inames = Integer.parseInt(names);
	 	    
	 	   
	 		for(int i=0;i<inames;i++)
	 		{
	 			if(Character.isDigit((inames)))
	 			{   
	 			   sum =  sum + inames;  
	 			}
	 				
	 		}
	 		
	 		 System.out.println("The sum of all integers: " + sum); 
	 				
	 	  }
	 				
	 }

the other way

I don't know of any other way to directly convert a character to its int value.
You can convert the char to a String and then use the Integer.parseInt() method.

The only compiler I know of for the current version of java comes with the JDK.

i have problem

What is the problem?

It would save time if you would post the problem with the code.

I don't know of any other way to directly convert a character to its int value.
You can convert the char to a String and then use the Integer.parseInt() method.

The only compiler I know of for the current version of java comes with the JDK.

okay sir, what part of my code im goin to change sir....the compiler sir are you using Gel?or JGrasp?what did you use sir?...

What is the problem?

It would save time if you would post the problem with the code.

here's the problem sir,


Exception in thread "main" java.lang.NumberFormatException: For input string: "names"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
at java.lang.Integer.parseInt(Integer.java:447)
at java.lang.Integer.parseInt(Integer.java:497)
at AddingnumbersAnotherway.main(AddingnumbersAnotherway.java:19)

Process completed.

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.

hello sir,

sir i need your help i got problem with the getNumericValue please correct my code...

System.out.println("Enter names");
	 	    names = console.next();
	 	    
	 	   
	 		
	 		
	 		for(int i=0;i<inames;i++)
	 		{
	 			if(Character.getNumericValue((names.charAt(i))))
	 			{	
	 		     
	 			   sum =  sum + names.charAt(i);
	 			   
	 			}
	 			
	 				
	 		}
	 		
	 		 System.out.println("The sum of all integers: " + sum); 
	 			

	 		
	 	  }
	 		
	 			
	 }

What is the problem?

It would save time if you would post the problem with the code.

hello sir i have problem in converting character to string please help me sir...
here's my code sir....

System.out.println("Enter names");
	inames = Integer.parseInt(names);
	 	    
	 	   


for(int i=0;i<inames.length();i++)
{
	 			   if((Character.getNumericValue(Character.isDigit(((Character.toString(i)))))));   
{	
	 		      
    sum =  sum + inames;
	 			   
}
	 			
	 				
}
	 		
 System.out.println("The sum of all integers: " + sum); 

 }

}

converting character to string

Look at the Character class, String class or the StringBuffer class

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

if requires a boolean value, not a numeric value. getNumericValue returns a numeric value, so this will fail. This is like saying

if (4) { do something }

That works in C, where non-zero values are considered true, but this is not the case in java.

if((Character.getNumericValue(Character.isDigit(((Character.toString(i)))))));

Good gravy, that's a lot of parens. Here, you're making the reverse mistake, feeding a boolean value into a method that expects a char. Again, you need to understand types if you're going to figure this out.
Primitive types in java are int, char, double, boolean, plus long and float - I think that's about it. You need to understand what each of these is and what it represents. This is all well documented on line, just google "java int", "java char", etc., and read until you understand it.

Once you understand that, you have to be able to read the sun API and understand what types a method expects, and what it returns.

This is all basic stuff, and until you grasp it, programming in java - even at this level! - will be beyond you. I can't help you until you understand this stuff.

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

if requires a boolean value, not a numeric value. getNumericValue returns a numeric value, so this will fail. This is like saying

if (4) { do something }

That works in C, where non-zero values are considered true, but this is not the case in java.

if((Character.getNumericValue(Character.isDigit(((Character.toString(i)))))));

Good gravy, that's a lot of parens. Here, you're making the reverse mistake, feeding a boolean value into a method that expects a char. Again, you need to understand types if you're going to figure this out.
Primitive types in java are int, char, double, boolean, plus long and float - I think that's about it. You need to understand what each of these is and what it represents. This is all well documented on line, just google "java int", "java char", etc., and read until you understand it.

Once you understand that, you have to be able to read the sun API and understand what types a method expects, and what it returns.

This is all basic stuff, and until you grasp it, programming in java - even at this level! - will be beyond you. I can't help you until you understand this stuff.

sir please correct me if i am wrong

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

                 }


   }
if(Character.getNumericValue((names.charAt(i))))

if requires a boolean value, not a numeric value. getNumericValue returns a numeric value, so this will fail. This is like saying

if (4) { do something }

That works in C, where non-zero values are considered true, but this is not the case in java.

if((Character.getNumericValue(Character.isDigit(((Character.toString(i)))))));

Good gravy, that's a lot of parens. Here, you're making the reverse mistake, feeding a boolean value into a method that expects a char. Again, you need to understand types if you're going to figure this out.
Primitive types in java are int, char, double, boolean, plus long and float - I think that's about it. You need to understand what each of these is and what it represents. This is all well documented on line, just google "java int", "java char", etc., and read until you understand it.

Once you understand that, you have to be able to read the sun API and understand what types a method expects, and what it returns.

This is all basic stuff, and until you grasp it, programming in java - even at this level! - will be beyond you. I can't help you until you understand this stuff.

sir it works with me please correct me if i am wrong...

for( i=0;i<names.length();i++)
	{
	     if(Character.isDigit(names.charAt(i)))
	 	{
	 			
	 	   sum =  sum +  Character.getNumericValue(names.charAt(i));
	 			   
	 	}
	 			
	 				
	 }
	 	
 System.out.println("The sum of all integers: " + sum); 
	 			

	 		
	 	  }
	 		
	 			
	 }

Looks good to me.
Have you tested it against various cases? If you want to exercise your code, look for cases that will strain it a little. "jemz123" was your test case before, so I assume it works with that now. Does it work when the integers are at the start of the string? In the middle? Will it work if you have ints and chars mixed up in a string? (I think you'll pass all of those cases, but try them anyway)

Then you might want to look at your requirements. "Add up all the integers appearing in a string." Okay, what about -1? Do you want to handle negative integers? If so, how would you do it? What if some joker wanted to put in hex digits?

All of this is overkill for this program, but if you want to be a better programmer, you could learn something by modifying this program to handle negatives. If nothing else, you should certainly take a half hour or so to try to think of every input that might come in to the program as you've written it, and testing to be sure it works.
(does it break if you don't give it anything, just the return key?)

I'm not going to go over those for you, that's your business. It'll be good for your head to do the work, not so much if someone does it for you.
Good luck.

Looks good to me.
Have you tested it against various cases? If you want to exercise your code, look for cases that will strain it a little. "jemz123" was your test case before, so I assume it works with that now. Does it work when the integers are at the start of the string? In the middle? Will it work if you have ints and chars mixed up in a string? (I think you'll pass all of those cases, but try them anyway)

Then you might want to look at your requirements. "Add up all the integers appearing in a string." Okay, what about -1? Do you want to handle negative integers? If so, how would you do it? What if some joker wanted to put in hex digits?

All of this is overkill for this program, but if you want to be a better programmer, you could learn something by modifying this program to handle negatives. If nothing else, you should certainly take a half hour or so to try to think of every input that might come in to the program as you've written it, and testing to be sure it works.
(does it break if you don't give it anything, just the return key?)

I'm not going to go over those for you, that's your business. It'll be good for your head to do the work, not so much if someone does it for you.
Good luck.

hello sir thank you for the reply, yes it works and your help is useful to me, about the -1 what do you want me to do, refrain user to input negative numbers?....hoping for your positive responds...

It's your program. It's up to you to decide what the correct behavior for negative integers should be. If you want to ignore them, your code will do that - it'll just add in the integer part and skip the (non-numeric) negative sign. On the other hand, if on reflection you decide you'd like to try to handle them, you can figure out how to do that.

So if someone enters "jon-13-525kiparsky", should they get -1+3+-5+2+5=4 or should they get 1+3+5+2+5=16? If the latter, you can just leave it as it is. If you think it's the former, you have to change the program.

Since this is not a program that's going to serve any practical purpose beyond what it teaches you in writing it, this is just a suggestion that I think would be helpful to you. Figuring it out shouldn't be very difficult, but it will probably take a bit of effort on your part, especially since I'm not going to help you. Give it a try if you like, or don't.

It's your program. It's up to you to decide what the correct behavior for negative integers should be. If you want to ignore them, your code will do that - it'll just add in the integer part and skip the (non-numeric) negative sign. On the other hand, if on reflection you decide you'd like to try to handle them, you can figure out how to do that.

So if someone enters "jon-13-525kiparsky", should they get -1+3+-5+2+5=4 or should they get 1+3+5+2+5=16? If the latter, you can just leave it as it is. If you think it's the former, you have to change the program.

Since this is not a program that's going to serve any practical purpose beyond what it teaches you in writing it, this is just a suggestion that I think would be helpful to you. Figuring it out shouldn't be very difficult, but it will probably take a bit of effort on your part, especially since I'm not going to help you. Give it a try if you like, or don't.

thank you sir....more power to you...

Same back atcha. Good luck.

Same back atcha. Good luck.

sir what is atcha?sir i send you private message a while ago...

Idiomatic English, sorry. Same back at you, in an informal spelling. In other words, thanks for your good wishes, and the same good wishes to you.

Idiomatic English, sorry. Same back at you, in an informal spelling. In other words, thanks for your good wishes, and the same good wishes to you.

oh!,okay i understand...:)

Idiomatic English, sorry. Same back at you, in an informal spelling. In other words, thanks for your good wishes, and the same good wishes to you.

oh!,okay i understand... :)

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.