Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Testing for a space in an input string  RSS feed

 
Billy White
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am working on code that asks the user for a text string then asks for an integer to be used as the index number to be used to identify the char value of the string. In an effort to identify if the position of the string is a space character, then I want to output a message that the character entered is null (or blank). Below is the code I have works except where commented out. For example, if the user input San Francisco and we look at index value 3, it is the space between the words. Thank you in advance for your help.

 
Joel Christophel
Ranch Hand
Posts: 250
1
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. A space character and a null are two different things. When you press the space bar, it simply inputs an invisible space character. Every character has a certain numerical value assigned to it. The numerical value for the space character happens to be 32. You can either check if the character is equal to 32 or ' '.
2. Where you originally had (len > pos), the sign needed to be switched. Obviously the String length should be allowed to be greater than the input position, but not vice versa.
3. You used the length() method incorrectly. It's str.length() instead of length(str).
 
Campbell Ritchie
Marshal
Posts: 55741
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joel Christophel, please read what appears on the title page of this forum:
We're all here to learn, so when responding to others, please focus on helping them discover their own solutions, instead of simply providing answers.
Simply giving an answer doesn’t help the OP at all; they learn much more by working out the answer for themselves.
Don’t be annoyed with me, but I have pulled rank and removed the answer.
I would suggest some improvements. You should not try to remember the numeric values of chars; that is very error‑prone. How do you know you aren’t going to write 23 by mistake for 32? You can use the == operator, or one of the methods in the Character class (but that method would produce slightly different results). You should use a char literal instead of the number.
Billy White, please be specific about what goes wrong. You didn’t tell us that you are getting a compiler error at that line. I can see why. You need to work out why that code wouldn’t compile.
Neither of you noticed the System.exit() call. If you are going to use System.exit(), don’t pass 0, which represents normal termination. Pass something else because that exit call represents an error (number too large). You should beware of System.exit(). Although it will work nicely in this situation, it can do serious damage in a threaded application by stopping execution when a task is incomplete, and corrupting files.
You don’t need the exit call anyway. You can simply change the if to an if…else and it becomes unnecessary.
 
Campbell Ritchie
Marshal
Posts: 55741
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Although JC is correct that you can have a String longer than the number you enter, the solution suggested is not quite right.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!