How do I write the external data entry command in Java?

Asked

Viewed 104 times

0

I migrated from C to Java and would like to know which command allows the user to enter external data and what would this command look like in code.

In language in C use the code below:

scanf(%tipo_da_variavel, &nome_da_variavel);
  • 3

    Which command you use in C?

  • In C it would be scanf(%type_da_variable, &variable_name);

  • In java is Scanner ler = new Scanner(System.in); int n = ler.nextInt();

  • @acklay wants to answer?

  • 1

    @acklay reopened.

1 answer

1

The scanf() in C is equivalent to Scanner in Java. See below for an example:

Scanner scanner = new Scanner(System.in);

// valores do tipo inteiros
int n = scanner.nextInt();

// valores do tipo strings
String s = scanner.nextLine(); 

// valores do tipo double
double d = scanner.nextDouble(); 

See more details and examples in the documentation on the scanner class.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.