Pregunta

I just started a new java project today, and I'm having a problem with println. Here's my main method:

public static void main(String[] args) {
    String stringNumGuards = JOptionPane.showInputDialog("How any guards do you have?");
    int numGuards = Integer.parseInt(stringNumGuards);
    Controller headGuard = new Controller();
    System.out.println("You have ", numGuards, " guards");
} //main

The javac output

Controller.java:10: cannot find symbol
symbol  : method println(java.lang.String,int,java.lang.String)
location: class java.io.PrintStream
        System.out.println("You have ", numGuards, " guards");

What did I do wrong? I've never had problems with println before.

¿Fue útil?

Solución

You concatenate Strings with + not ,

System.out.println("You have ", numGuards, " guards");

Should become

System.out.println("You have " + numGuards + " guards");

Otros consejos

You need to have your println like this:

System.out.println("You have " + numGuards + " guards");

This concatenates a string with a variable that you put in the println statement.

In java, you have to give + symbol instead of , in the println method to concatenate the strings. So you have to enter like this.

System.out.println("You have " + numGuards + " gurads");
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top