943,987 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 1227
  • Java RSS
Nov 5th, 2009
0

problem with character space (character search)

Expand Post »
Hi :-)

Yeah I'm new to this whole thing, that's why I'm probably going to ask a lot of stupid questions, I apologize in advance..
The story is: I'm writing a program to enter a string and a search character and output the number of times this character occured in the string.

Java Syntax (Toggle Plain Text)
  1. // s for string and c for search character
  2. int i = 0;
  3. int result = 0; //number of occurences
  4. for(i = 0; i < s.length(); i++)
  5. {
  6. if (s.charAt(i) == c) //
  7. result++;
  8. }

Everything is fine, but I haven't got a clue how to handle the situation where the search character is a space (or other special characters i guess)..
Thanks for the help :-)
Last edited by peter_budo; Nov 5th, 2009 at 9:02 am. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks)
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
snehula is offline Offline
3 posts
since Nov 2009
Nov 5th, 2009
0
Re: problem with character space (character search)
You've written a program to count the specific character given c. This character may also be a space. How much can be different characters in your text? Each character has its representation as an integer.
Create an array of sufficient numbers of cells, and then in a single loop process, increase a value of a cell at the index, where index is the integer representation of char. (base ascii set of chars 0-255)
Reputation Points: 123
Solved Threads: 106
Posting Pro
quuba is offline Offline
573 posts
since Nov 2008
Nov 6th, 2009
0

hmm..

Click to Expand / Collapse  Quote originally posted by quuba ...
You've written a program to count the specific character given c. This character may also be a space. How much can be different characters in your text? Each character has its representation as an integer.
Create an array of sufficient numbers of cells, and then in a single loop process, increase a value of a cell at the index, where index is the integer representation of char. (base ascii set of chars 0-255)
What you're saying is get the index position of the given character (is it?), which i had to do for another program.. this one only has to count how many times the character occurs in the input string.
Anyway, my problem is not that I can't count the occurences or find the index positions of the character.
It's all working fine but it won't let me enter space as a search character. I don't know if I'm supposed to reference space some other way but anyway, this is supposed to be a simple little program where the user can enter any search character they like. Apparently the space key is a special character..
Reputation Points: 10
Solved Threads: 0
Newbie Poster
snehula is offline Offline
3 posts
since Nov 2009
Nov 6th, 2009
0
Re: problem with character space (character search)
Quote ...
but it won't let me enter space as a search character
Your program
Java Syntax (Toggle Plain Text)
  1. // s for string and c for search character
  2. int i = 0;
  3. int result = 0; //number of occurences
  4. for(i = 0; i < s.length(); i++)
  5. {
  6. if (s.charAt(i) == c) //
  7. result++;
  8. }
define
Java Syntax (Toggle Plain Text)
  1. char c = ' ';
  2. String s = "space space";
and run
This piece of code does not discriminate spaces
Look in your code places where space is removed
Last edited by quuba; Nov 6th, 2009 at 7:50 pm.
Reputation Points: 123
Solved Threads: 106
Posting Pro
quuba is offline Offline
573 posts
since Nov 2008
Nov 9th, 2009
0

auch!

silly me :-)))
problem was that i was using a class for keyboard input that trims space..
sorry for the trouble :-))) thnx
Reputation Points: 10
Solved Threads: 0
Newbie Poster
snehula is offline Offline
3 posts
since Nov 2009
Nov 9th, 2009
0
Re: problem with character space (character search)
You gave advice.
Reputation Points: 123
Solved Threads: 106
Posting Pro
quuba is offline Offline
573 posts
since Nov 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Help with Code
Next Thread in Java Forum Timeline: Line Clipping and Drawing





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC