# Thread: Need help with Strings

1. lol, did you solve it?

2. Member
Join Date
Feb 2011
Posts
18
Rep Power
0
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. Member
Join Date
Feb 2011
Posts
15
Rep Power
0
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 07:46 AM.

4. Member
Join Date
Feb 2011
Posts
15
Rep Power
0
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. Senior Member
Join Date
Mar 2010
Posts
952
Rep Power
7
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 First 12

#### Posting Permissions

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