Cryptography Reference
In-Depth Information
//This constructor converts a String to an Int. May throw an
//Exception if the String cannot be converted to an Int.
public Int(String s) throws IntException {
//Place the string into an array of characters
char[] temp=s.trim().toCharArray();
//Parse the array.
//First character may be a sign
//firstDigitLoc records index of first digit
int firstDigitLoc=0;
//If “-” sign symbol encountered, make negative Int, move to
//next index
if (temp[0]=='-') {
negative=true;
firstDigitLoc++;
//If “+” just move to next symbol
} else if (temp[0]=='+') {
firstDigitLoc++;
}
int index=firstDigitLoc;
//Check if remaining characters are digits-record # leading
//zeros
boolean significantDigitFound=false;
while (index<temp.length&&Character.isDigit(temp[index])) {
if (!significantDigitFound) {
//Skip any leading zeros
if (temp[index]=='0') firstDigitLoc++;
else significantDigitFound=true;
}
index++;
}
//Throw an exception if nondigit found
if (index<temp.length) throw new IntException(“This is not a
valid integer!”);
//If no significant digit found, this was a string of all zeros
//Make the zero Int and return
if (!significantDigitFound) {
negative=false;
digits=new int[1];
digits[0]=0;
return;
}
Search WWH ::




Custom Search