Post

Visualizzazione dei post con l'etichetta standard input

Lettura da tastiera

DOMANDA: Come posso leggere un input da tastiera in Java? RISPOSTA: In Java esistono diversi modi per leggere da standard input (la nostra cara tastiera). Partiamo dal più generico (e più complesso) BufferedReader : package letturatastiera; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class LetturaDaTastiera {   public static void main(String[] args) throws IOException { InputStreamReader is = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(is); System.out.println( "Inserire una stringa:" ); String letta = br.readLine(); System.out.println( "Stringa letta: " +letta);    } } Vi chiederete: " mamma mia, tutto questo per leggere una riga da tastiera? ". Bella domanda, però c'è una motivazione: con questo codice possiamo gestire qualunque tipo di input, non solo quello da tastiera (quindi File, str