• Post Reply Bookmark Topic Watch Topic
  • New Topic

String CharAt question  RSS feed

 
Ryan Bishop
Ranch Hand
Posts: 143
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

If I have a string of words and want to get a letter from each word (let's say the first), how do I code for anything past two?

For example, if I have North Atlantic Treaty Organization and I want to convert it to NATO I would do something like:

charAt(0) for N
charAt(x + 1) for A

But what do I do for anything past the second string? If I do charAt(x ++ 1) I get an error.

Thanks.
 
Barry Burd
Author
Ranch Hand
Posts: 133
10
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use the Scanner's next() method to get a word, and then use the charAt(0) method to get the first letter of a word. Repeat this for each word in the phrase. Okay?
 
Ryan Bishop
Ranch Hand
Posts: 143
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Never did that but going to mess around now. Thanks!
 
Ryan Bishop
Ranch Hand
Posts: 143
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I keep getting a findInLine error. This is part of my overall program:



Am I supposed to put something in the () in findInLine? I browsed online and saw some people put (".") but I get an error when I do that.
 
Barry Burd
Author
Ranch Hand
Posts: 133
10
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With your code, I'm getting a compile-time error because you can't call findInLine() on a String object (that is, you can't call threeword.findInLine). I think you want to be calling findInLine() on the scanner object. Also, the Scanner class's findInLine method takes a regular expression as its argument. So, for example, to be finding a character, you want to call findInLine{".") because the dot (when part of a regular expression) stands for "any character.

Just one more thing -- I've tried using findInLine and findWithinHorizon for tasks similar to yours. In some situations, findWithinHorizon works well when findInLine fails me. The findWithinHorizon method requires one more argument (a limit on the number of characters to look at) but often, findWithinHorizon is very helpful.

Let me know if this helps.
 
Ryan Bishop
Ranch Hand
Posts: 143
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Am I even close with something like this? I'm getting really lost on how to get the char from a string (or how to convert them so it works).

 
Barry Burd
Author
Ranch Hand
Posts: 133
10
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about this?

char a = input.findInLine(".").charAt(0);

The findInLine call gets a single character from the input scanner (which contains a single word). So, for example, the findInLine call takes a word like "National" and gets "N" from it. But this result "N" is a string not a char (even though "N" contains only one letter). So to turn the String "N" into the char value 'N', you call charAt(0).
 
Ryan Bishop
Ranch Hand
Posts: 143
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I feel like I've essentially come full circle and with the exact same problem. The second char works fine but the first doesn't and I don't know how to label the third. This is what I have:

 
Barry Burd
Author
Ranch Hand
Posts: 133
10
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about this?



The first call gets the next word from input, and uses charAt(0) to get the initial letter from that first word. The second call gets the next (second) word from input and uses charAt(0) to get the initial letter from that second word. And so on.
 
Ryan Bishop
Ranch Hand
Posts: 143
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That works for the second and third words but now the first word will pull the second letter. I appreciate all your help but I'm sure I'm taking up a large part of your time for something trivial. Thank you for your advice.
 
Barry Burd
Author
Ranch Hand
Posts: 133
10
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The first one didn't pull up the second letter when I ran it. I think you might have an extra statement before my "char a =" statement ... an extra statement that you don't need to have. (... and don't worry about wasting my time. I'm perfectly capable of wasting it without your help :-)
 
Ryan Bishop
Ranch Hand
Posts: 143
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mmmm...I deleted the input = nextInLine() statement and now it works. Thank you so much for your help. I ordered your book (I'm definitely a dummy) and am very thankful you responded so much.

 
Barry Burd
Author
Ranch Hand
Posts: 133
10
Android Java
 
Ryan Bishop
Ranch Hand
Posts: 143
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I continue to struggle with how to manipulate strings. As part of my code I have to indicate an error message and have the user start over if they enter more than four words. This is what I have but anytime
I do something like while(tokens > 3) if gives me an error that ">" is undefined for the type String. It also then creates errors for the chars. I don't understand it. I've tried messing around with whiteSpace() and I get the same error over and over and over again but I don't know what it actually means or how to correct it.

 
Barry Burd
Author
Ranch Hand
Posts: 133
10
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To find out how many elements you have in the tokens array (how many String objects you have in that array) you can ask if tokens.length > 4
As for the error on "char a", I think you're missing an open curly brace after the if condition if (tokens.length > 4) {
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ryan Bishop wrote:I continue to struggle with how to manipulate strings...

Well, to start with, your split() call is wrong. Assuming "words" are delimited with whitespace, the best thing is to use:
threeWord.split("\\s+");

What your call will do is split each character into a separate string.

Try that, see if it helps; and come back if you have any more problems.

Winston
 
Ryan Bishop
Ranch Hand
Posts: 143
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks so much for your guys advice. It works great now.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!