Java Java Basics Getting Started with Java Receiving Input

I'm not using console,I used System.out &its been working so far but now for System.out.printf.readLine(" ") ...

it doesnt work, It says 'Error: printf cannot be resolved or is not a field' , What can I do to make this work without putting console

Armin Halilovic
Armin Halilovic
2,933 Points

Hi Carolina, maybe you have already found an answer in the meantime but nonetheless here is one possible way to get the input from the user without using the console keyword:

There is a Scanner class in Java which allows to scan text. You have to import the Scanner class, create a Scanner object and use the .nextLine() method to read the input from the user:

import java.io.Console;

import java.util.Scanner;

public class Introductions {

public static void main(String[] args) {
    Console console = System.console();

    // Create the Scanner Object
    Scanner scanner = new Scanner(System.in);

    // Print out your statement to the user
    System.out.printf("Enter Username: \n");

    // Read the user input (strings)
    String userName = scanner.nextLine();
    // Print out the input from the user
    System.out.printf(userName);

    // To read integers you have to use nextInt()
    // To read doubles you have to use nextDouble()
    // and so on...

} }

Hope this helps!