Back to index

wims  3.65+svn20090927
Utils.java
Go to the documentation of this file.
00001 /*
00002     WIMSchem Elements: Chemistry molecular diagram drawing tool.
00003     
00004     (c) 2005 Dr. Alex M. Clark
00005     
00006     Released as GNUware, under the Gnu Public License (GPL)
00007     
00008     See www.gnu.org for details.
00009 */
00010 
00011 package WIMSchem;
00012 
00013 import java.util.*;
00014 import java.text.*;
00015 import java.lang.*;
00016 import javax.swing.*;
00017 
00018 // A very generic class containing static utility functions, which are painfully absent from the Java libraries.
00019 
00020 public class Utils
00021 {
00022     // pure convenience
00023     public static void write(String S) {System.out.print(S);}
00024     public static void writeln(String S) {System.out.println(S);}
00025 
00026     public static void errmsg(String Title,String Text)
00027     {
00028        JOptionPane.showMessageDialog(null,Text,Title,JOptionPane.ERROR_MESSAGE);
00029     }
00030 
00031     // parse number-from-string functions which return a default value if it's badly formatted, instead of throwing an exception
00032     public static int safeInt(String S,int Def) 
00033     {
00034        if (S==null) return Def;
00035        try {return new Integer(S).intValue();} 
00036        catch (NumberFormatException e) {return Def;}
00037     }
00038     public static int safeInt(String S) {return safeInt(S,0);}
00039     public static double safeDouble(String S,double Def) 
00040     {
00041        if (S==null) return Def;
00042        try {return new Double(S).doubleValue();} 
00043        catch (NumberFormatException e) {return Def;}
00044     }
00045     public static double safeDouble(String S) {return safeDouble(S,0);}
00046 }
00047