Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Convert String to a Number

Status
Not open for further replies.

wgcs

Programmer
Mar 31, 2002
2,056
EC
What you can do :
Code:
String toto= new String("1234");
Integer totoInt = new Integer(toto);
int totoVal = totoInt.intValue();

hope that helps. Water is not bad as soon as it stays out human body ;-)
 
It helped alot, Thanks! I had just found the Integer( String ) constructor, but the examples helped me put it all together. Can you tell me if this is good Java code (It does compile, but it seems a little long-winded) for validating a CC expiration date against today's date?
Code:
 Calendar cal = new GregorianCalendar();
 int year  = new Integer( cal.get(Calendar.YEAR)    ).intValue(); // 2002
 int month = new Integer( cal.get(Calendar.MONTH)+1 ).intValue(); // 0=Jan, 1=Feb, ...
 month++; // Correct for 0-based calendar
     
 // // Parse decimal string
 int nowYr = new Integer(req.getParameter("PayExpMo")).intValue();
 int nowMo = new Integer(req.getParameter("PayExpYr")).intValue();

 if (  (nowYr>year) || ( (nowYr==year) && (nowMo>month) ) )
  { errorlist += "The Credit Card has already Expired."; }
 
Here is some sample code to play with :):
Code:
import java.util.Date;
import java.util.Calendar;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

public class CCExpire {

  private static final DateFormat df = new SimpleDateFormat("dd MMM yyyy HH:mm:ss:SSS");

  private Calendar expiry = Calendar.getInstance();

  public CCExpire(String month, String year) 
      throws IllegalArgumentException, NumberFormatException
  {
    if(year.length() != 4)
      throw new IllegalArgumentException("year must be 4 digits");
    /*
       The card is valid up to and including the last day of the expiry
       month, so set the expiry date as the first day of the next month.
       Remember that months are 0-based so:

       m = Integer.parseInt(month) - 1   // to make 0-based
                                   + 1   // to make next month
    */
    int m = Integer.parseInt(month);
    int y = Integer.parseInt(year);
    expiry.set(Calendar.MONTH, m);
    expiry.set(Calendar.YEAR, y);
    expiry.set(Calendar.DATE, 1);
    expiry.set(Calendar.HOUR_OF_DAY, 0);
    expiry.set(Calendar.MINUTE, 0);
    expiry.set(Calendar.SECOND, 0);
    expiry.set(Calendar.MILLISECOND, 0);

    synchronized(df) {
      System.out.println("Expires: "+df.format(expiry.getTime()));
    }
  }

  public boolean isExpired() {
    return new Date().after(expiry.getTime());
  }

  public static void main(String[] args) {

    CCExpire ccard = null;

    if(args.length != 2)
      System.exit(0);

    try {
      ccard = new CCExpire(args[0], args[1]);
      synchronized(df) {
        System.out.println(df.format(new Date())+" expired: "+ccard.isExpired());
      }
    } catch(Exception e) {
      e.printStackTrace();
    }
  }
}
HTH, Cheers Neil ;-)
 
Thanks, Neil;

That looks like the reusable, truly-Java way to do it!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top