View Single Post
  #33 (permalink)  
Old 05-10-2008, 08:38 AM
hardwired hardwired is offline
Senior Member
 
Join Date: Jul 2007
Posts: 1,017
hardwired is on a distinguished road
I dont quite understand, the userPhoneTwo is already parsed to an integer in that first line
Yes, you're right. Sorry, I didn't look closely enough before answering.
Code:
public class Test { public static void main(String[] args) { // b. The phone number of the customer is not blank and // doesn’t exceed 10 characters and is an integer // with positive number only String userPhone = "1234567890"; // Tests for blank phone number: // userPhone.equals("") _or_ userPhone.length() == 0 // Test for integer value: // userPhone.indexOf(".") == -1 boolean hasDecimal = userPhone.indexOf(".") != -1; if(hasDecimal) JOptionPane.showMessageDialog(null, "Not an integer value"); // Or you could catch a NumberFormatException in parsing the string. System.out.println("Integer.MAX_VALUE = " + Integer.MAX_VALUE); // Eleven digit numbers are too big for an int size // so use long data type. Or you could catch an exception // in parsing the string as an int. long phoneNumber = Long.parseLong(userPhone); System.out.printf("userPhone = %s phoneNumber = %d%n", userPhone, phoneNumber); int length = userPhone.length(); if (length == 0 || length > 10 || phoneNumber < 0) { JOptionPane.showMessageDialog(null, "Phone number cannot be " + "longer than 10 digits and must be a positive integer"); System.exit(0); } } }
Reply With Quote