DEV Community

Cover image for Récupérer les informations saisies au clavier
NDONGO TONUX SAMB
NDONGO TONUX SAMB

Posted on

3 2

Récupérer les informations saisies au clavier

Dans ce tutoriel, nous allons voir comment accepter les entrées de l'utilisateur. Nous utilisons la classe Scanner pour obtenir l'entrée. Dans l'exemple ci-dessous, nous recevons une entrée de type String, integer et float. Pour cela, nous utilisons les méthodes suivantes :

  • 1) public String nextLine() : Pour obtenir l'entrée String
  • 2) public int nextInt() : Pour l'entrée d'un nombre entier
  • 3) public float nextFloat() : Pour l'entrée des flottants
import java.util.Scanner;

class GetInputData
{
  public static void main(String args[])
  {
     int num;
     float fnum;
     String str;

     Scanner in = new Scanner(System.in);

     //Obtenir la chaîne d'entrée
     System.out.println("Saisir une chaine: ");
     str = in.nextLine();
     System.out.println("La chaine saisie est: "+str);

     //Get input Integer
     System.out.println("Saisir un entier: ");
     num = in.nextInt();
     System.out.println("L'entier saisi est: "+num);

     //Get input float number
     System.out.println("Entrez un numéro de flottant: ");
     fnum = in.nextFloat();
     System.out.println("Le numéro de flottant est : "+fnum); 
  }
}
Enter fullscreen mode Exit fullscreen mode

Sortie sur la console.

Saisir une chaine: 
Astou
La chaine saisie est: Astou
Saisir un entier: 
313
L'entier saisi est: 313
Entrez un numéro de flottant: 
12.56
Le numéro de flottant est: 12.56
Enter fullscreen mode Exit fullscreen mode

Sans plus tardé, on va maintenant voir comment faire des conditions avec Java.

prochain chapitre: les conditions

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay