简体   繁体   中英

actual argument cannot be converted by method invocation conversion

What is the correct way to add a char to charQueue which is a final ConcurrentLinkedQueue<Character> parameter?

Oracle seems to say that it should work:

From type char to type Character

http://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html#jls-5.1.7

code:

package telnet;

import static java.lang.System.out;
import java.io.IOException;
import java.io.InputStream;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.logging.Logger;

public class InputStreamWorker {

    private final static Logger LOG = Logger.getLogger(InputStreamWorker.class.getName());

    public InputStreamWorker() {
    }

    public void print(final InputStream inputStream,  final ConcurrentLinkedQueue<Character> charQueue) {

        Thread print = new Thread() {

            StringBuilder sb = new StringBuilder();

            @Override
            public void run() {
                try {
                    char ch = (char) inputStream.read();
                    sb.append(ch);
                    while (255 > ch && ch >= 0) {
                        charQueue.add(ch);
                        ch = (char) inputStream.read();
                        System.out.print(ch);
                    }
                } catch (IOException ex) {
                    out.println("cannot read inputStream:\t" + ex);
                }
            }
        };
        print.start();
    }
}

Extract from build results:

-do-compile:
    [mkdir] Created dir: /home/thufir/NetBeansProjects/TelnetConsole/build/empty
    [mkdir] Created dir: /home/thufir/NetBeansProjects/TelnetConsole/build/generated-sources/ap-source-output
    [javac] Compiling 11 source files to /home/thufir/NetBeansProjects/TelnetConsole/build/classes
    [javac] /home/thufir/NetBeansProjects/TelnetConsole/src/telnet/InputStreamWorker.java:28: error: no suitable method found for add(char)
    [javac]                         charQueue.add(ch);
    [javac]                                  ^
    [javac]     method ConcurrentLinkedQueue.add(Character) is not applicable
    [javac]       (actual argument char cannot be converted to Character by method invocation conversion)
    [javac] Note: Some input files use unchecked or unsafe operations.
    [javac] Note: Recompile with -Xlint:unchecked for details.
    [javac] 1 error

You are right, it should work fine as method invocation conversion allows boxing conversion from char to Character . Your code compiles fine on my machine, so I suspect a NetBean specific issue.

In the meantime, you can explicit the conversion from char to Character to please the compiler. This should do the trick:

char ch = Character.valueOf(inputStream.read());

The problem was of my own creation. In the same package was a Character class which was creating, obvious now, a naming conflict.

Renaming the class to MyCharacter and checking that the Queue used Character has fixed the bug.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM