I keep recieving this error when I compile the program:
error: cannot find symbol
if(letter == charAt(0).getTheword())
symbol: method charAt(int)
The word is an arrayList and letter is a keyboard input. What am I doing wrong and what should be changed?
charAt
is a method that operates on a String.
So example usage would be:
String s = "abc";
System.out.println(s.charAt(0)); //prints out 'a';
You need to change condition to
if(letter == getTheword().charAt(0))
ie string should be before the method charAt()
charAt() is a method that only works on Strings, as described in the documentation. It returns the char at the given index. Let's look at a simple example:
String word = "Cow";
char letter = word.charAt(0);
System.out.println(letter);
This will print out the letter (char) "C" to the console, since the letter 'C' is at index 0 of the word "Cow".
So the part where you're going wrong is that you're not specifying on which String you want to call the charAt()
method.
Use this code.... Your not using a string to use charAt
import java.util.ArrayList;
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a character: ");
ArrayList<String> words = new ArrayList<String>();
words.add("Test");
char ch = sc.nextLine().charAt(0);
if(ch == words.get(0).charAt(0)){
System.out.println("Match");
}else{
System.out.println("Do not Match");
}
sc.close();
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.