Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.
Trending News
Java Compiler Error?
Hey guys I need help to solve this compiler error!
C:\Documents and Settings\Aditya Aditama H\My Documents\CSC\Homework 9\WordProcessor.java:46: char cannot be dereferenced
if ((word.charAt(i)).equals('.'))
thanks
6 Answers
- 1 decade agoFavorite Answer
the charAt(i) function returns a char which is primitive type
and the equals() function can only be applied to objects
you can use == instead
use
if(word.charAt(i) == '.')
good luck.
- 1 decade ago
the method charAt(int x) returns a char - a primitive type. chars do not have the method equals so instead you should do a comparison as you would with other primitive types (ints, doubles, etc.)
if (word.charAt(i) == '.')
Additionally you can cast the char to a string but this is making the program do more work as after converting a string the program will have to break it down from a string to do the equals.
Source(s): B.S. in Computer Science, Carnegie Mellon - srihari_reddy_sLv 61 decade ago
char is a primitive type, not an object type, and cannot be treated as a reference.
So basically look at your code, for variables or return values whose type is char, which you've treated as an object type by trying to access its fields or methods.
You have a String (or perhaps a StringBuffer) which you're calling a method on to get individual characters. Then you're taking the return value of that method (the return value is a char) and calling a method on that.
try to get the string value of it.it should work
from.charAt(i).toString()
should be
String.valueOf(from.charAt(i))
- Anonymous1 decade ago
I recon u shd put the value of charAt(i) in something (like a String x) and then put that thing in the problem statement(if)
char is a primitive type, not an object type, and cannot be treated as a reference.
cheers
- How do you think about the answers? You can sign in to vote the answer.
- Anonymous5 years ago
Read what the error says. It says your code has a function call which can throw an uncaught exception. You need to add a try/catch statement to deal with the exception. I suspect that the exception is expected from one of these two lines: FileWriter fw = new FileWriter("outputFile.txt"); PrintWriter pw = new PrintWriter(fw); It says 'line 27', so check which of those is line 27 in your file. Surround it with a try/catch statement. You don't actually need to do anything in the catch block in order for the program to compile properly, but you do need to have the try/catch statement in place.
- 1 decade ago
Just cast the word.charAt to the type string
if(((string)word.charAt(i)).equals("."))
I hope that helps