• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Java help!

Can anyone explain to me how to use the char primitive data type? I need to check a string for a value and i know you can't do it with the string data type.
 
Have you tried something like:


String testing = "1234abcd";

if(testing.compareTo("1234abcd")==0)
{
System.out.println("Checked OK!");
}
 
you can do it with String.... use java.lang.regex or something with Pattern and Matcher.... or you can just write a while loop and the charAt method of String.
 
Originally posted by: b4u
Have you tried something like:


String testing = "1234abcd";

if(testing.compareTo("1234abcd")==0)
{
System.out.println("Checked OK!");
}

You know you can use String.equals()

 
If you are trying to check and see if a single character exists in a string you can use the y.charAt(i) function.

y.charAt(i) treats the string as an array of characters and returns the character in the i position of string y.

Say you had string "ZBCDEFG"
And wanted to know if 'V' was in the string.

presuming:
x = 'V'
y = "ZBCDEFG"

boolean find(char x, string y)
Char test = x
String source = y
Int stringLen = y.length()
boolean answer = false

for (int i = 0; i < stringLen; i++)
if y.CharAt(i) = x
boolean answer = true

return answer
 
Originally posted by: wjsulliv
If you are trying to check and see if a single character exists in a string you can use the y.charAt(i) function. y.charAt(i) treats the string as an array of characters and returns the character in the i position of string y. Say you had string "ZBCDEFG" And wanted to know if 'V' was in the string. presuming: x = 'V' y = "ZBCDEFG" boolean find(char x, string y) Char test = x String source = y Int stringLen = y.length() boolean answer = false for (int i = 0; i < stringLen; i++) if y.CharAt(i) = x boolean answer = true return answer

You would actually have tighter code using the IndexOf command...
 
Back
Top