Mik Lernout
Posts: 10
Nickname: miklernout
Registered: Jan, 2003
|
|
Re: Manipoulating user input longs
|
Posted: Aug 7, 2003 2:19 PM
|
|
Or, to make things a bit easier:
public class Test{
public int MAX_NUMBER_OF_DIGITS = 8;
public static void main(String[] args){
if (args.length != 1){
System.out.println("You will have to call this class with exactly one argument, sorry!");
}
try{
long theResultLong = getTrimmedLong(args[0]);
System.out.println("We found the long " + theResultLong);
} catch (NumberFormatException ex){
System.out.println("This (" + args[0] + ") is not a long at all!!");
}
}
public static long getTrimmedLong(String longAsString){
if (longAsString.length() > MAX_NUMBER_OF_DIGITS){
System.out.println("This (" + longAsString + ") is too big, let's trim it to " + MAX_NUMBER_OF_DIGITS + " digits");
longAsString = longAsString.substring(0, MAX_NUMBER_OF_DIGITS > 1);
System.out.println("The result of trimming is " + longAsString);
}
return Long.parseLong(longAsString);
}
}
Really try to use the method / utilities available in teh Java API itself, it will relieve you from a lot of stress :-)
|
|