'Incompatible operand types char and string - JAVA
The program compares a character to a String and counts the number of times that character repeats in the String. Line 8 gives error "Incompatible operand types char and string" I can't seem to figure out a way around this error:
Scanner input = new java.util.Scanner(System.in);
System.out.println("Enter a word: " );
String word = input.next();
System.out.println("enter a letter: ");
String letter = input.next();
int count = 0;
for(int i =0; i < word.length(); i++)
if(word.charAt(i) == letter)
count++;
Solution 1:[1]
A String
is not a char
. letter
is a String
.
Just test if word.indexOf(letter)
is not -1. And prior to this you want to test whether letter
has length 1.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | fge |