previous pagecontentnext page

        Lecture console - classe Scanner

La classe Scanner se trouve dans le pacquage util et il possède des méthodes simples de lecture des variable de types primitifs de l'entrés standart (System.in) .

Les méthodes de la classe partage l'information par l'unités  (tokens). On peux choisit differents séparateurs, mais par default on utilise l'éspace.

Quelque méthodes ::

type de résultat Description
Scanner
new Scanner(System.in)  //constructeur
 BigDecimal nextBigDecimal()
          Scans the next token of the input as a BigDecimal.
 BigInteger nextBigInteger()
          Scans the next token of the input as a BigInteger.
 BigInteger nextBigInteger(int radix)
          Scans the next token of the input as a BigInteger.
 boolean nextBoolean()
          Scans the next token of the input into a boolean value and returns that value.
 byte nextByte()
          Scans the next token of the input as a byte.
 byte nextByte(int radix)
          Scans the next token of the input as a byte.
 double nextDouble()
          Scans the next token of the input as a double.
 float nextFloat()
          Scans the next token of the input as a float.
 int nextInt()
          Scans the next token of the input as an int.
 int nextInt(int radix)
          Scans the next token of the input as an int.
 String nextLine()
          Advances this scanner past the current line and returns the input that was skipped.
 long nextLong()
          Scans the next token of the input as a long.
 long nextLong(int radix)
          Scans the next token of the input as a long.
 short nextShort()
          Scans the next token of the input as a short.
 short nextShort(int radix)
          Scans the next token of the input as a short.
  
Exemple:
import java.util.*;

public class StdInputTest

   public static void main(String[] args)
   { 
      Scanner in = new Scanner(System.in);

      // get first input
      System.out.print("What is your name? ");
      String name = in.nextLine();

      // get second input
      System.out.print("How old are you? ");
      int age = in.nextInt();

      // display output on console
      System.out.println("Hello, " + name + ". Next year, you'll be " + (age + 1));
   }
}
 
Lecture console - classe JOptionPane
utilise une pop up  dialog box

 import javax.swing.JOptionPane;

 public class TestInp {

    public static void main( String[] args ){
       String name = "",s_age="";
       int age;
       name=JOptionPane.showInputDialog("Please enter your name");
       s_age=JOptionPane.showInputDialog("How old are You?");
       age=Integer.parseInt(s_age);
       String msg = "Hello " + name + " next year,you'll be " + (age + 1);
   
       JOptionPane.showMessageDialog(null, msg);
    }
}

JOptionPane.showInputDialog() - return une chaine comme résultat











previous pagecontentnext page