Cryptography Reference
In-Depth Information
//This parsed as an integer-store it, ignoring leading zeros
char[]
c=s.trim().substring(firstDigitLoc,s.length()). toCharArray();
digits=new int[c.length];
//Subtract zeroPos from the character-this gives the
//corresponding int
for (int i=0;i<c.length;i++) digits[i]=(int)c[i]-zeroPos;
}
For output purposes, we should be able to convert an Int object to a string and display
it. Thus, as with all good classes in Java, we will supply a toString() method.
//Returns the Int as a String, mainly for output purposes
public String toString() {
//Use a StringBuffer for efficiency
StringBuffer answer=new StringBuffer(“”);
//Put a “-” symbol in front if negative
if (negative) answer.append(“-”);
//Append each digit to the StringBuffer and return it as a String
for (int i=0;i<digits.length;i++) {
answer.append(new Integer(digits[i]).toString());
}
return new String(answer);
}
Now that we have designed these constructors, we should test them to verify that they
work. The class TestIntConstructors is a console program that simply asks the user to enter
some integers and then converts them to Ints. It then turns them back to strings using the
toString() method and displays them. The code can be found at the topic's website, and an
example run is shown in Figures 2.2(a)-(e).
FIGURE 2.2
(a)
(b)
Search WWH ::




Custom Search