• Post Reply Bookmark Topic Watch Topic
  • New Topic

comparing to char from two diferent strings.  RSS feed

 
Sean Paulson
Ranch Hand
Posts: 87
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
im trying to compare the first characters of two strings.
i Know there's set characters banana and b but it's from a learning program I'm using zbooks i belive


in the if statement i get this error Cannot invoke charAt(int) on the primitive type char
 
Dennis Grimbergen
Ranch Hand
Posts: 159
IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Primitives don't have methods. You are calling charAt(0) on firstLetter, which is a primitive char.
What you try to do is comparing two chars. FirstLetter is already a char, so just compare userInput.charAt(0) with firstLetter.
 
Sean Paulson
Ranch Hand
Posts: 87
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dennis Grimbergen wrote:Primitives don't have methods. You are calling charAt(0) on firstLetter, which is a primitive char.
What you try to do is comparing two chars. FirstLetter is already a char, so just compare userInput.charAt(0) with firstLetter.


Ok thank you it worked but how does it know to compare firstLetters first char, is it because userInput.charAt(0) is telling it to already?
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Look up the method of the String class and the first one you get to is charAt. If you read that link, you find it says char to the left of charAt. That means, if you run charAt, the whole expression
input.charAt(0)
turns into a char. Your firstLetter variable is already a char, so you now have a char on the left of the == sign and a char on the right of the == sign. AOK.
By the way: don't hard‑code your variables and don't declare them before you need them. You can lose lines 4-5 like this:-I have also removed the Scanner import because you never used it. The name of the class is hardly informative, but it should start with T not t.
You can delete the return statement too, because that is redundant. If you don't hard‑code the variables you can use different values for each run. If you use a Scanner, try this:-
campbell@campbellsComputer:~/java$ java TestIf
Please enter word: banana
Please enter letter: b
Found match: b
campbell@campbellsComputer:~/java$ java TestIf
Please enter word: banana x
Please enter letter: No match: x
Note you can enter something for a Scanner before it actually asks for it. Scanner doesn't have a nextChar method so the nearest is next().charAt(0).
 
Sean Paulson
Ranch Hand
Posts: 87
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well the reason it had set variables is because it's code I just did part of from this tutorial I'm doing but thanks allot!! Also just one more q what if I wanted to compare the first char of one string to the second char of another string.

Again thanks for your help though
 
Patrick De
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was looking at your code and tried to make some changes such as finding out the 3rd character rather then the first (I have also tried the 2nd character too) but received this error. It seems to work with ZERO, but not the changes - here is the error and the code below. Forgive my comment statements.

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 3
at java.lang.String.charAt(Unknown Source)
at lookingatletters1.main(lookingatletters1.java:16)


Here is the code :
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are trying charAt(3) that is the fourth keystroke so your input must have 4 or more letters.
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you want charAt(3) on the first input and charAt(0) on the second input.
 
Sean Paulson
Ranch Hand
Posts: 87
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try having the user input another string instead of char, I will try and Mrs around with it when I get off work tonight, just so I can learn manly, I kind of think you would have to extract each char into two var and then compare them, maybe..Ha this is fun though I like this site. XD
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Glad to be of help

By the way: beware of predictive text
 
Patrick De
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, I cannot get this to work.
 
Sean Paulson
Ranch Hand
Posts: 87
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Im.working ot so ima try tonight if I don't.fall asleep.
Try turning each string string into a array of each.char
 
Patrick De
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok I got it to print out the third letter, now I just need to compare it .

I have commented all of the code after to test a bit at a time. I did try the code and received the same error using this method, but at least I know it is looking and printing the third character.


 
Patrick De
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok finally got it, I needed to compare the aChar at character ZERO instead of character 3, which wouldn't work because the user doesn't use that many letters in the second input. If the user types more than one character, it asks them to retype it.

Now how do I get the user to ONLY input ONE letter for the verification? If for example, the user typed in
"What is the word : hello
What is the third letter : llo <----- I only want one letter to be typed in or it tell the user to try again.

