+ 2
Beginning Java question
In my code I keep getting errors. What am I doing wrong? public class Program { public static void main(String[] args) { } } // This is a practive run for Java. This is my very first Java Program String name =console.readLine("What is your name?"); String age = console.readLine("What is your age?"); console.printf("user is %s and their name is %s" name, age);
9 Antworten
+ 3
Denise Roßberg, it works if you run it from command line (eg on desktop OS), where console is open, not in Sololearn environment. (You can test return value for null to avoid error)
I think standard PrintStream in System.out you can redirect eg to file or printer, and then you can use console for specific output like service message to user who operate program. Same for standard input
+ 5
Looks like there is a comma missing before " name" in the last line.
+ 3
System.console().readLine() does not work in an IDE.
Try this:
https://code.sololearn.com/cjNRK4uiI2DK/?ref=app
https://www.sololearn.com/learn/Java/2220/?ref=app
+ 2
About System.out.printf()
https://www.baeldung.com/java-printstream-printf
+ 2
import java.io.Console;
public class Main {
public static void main(String[] args) {
Console console = System.console();
String name = console.readLine("What is your name? ");
String age = console.readLine("What is your age? ");
console.printf("user is %s and their name is %s", name, age);
}
}
+ 2
Thank you so much everyone!
+ 2
Markie Alicia Your welcome. Happy coding :)
+ 1
zemiak
I am bit confused:
https://stackoverflow.com/questions/4644415/java-how-to-get-input-from-system-console
What is the advantage of using console?
+ 1
zemiak
It does not work.
https://code.sololearn.com/cn76AA2cCwQm/?ref=app