Ciao.
Come richiestomi, vi posto una classe Java per la lettura di tutti i tipi primitivi da tastiera.
I tipi primitivi di Java sono i seguenti:
- byte (intero a 8 bit: da -128 + 127)
- short (intero corto a 16 bit)
- int (Intero a 4 byte)
- long (intero lungo a 64 bit)
- float (numero in virgola mobile a 32 bit)
- double (numero in Floating Poin a doppia precisione 64 bit)
- char (tipo carattere a 16 bit)
- boolean (valore booleano, true o false)
I tipi di dato float e double sono rappresentati secondo lo standard IEEE-754.
Inoltre, questa classe prevede anche un metodo per la lettura di una Stringa qualsiasi.
Questo è il sorgente della classe, potete copiarlo all'interno del vostro editor di testi e compilarlo:
codice:
import java.io.*;
import java.util.StringTokenizer;
public class Tastiera {
private static InputStreamReader is = new InputStreamReader( System.in );
private static BufferedReader br = new BufferedReader( is );
private static StringTokenizer st;
public static String leggiStringa() {
String s = "";
try {
s = br.readLine();
} catch (Exception e) { e.printStackTrace(); }
return s;
}
public static int leggiIntero() {
int i = 0;
try {
String tmp = br.readLine();
st = new StringTokenizer(tmp);
i = Integer.parseInt( st.nextToken() );
} catch (Exception e) { e.printStackTrace(); }
return i;
}
public static float leggiFloat() {
float i = 0f;
try {
String tmp = br.readLine();
st = new StringTokenizer(tmp);
i = Float.parseFloat( st.nextToken() );
} catch (Exception e) { e.printStackTrace(); }
return i;
}
public static double leggiDouble() {
double i = 0;
try {
String tmp = br.readLine();
st = new StringTokenizer(tmp);
i = Double.parseDouble( st.nextToken() );
} catch (Exception e) { e.printStackTrace(); }
return i;
}
public static char leggiCarattere() {
char i = (char) 0;
try {
i = (br.readLine()).charAt(0);
} catch (Exception e) { e.printStackTrace(); }
return i;
}
public static long leggiLong() {
long i = 0L;
try {
String tmp = br.readLine();
st = new StringTokenizer(tmp);
i = Long.parseLong( st.nextToken() );
} catch (Exception e) { e.printStackTrace(); }
return i;
}
public static short leggiShort() {
short i = (short) 0;
try {
String tmp = br.readLine();
st = new StringTokenizer(tmp);
i = Short.parseShort( st.nextToken() );
} catch (Exception e) { e.printStackTrace(); }
return i;
}
public static byte leggiByte() {
byte i = (byte) 0;
try {
String tmp = br.readLine();
st = new StringTokenizer(tmp);
i = Byte.parseByte( st.nextToken() );
} catch (Exception e) { e.printStackTrace(); }
return i;
}
public static boolean leggiBoolean() {
boolean i = false;
try {
String tmp = br.readLine();
st = new StringTokenizer(tmp);
i = new Boolean( st.nextToken() ).booleanValue();
} catch (Exception e) { e.printStackTrace(); }
return i;
}
}
Vi posto anche un esempio di utilizzo di questa classe, che deve essere presente nella cartella dell'applicazione che state realizzando, oppure linkato nella variabile d'ambiente CLASSPATH. L'esempio è banale e mostra l'utilizzo di tutti i metodi forniti dalla classe:
codice:
public class ProvaTastiera {
public static void main(String [] a) {
System.out.print("Scrivi una stringa: ");
String s = Tastiera.leggiStringa();
System.out.println();
System.out.print("Scrivi un numero intero: ");
int i = Tastiera.leggiIntero();
System.out.println();
System.out.print("Scrivi un numero a doppia precisione: ");
double d = Tastiera.leggiDouble();
System.out.println();
System.out.print("Scrivi un valore booleano: ");
boolean b = Tastiera.leggiBoolean();
System.out.println();
System.out.print("Scrivi un numero intero corto: ");
short sh = Tastiera.leggiShort();
System.out.println();
System.out.print("Scrivi un numero intero lungo: ");
long l = Tastiera.leggiLong();
System.out.println();
System.out.print("Scrivi un valoer byte: ");
byte by = Tastiera.leggiByte();
System.out.println();
System.out.print("Scrivi un carattere: ");
char c = Tastiera.leggiCarattere();
System.out.println("Questi sono i valori digitati:");
System.out.println();
System.out.println("Stringa: " + s);
System.out.println("Intero: " + i);
System.out.println("Double: " + d);
System.out.println("Booleano: " + b);
System.out.println("Intero corto: " + sh);
System.out.println("Intero lungo: " + l);
System.out.println("Byte: " + by);
System.out.println("Carattere: " + c);
}
}
come potete notare è molto semplice da usare e non richiede l'istanziazione di alcun oggetto, dato che i metodi sono tutti statici.
In questo modo vi potete risparmiare la procedura di creazione degli oggetti atti alla ricezione dell'input da tastiera e tutti i problemi relativi alla cattura delle possibili eccezioni.
Ciao.