• Post Reply Bookmark Topic Watch Topic
  • New Topic

Taking a string and printing only the digits  RSS feed

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is the code that I have so far, It will only print out the digit if it is entered first..How to I get it to print out all digits? and I am getting an error that the c is not initialized




thank you for your help
 
Bartender
Posts: 689
17
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You print statement does not appear in your for-loop. This means that there will only ever be at-most one print statement executed. If you want to print out every digit then you need to move the print statement into the for-loop.

Are you sure it only prints digits that appear first though? It looks to me like the local variable 'c' will only ever contain the final character from the String.
 
Sammie Hassett
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike. J. Thompson wrote:You print statement does not appear in your for-loop. This means that there will only ever be at-most one print statement executed. If you want to print out every digit then you need to move the print statement into the for-loop.

Are you sure it only prints digits that appear first though? It looks to me like the local variable 'c' will only ever contain the final character from the String.


Thank you so much, that fixed it..Yes i did come across that but now it seems to be working fine. I changed my code to
 
Sammie Hassett
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok so now I have to add it to a toString in the second part of my work,
I am having an error with how to return the toString
 
Saloon Keeper
Posts: 3330
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sammie Hassett wrote:Ok so now I have to add it to a toString in the second part of my work,
I am having an error with how to return the toString

Well, in your toString() method you'll only be able to create a String from fields you have access to. In your case, the class CreditCard has no fields to work with. In your constructor you'll need to process the characters as you've done, but you'll need to store the results in a way (most likely a String) that is useful to your toString() method. To support this you'll probably want a class instance field called 'number' that is a String. I highly recommend that you look up the StringBuilder class API documentation, and specifically, its append() method.
 
Marshal
Posts: 56608
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another thing about the for loop: look in the Java® Tutorials and you find a second version of the for loop which iterates an array without needing the counter. Of course you can't use that on a String directly. Not unless you can find a method in the String class which turns it to a char array.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!