Page 2 of 2 FirstFirst 12
Results 21 to 25 of 25
  1. #21
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

  2. #22
    bkim33 is offline Member
    Join Date
    Feb 2011
    Posts
    18
    Rep Power
    0

    Default

    So if we wanted to check if the integer is a double? how would we do that with this code?

    Java Code:
    public static void main(String args[]) {
    		System.out.print(isInteger(clearLeadingSpaces("2.33")));
    	}
    	
    	public static String clearLeadingSpaces(String source) {
    		while (source.length()>0 && source.charAt(0)==' '){
    			source = source.substring(1);
    		}
    		return(source);
    	}
    	public static boolean isInteger(String source) {
    		boolean a = false;
    		char c;
    		for (int x = 0; x < source.length(); x++) {
    			c = source.charAt(x);
    			if (c < '0' || c > '9') {
    				return a;
    			
    			} else {
    				a = true;
    			}
    		}
    			
    		int length = source.length();
    		if (length == 0) {
    			return false;
    		} else {
    			return true;
    		}
    		
    		
    		
    				
    				
    				
    				
    				
    	
    
    		
    		
    	}
    
    
    }
    i tried putting in c != '.' into the test in the if statement but it comes out to false if i enter 23.4 into the parameter

  3. #23
    </3java is offline Member
    Join Date
    Feb 2011
    Posts
    15
    Rep Power
    0

    Default

    I seriously think I am going crazy, this is the correct prompt:

    Java Code:
    // Examples of "good" integer strings:
    	//      "12.7"
    	//      "98."
    	//      "00023.00"         allow leading zeros
    	//      "  87.7   "        allow leading and trailing spaces
    	//
    	// Examples of "bad" integer strings:	
    	//      "34*8D3.456$(9"   bad symbols
    	//      "2.3.6"          too many decimals
    	//      "45 4.54"        string should be one double
    We need stuff for decimals and trailing spaces.
    Last edited by </3java; 02-17-2011 at 06:46 AM.

  4. #24
    </3java is offline Member
    Join Date
    Feb 2011
    Posts
    15
    Rep Power
    0

    Default

    Just a fast random question, how do you convert an int to a double? Because two of the methods are the same except one deals with "returning true if string represents an integer" and other is "returning true if string represents an double".

    The code for integers is above (still have one wrinkle, leading spaces).

  5. #25
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Your isInteger() method can simply use your isLike() method, with a second parameter of "0123456789". (A little extra code will allow a '-' as the first character.)

    -Gary-

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Strings
    By zoala001 in forum Java Applets
    Replies: 9
    Last Post: 01-02-2011, 10:26 AM
  2. Strings and fonts
    By TheGoodGuy in forum New To Java
    Replies: 12
    Last Post: 09-05-2010, 02:01 AM
  3. It is possible in Strings..?
    By mlibot in forum New To Java
    Replies: 1
    Last Post: 03-12-2010, 05:30 AM
  4. Strings and delimeters
    By Manfizy in forum New To Java
    Replies: 12
    Last Post: 06-09-2009, 07:51 AM
  5. Reading in strings
    By thekermo in forum New To Java
    Replies: 2
    Last Post: 10-19-2008, 05:24 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •