scanf/gets equivalent for java

Asked

Viewed 4,727 times

1

good people I’m liking programming in java and my doubt is the following, in C language I learned that for the user to type something is used scanf/gets function and in java which would be??

I looked around and found this

package br.com.treinaweb.parte2;

import java.util.Scanner;/*essa linha*/ 

public class fluxo {

  public static void main(String[] args) {

  Scanner leitor = new Scanner(System.in);/*essa linha*/
  int nota;

  System.out.println ("Insira o numero");  

  nota=leitor.nextInt(); /*essa linha*/



    System.out.println(nota >= 7 ? "Aprovado" : "Reprovado");

  }

}

there is some easier way to do this? because I don’t think I’ll have to use 2 lines of code to do one thing.

  • 1

    You use one line of code only to read. The other is to instantiate the reader. In C, you use the #include <stdio.h> to include the library, and the scanf to read.

1 answer

3


In JAVA the best way to do this is to actually use the Scanner class. The meaning of the Scanner class for many at first is a little complicated to understand, but over time the programmer gets used to its definition. A plain text scanner can parse primitive types and strings using regular expressions.

The Scanner class aims to separate the text input into blocks, generating the well-known tokens, which are character sequences separated by delimiters that by default correspond to whitespaces, tabulations and line changes.

With this class, texts can be converted to primitive types, and these texts can be considered as String, Inputstream and file objects.

In Practice: First of all, it is necessary to know some functions and aspects that this class has to perform the functioning within the expected. When invoked the Scanner class, the compiler will ask to make the following import:

List 1: Importing the Scanner class

import java.util.Scanner;

As described in the introduction, this class helps in reading the informed data. To do this action in practice, it is necessary to create a scanner object that passes as argument the System.in object inside constructor, as follows:

Listing 2: Scanner Statements

import java.util.Scanner;

public class TestaDeclaracaoScanner {
public static void main(String[] args) {
    //Lê a partir da linha de comando
    Scanner sc1 = new Scanner(System.in); 
    String textoString = "Maria Silva";
    //Lê a partir de uma String
    Scanner sc2 = new Scanner(textoString); 
}
}

Listing 3: Counting tokens in a string

import java.util.Scanner;

public class ContaTokens {
public static void main(String[] args) {
    int i = 0;
    Scanner sc = new Scanner(System.in);
    System.out.print("Digite um texto:");
    while(sc.hasNext()){
        i++;
        System.out.println("Token: "+sc.next());
    }
    sc.close(); //Encerra o programa
}
}

The System.in object is what reads what is written on the keyboard. See below how are invoked some of the main methods that match the signature that returns a value of the type that was invoked. That is, for each of the primitives there is a call from the method to return the value specified in the data entry, always following the nextTipoDado format().

List 4: Methods invoked from the Scanner class

    Scanner sc = new Scanner(System.in);

    float numF = sc.nextFloat();
    int num1 = sc.nextInt();
    byte byte1 = sc.nextByte();
    long lg1 = sc.nextLong();
    boolean b1 = sc.nextBoolean();
    double num2 = sc.nextDouble();
    String nome = sc.nextLine();

Since the Scanner class works with data entry, it is always good practice to make use of Try/catch so that systems are well built.

Browser other questions tagged

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