0

I am trying to compare a user entered character(letter) with individual letters in a word; and generating an appropriate message when this letter matches any of these individual letters in the word. As I ask the user to enter the letter like:

String newLetter;
Scanner keyboard= new Scanner(System.in);
System.out.println("Enter your letter: ");
newLetter = keyboard.next();

It assumes that the entered letter is a string(I can not express is otherwise, for example with char) on the other hand, I express individual letters of a given word as:

char i=word.charAt(n-1);

where n equals the length of the string(word). But, when I tend to compare i with newLetter, to see if these letters are the same or not, I cannot find any solution; however, both of them return a letter but one of them was defined as 'string' and the other one as 'char'. Can you please assist me with that and if there is anything wrong here? Thanks a lot

  • 1
    which character of string you want to compare? – vb_rises Sep 22 '19 at 12:01
  • The question is that for example: you are given a word like " Programming" and you are trying to find out if a user's input letter (for example: 'a') can be found in that word and if yes, at which index(s). – MattBahadori Sep 22 '19 at 12:14
  • You van use `a.indexOf(b);` it will return the index of the character found in String a. If its not in string a it will return -1 – Willem Sep 22 '19 at 12:16
  • Thanks.How may I print the indexes if that letter was found at several indexes? – MattBahadori Sep 22 '19 at 12:26
  • https://stackoverflow.com/questions/5034442/indexes-of-all-occurrences-of-character-in-a-string – vb_rises Sep 22 '19 at 13:59

0 Answers0