//Méthodes utilitaires pour bien commencer en Java /** U.java Quelques méthodes utiles static void a( String txt) affichage static void a_( String txt) affichage et retour à la ligne static void a( String txt, Exception e) affichage d'une exception Trois méthodes de saisie, après affichage d'une invite public static float float_(String intitulé) public static int int_(String intitulé) { public static String string_(String intitulé) { Trois méthodes de saisie, sans invite public static float float_() public static int int_() public static String string_() La date du jour static String date() static String dateHeure() Des Répertoires (cf System.getProperties()): Répertoire courant: System.getProperty("user.dir") Répertoire d'accueil: System.getProperty("user.home") Répertoires de classes: System.getProperty("java.class.path") En plus: System.getProperty("file.separator") System.getProperty("line.separator"); System.getProperty("path.separator"); System.getProperty("os.name"); System.getProperty("os.version"); * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ import java.io.*; // cf BufferedReader import java.util.GregorianCalendar; // cf date() import java.util.Date; import java.util.Locale; import java.text.DateFormat; /** Classe utilitaire, évitant des frappes au clavier. <li>Faciliter les lectures de valeurs numériques au clavier avec les méthodes <b>int_</b>, <b>float_</b> et string_</b> <br> exemple: int ent=U.int_("Entier ? "); <li>Remplacer: System.out.print par <b>U.a</b> <li>Remplacer System.out.println par <b>U.a_</b> <li>Afficher une exception avec <b>U.a</b>(String message, Exception e) <li>Fournir la date du jour par <b>U.date()</b> <li>Fournir date et heure courante par <b>U.dateHeure()</b> */ public class U { /** Flot tamponné associé à l'entrée standard. */ private static BufferedReader clavier = new BufferedReader( new InputStreamReader(System.in)); // LECTURE D'UN DECIMAL (float) /** Affiche l'intitulé et lit une valeur numérique décimale (lecture bloquante). @param intitulé : message affiché, avant la saisie @return le décimal(float) lu ou <b>Float.NEGATIVE_INFINITY</b> si les caractères lus ne sont pas convertibles en un 'float'. */ public static float float_(String intitulé) { U.a( intitulé ); return float_(); } /** * Lit une valeur numérique décimale (lecture bloquante). * @return le décimal(float) lu ou <b>Float.NEGATIVE_INFINITY</b> si les * caractères lus ne sont pas convertibles en un 'double'. */ public static float float_() { float dd=Float.NEGATIVE_INFINITY; try { String ch = clavier.readLine().trim(); while( ch.length()==0 ) ch = clavier.readLine().trim(); dd=Float.parseFloat(ch); } catch (NumberFormatException e) {U.a("float_(): (décimal attendu)",e); } catch (IOException e) {U.a("float_() (erreur E/S)",e);} return dd; } // LECTURE D'UN ENTIER (int) /** Affiche un intitulé et lit une valeur numérique entière * (cette lecture est bloquante). * * @param intitulé : message affiché, avant la saisie * @return l'entier lu ou Integer.MIN_VALUE si les caractères lus ne * sont pas convertibles en un 'int'. */ public static int int_(String intitulé) { U.a( intitulé ); return int_(); } /** Lit une valeur numérique entière (cette lecture est bloquante). * @return l'entier lu ou Integer.MIN_VALUE si les caractères lus * ne sont pas convertibles en un 'int'. */ public static int int_() { int ii=Integer.MIN_VALUE; try { String ch = clavier.readLine().trim(); // espaces enlevés while( ch.length()==0 ) ch = clavier.readLine().trim(); // Une possibilité: ii=(new Integer(ch)).intValue(); ii = Integer.parseInt(ch); } catch (NumberFormatException e) { U.a("FluxCaractère.int_(): (entier attendue!)",e); } catch (IOException e) { U.a("FluxCaractère.int_() (erreur E/S)",e); } return ii; } // LECTURE D'UNE CHAINE (String) /** Affiche un intitulé et lit une ligne. @param intitulé : message affiché, avant la saisie @return suite de caractères lus (sans R/C - saut de ligne) */ public static String string_(String intitulé) { U.a( intitulé ); return string_(); } /** Lit une ligne. @return suite de caractères lus (sans R/C - saut de ligne) */ public static String string_() { String ch=null; try { ch = clavier.readLine(); } catch(IOException e) { U.a("U.string_() ", e); } return ch; } // POUR ABREGER LA FRAPPE /** Afficher le texte txt. * * Utiliser cette méthode pour abréger la frappe au clavier, car <tt>U.a(</tt> * est plus court à écrire que <tt>System.out.print(</tt> * </p> * @param txt : texte à afficher */ static void a( Object txt) {System.out.print(txt);} /** * Afficher le texte txt, et <b>passer</b> à la ligne * @param txt : texte à afficher */ static void a_( Object txt) {System.out.println(txt);} /** Affiche le texte txt, puis le message lié à une exception. * <p>La pile d'appels n'est pas affichée.</p> * @param txt : texte à afficher * @param e : exception * */ static void a( String txt, Exception e) { a_("\n"+txt); if( e != null) { // a_(" --> "+e.getLocalizedMessage()); // message court a_(" --> " + e.toString()); // e.printStackTrace(); // trace des appels } } // DATE DU JOUR /** * Date et heure au format: 23 décembre 2003, 5:00:16 * @return date et heure sous forme chaîne * nécessite import java.util.GregorianCalendar; */ static String dateHeure() { final String mois[]={"janvier","février","mars","avril","mai","juin" ,"juillet","août","septembre","octobre","novembre","décembre"}; GregorianCalendar cal = new GregorianCalendar(); String hms=""+cal.get(GregorianCalendar.HOUR); int m=cal.get(GregorianCalendar.MINUTE), s=cal.get(GregorianCalendar.SECOND); hms=hms+":"; if( m<10) hms=hms+"0"; hms=hms+m+":"; if( s<10) hms=hms+"0"; hms=hms+s; return "" + cal.get(GregorianCalendar.DAY_OF_MONTH)+' ' + mois[cal.get(GregorianCalendar.MONTH)]+' ' + cal.get(GregorianCalendar.YEAR)+", "+hms; } /** * Date au format java: 23 décembre 2003 * @return date et heure sous forme chaîne * nécessite import java.util.Date; import java.util.Locale; import java.text.DateFormat; */ static String date() { String laDate=DateFormat.getDateInstance( DateFormat.LONG,Locale.FRANCE).format(new Date()); return laDate; } /** Arrêt de l'exécution, qui reprend en tapant R/C */ static void arrêt() { byte aux[]=new byte[80]; try { a("\nSuite >>> "); System.in.read(aux,0,80);} catch (Exception e) {} } } // fin class U // fin U.java