Lecture d'une variable depuis le clavier

cs_salim1972 Messages postés 2 Date d'inscription dimanche 26 juin 2005 Statut Membre Dernière intervention 26 juin 2005 - 26 juin 2005 à 16:50
Twinuts Messages postés 5375 Date d'inscription dimanche 4 mai 2003 Statut Modérateur Dernière intervention 14 juin 2023 - 26 juin 2005 à 22:27
Dans le langage PASCAL pour lire une valeur depuis le clavier : read(i).
et en JAVA on met quel instruction ?

3 réponses

kaloway Messages postés 358 Date d'inscription jeudi 24 octobre 2002 Statut Membre Dernière intervention 13 avril 2020
26 juin 2005 à 18:41
Scanner s = new Scanner(System.in);

String param = s.next();//pour un string

int value = s.nextInt();//pour un int

s.close();
0
cs_salim1972 Messages postés 2 Date d'inscription dimanche 26 juin 2005 Statut Membre Dernière intervention 26 juin 2005
26 juin 2005 à 20:28
Merci pour la réponse.
Mais c'est quoi la métthode Scanner ? (le traducteur java ne l'a pas reconnue).
0
Twinuts Messages postés 5375 Date d'inscription dimanche 4 mai 2003 Statut Modérateur Dernière intervention 14 juin 2023 111
26 juin 2005 à 22:27
hello si tu est sous jdk 1.5 utilise la classe java.util.Scanner qui
permet les saisie clavier sinon tu upgrade en java 1.5 ou alors tu
utilise cette classe dsl mais avant 1.5 la lecture au clavier etait
tres lourde (cette classe tu la trouve sur le site ou dans les books
java) :



import java.io.*;



public abstract class InputReader {



public static String S() { // Lire un String

String tmp = "";

char C = '\0';

try {

while ( (C = (char) System.in.read()) != '\n') {

if (C !'\r') tmp tmp + C;



}

}

catch (IOException e) {

System.out.println("Erreur de frappe");

System.exit(0);

}

return tmp;

} // fin de S()



public static byte b() {

byte x = 0;

try {

x = Byte.parseByte(S());

} catch (NumberFormatException e) {

System.out.println(e);

System.exit(0);

}

return x;

}



public static short s() {

short x = 0;

try {

x = Short.parseShort(S());

}

catch (NumberFormatException e) {

System.out.println(e);

System.exit(0);

}

return x;

}



public static int i() {

int x = 0;

try {

x = Integer.parseInt(S());

}

catch (NumberFormatException e) {

System.out.println(e);

System.exit(0);

}

return x;

}



public static long l() {

long x = 0;

try {

x = Integer.parseInt(S());

}

catch (NumberFormatException e) {

System.out.println(e);

System.exit(0);

}

return x;

}



public static double d() {

double x = 0.0;

try {

x = Double.valueOf(S()).doubleValue();

}

catch (NumberFormatException e) {

System.out.println(e);

System.exit(0);

}

return x;

}



public static float f() {

float x = 0.0f;

try {

x = Double.valueOf(S()).floatValue();

}

catch (NumberFormatException e) {

System.out.println(e);

System.exit(0);

}

return x;

}



public static char c() {

String tmp = S();

if (tmp.length() == 0)

return '\n';

else {

return tmp.charAt(0);

}

}

}


WORA
0
Rejoignez-nous