scanf / gets equivalent to java

1

I've searched 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");

  }

}

Is there any easier way to do this? because I do not think I have to use 2 lines of code to do one thing.

    
asked by anonymous 22.07.2014 / 18:12

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 in the beginning is a little tricky to understand, but over time the programmer becomes accustomed to its definition. A simple text scanner can parse primitive types and strings using regular expressions.

The Scanner class has the purpose of separating the input of the texts into blocks, generating the known tokens, which are separated by delimiters that by default correspond to whitespace, tabs and line changes.

With this class text can be converted to primitive types, and these texts can be considered as objects of type String, InputStream and files.

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

Listing 1: Importing the Scanner class

import java.util.Scanner;

As described in the introduction, this class assists in reading the reported data. To do this action in practice, you need to create a Scanner object that passes the System.in object into the 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 you read from the keyboard. See below how to invoke 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 method call to return the specified value in the data entry, always following the format nextType ().

Listing 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();

As the Scanner class works with data entry, it is always good practice to use try / catch to make the systems well built.

    
22.07.2014 / 18:18