[ Java ] Useful methods to save time.
Posted: Fri Aug 30, 2002 12:47 am
Being a JAVA programmer on UVA can be very frustrating, can't it? Well, I created some methods to help myself out and I felt I'd share 'em. I know they're pretty basic, but hey, it might help some of you.
METHODS FOR USING STANDARD IN
notice the "percent" before the numbers... Tribute to C
[java]
/* reads a float as the next token of a file */
/* returns NaN if number is not valid */
static float percentFloat(){
String s = token();
if( s == null ) return Float.NaN;
try{
Float f = new Float( s );
return f.floatValue();
}catch( Exception e ){}
return Float.NaN;
}
/* reads a float as the next token of a file */
/* returns NaN if number is not valid */
static double percentDouble(){
String s = token();
if( s == null ) return Double.NaN;
try{
Double d = new Double( s );
return d.doubleValue();
}catch( Exception e ){}
return Double.NaN;
}
/* reads a long as the next token of a file */
/* since Long has no NaN, I return MIN_VALUE if not valid */
static long percentLong(){
String s = token();
if( s == null ) return Long.MIN_VALUE;
try{
Long l = new Long( s );
return l.longValue();
}catch( Exception e ){}
return Long.MIN_VALUE;
}
/* reads an int as the next token of a file */
/* since Integer has no NaN, I return MIN_VALUE if not valid */
static int percentInt(){
String s = token();
if( s == null ) return Integer.MIN_VALUE;
try{
Integer i = new Integer( s );
return i.intValue();
}catch( Exception e ){}
return Integer.MIN_VALUE;
}
/* read line from stdIn by using \n as delim */
static String readLine(){return token( "\n\r" );}
/* read token from stdIn with standard delims */
static String token( ){return token( " \n\r\t" );}
/* read token from stdIn with custom delims */
/* returns null for end of file or any exceptions */
static String token( String delim ){
char c = delim.charAt(0);
StringBuffer s = new StringBuffer("");
try{
while( delim.indexOf( (int) c ) != -1 && c != 65535 )
c = (char) System.in.read();
while( delim.indexOf( (int) c ) == -1 && c != 65535 ){
s.append( (char) c );
c = (char) System.in.read();
}
}catch( Exception e ){ return (null); }
if( s.toString().equals("") ) return null;
return s.toString();
}
[/java]
Please post a response if you found these at all useful.
METHODS FOR USING STANDARD IN
notice the "percent" before the numbers... Tribute to C
[java]
/* reads a float as the next token of a file */
/* returns NaN if number is not valid */
static float percentFloat(){
String s = token();
if( s == null ) return Float.NaN;
try{
Float f = new Float( s );
return f.floatValue();
}catch( Exception e ){}
return Float.NaN;
}
/* reads a float as the next token of a file */
/* returns NaN if number is not valid */
static double percentDouble(){
String s = token();
if( s == null ) return Double.NaN;
try{
Double d = new Double( s );
return d.doubleValue();
}catch( Exception e ){}
return Double.NaN;
}
/* reads a long as the next token of a file */
/* since Long has no NaN, I return MIN_VALUE if not valid */
static long percentLong(){
String s = token();
if( s == null ) return Long.MIN_VALUE;
try{
Long l = new Long( s );
return l.longValue();
}catch( Exception e ){}
return Long.MIN_VALUE;
}
/* reads an int as the next token of a file */
/* since Integer has no NaN, I return MIN_VALUE if not valid */
static int percentInt(){
String s = token();
if( s == null ) return Integer.MIN_VALUE;
try{
Integer i = new Integer( s );
return i.intValue();
}catch( Exception e ){}
return Integer.MIN_VALUE;
}
/* read line from stdIn by using \n as delim */
static String readLine(){return token( "\n\r" );}
/* read token from stdIn with standard delims */
static String token( ){return token( " \n\r\t" );}
/* read token from stdIn with custom delims */
/* returns null for end of file or any exceptions */
static String token( String delim ){
char c = delim.charAt(0);
StringBuffer s = new StringBuffer("");
try{
while( delim.indexOf( (int) c ) != -1 && c != 65535 )
c = (char) System.in.read();
while( delim.indexOf( (int) c ) == -1 && c != 65535 ){
s.append( (char) c );
c = (char) System.in.read();
}
}catch( Exception e ){ return (null); }
if( s.toString().equals("") ) return null;
return s.toString();
}
[/java]
Please post a response if you found these at all useful.