I am sure it is the length function. My main challenge is getting it in a WHILE loop(?) or something like that so it continues to ask unless the character length is only 1

 
Liutauras Vilda
Sheriff
Posts: 4913
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David De wrote:Now how do I get the user to ONLY input ONE letter for the verification? If for example, the user typed in
"What is the word : hello
What is the third letter : llo <----- I only want one letter to be typed in or it tell the user to try again.
How about DO WHILE? It seems it should work perfectly in this situation.
 
Patrick De
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think so too, but I am not sure where EXACTLY to put the while loop in this case?
 
Liutauras Vilda
Sheriff
Posts: 4913
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David De wrote:I think so too, but I am not sure where EXACTLY to put the while loop in this case?

Answer:
David De wrote:What is the third letter : llo <----- I only want one letter to be typed in or it tell the user to try again.

Amend your code how do you think, and we'll tell you what we think. Don't be scared of experimenting, see what errors compiler gives you (if some), try to fix them - it is very important learning process.
 
Patrick De
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yep good idea, I have spent most of the morning trying that. It is frustrating, but I think I got the loop part, but
1) trying to break the loop
2) after breaking the loop, do I jump to a method?
3) I need to test to see if they got the right letter or not, but it doesn't seem to be getting me there, so in comment I tried to make it a method, but that gives me errors as well

 
Liutauras Vilda
Sheriff
Posts: 4913
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nice try. Maybe you need to do short brake and then come back to your code.
Couple of things at first:
1. Class name suppose to start with Upper case.
2. Comments makes your code more difficult to read, and they are not explaining much more than java syntax itself (probably delete them).
3. Code is not indented properly, this is quite important.

Ok, this is how you can achieve what you want:
 
Sean Paulson
Ranch Hand
Posts: 87
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try ToArraychar[] or however it's spelled and case each letter that way you could all the user to enter to strings and ask what char to find,
Todd have to make a error for to different size strings
 
Patrick De
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1) I am still learning, and at the very basic stage, so the comments are more for me to understand when I look at it again what the heck I was doing.
2) I know the BREAK command stops the entire program from happening, so I have to rethink that (I think?)
3) Arrays would be nice, but right now just trying to understand how Java works without making it easier with more advanced code.
4) I will fix the class name next time.

SUCCESS ! I think I got it - moving the BREAK to the end of the IF statements seem to work. Which leaves me with another questions, how do you continue a program after the BREAK if the break jumps to the end?

 
Sean Paulson
Ranch Hand
Posts: 87
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok I see yeah thats basically a long way of using case statements or arrars I like it, also you might just want to write it down rapter style or sum to figure that one out
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David De wrote: . . . I know the BREAK command stops the entire program from happening, so I have to rethink that (I think?) . . .
It isn't BREAK but break; And it does not stop the entire program … at all. It is one of the control flow instructions and you will find it is called a branching statement.
 
Patrick De
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I only wrote BREAK in capitals to emphasize that it was a command, not just a word in the sentence.
I am curious about the "break" command, because when I had something after it, it did not execute (I know I must have done something wrong there).
 
Junilu Lacar
Sheriff
Posts: 11476
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David De wrote:I am curious about the "break" command, because when I had something after it, it did not execute (I know I must have done something wrong there).

The statement after doesn't execute because break "short-circuits" the normal execution flow and exits the immediate loop. In your case, the break statements exits from the while-loop that starts on line 27. Note that you should try to avoid using break because it can make the code more difficult to follow. Having multiple break statements in a large loop like what you have can make it even more difficult. This is poor programming practice.
 
Liutauras Vilda
Sheriff
Posts: 4913
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David De wrote:I only wrote BREAK in capitals to emphasize that it was a command, not just a word in the sentence.
I am curious about the "break" command, because when I had something after it, it did not execute (I know I must have done something wrong there).
You messing around the problems. You haven't got first task work. Why you're not using "do-while"? Why do you think "while" loop with "break" statements would achieve this goal better? Each loop has particular purpose, where it fits the best. In this case, I think "do-while" is the best option to use.
Once again:


 
Patrick De
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok I am just learning, I will try a do while loop instead.
 
Patrick De
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does someone have a simple project to try with my new rudimentary skills?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!