English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

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

2006-10-26 15:35:05 · 6 answers · asked by ? 1 in Computers & Internet Programming & Design

6 answers

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.

2006-10-26 18:24:29 · answer #1 · answered by Anand 3 · 2 0

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.

2006-10-26 17:03:45 · answer #2 · answered by mebonnet 3 · 1 0

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))

2006-10-26 19:07:26 · answer #3 · answered by srihari_reddy_s 6 · 1 0

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

2006-10-26 16:06:52 · answer #4 · answered by Anonymous · 0 0

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.

2016-05-21 23:49:28 · answer #5 · answered by Anonymous · 0 0

Just cast the word.charAt to the type string

if(((string)word.charAt(i)).equals("."))

I hope that helps

2006-10-26 16:27:14 · answer #6 · answered by I.M. 2 · 0 0

fedest.com, questions and answers