compare « character « Java Data Type Q&A

1. Comparing a char to a code-point?

What is the "correct" way of comparing a code-point to a Java character? For example:

int codepoint = String.codePointAt(0);
char token = '\n';
I know I can probably do:
if (codepoint==(int) token)
{ ... }
but this ...

2. Compare first three characters of two strings

Strings s1 and s2 will always be of length 1 or higher. How can I speed this up?

int l1 = s1.length();

if (l1 > 3) { l1 = 3; }

if (s2.startsWith(s1.substring(0,l1))) 
 // ...

3. Getting a cryptic error on my simple Java program, comparing char[i] to a string

public static void ejemplosString(String palabra){
        char[] letras = palabra.toCharArray();
        int contadorVocales = 0;


4. How to compare 2 strings containing same characters

My question is that, I have 2 strings, say String1 & String2. Now I want to check whether these 2 strings contain same characters or not, irrespective of their sequence. Suppose String1= ...

5. Java .chartAt(i) comparison issue

Why when comparing a char against another it must be taken also from a string? For example; This does not work

   while(i < t.length() && zeroCount < 5) {

6. Is there such a thing as a wildcard character in Java?

I'm running a comparison program and at the minute it does a direct 'string-to-string' comparison and if they are an exact match it outputs that they are a match. Well, I was ...

7. Comparing chars in Java

It's been a while since I've done any Java so my syntax is not the greatest at the moment. I want to check a char variable is one of 21 specific chars, ...

8. Java String Manipulation : Comparing adjacent Characters in Java

i have the following problem
Given a string, return a "cleaned" string where adjacent chars that are the same have been reduced to a single char. So "yyzzza" yields "yza". ...

9. Comparison Ignoring Leading Characters

I receive strings of the form "0000A", "00000000A", "0A". They can have many or no leading zeros. I want them to evaluate as equal ignoring the leading zeros. ...

10. compare character with single space java

i'm trying to test a program that will print "space" if the user enters a single space. but nothings displayed when i hit space then enter. my aim was really to ...

11. How To Compare Two Strings If they are rearranged

I wanted to know how can I compare two rearranged strings E.g if String a="string" ,String b="tsrngi" ... If I compare a.equals(b), It will return false because of order of ...

12. Character Comparison

Assume that c is a char variable has been declared and already given a value. Write an expression whose value is true if and only if x is what is called ...

13. java - how to compare string from construction through char[]

I tried the following but could not get any answer to comparing 2 String. How can I compare b and x and get 'true'?


class Test {
  public static ...

14. How do I compare two strings of different lengths and return false for just 1 invalid char?

public boolean setValidColor(String input, String colors) {
    int exists;
    isValidColor = true;
    char[] colorch = colors.toCharArray();

15. comparing chars

16. How do I compare the contents of a char?

I got a rather silly assignment in which I am supposed to change all the "com"'s in a String, to "borom"'s. That is, the word "complicated" is supposed to be printed out as "boromplicated". And so on. Anyway, I'm not asking of how to do it. The problem is, how do I check and compare the contents of a StringBuffer? I ...

17. how to compare character to comma

To "dereference" means (in so many words) to do anything that involves putting a dot right after the thing -- i.e., calling a method with thing.something(), or trying to read a member variable, with thing.something else. You're trying to call a method toString() on a char (the return value of charAt() ) and you can't do that. You can't dereference any ...

18. comparing a char?

hi, i tried to make a comparison between 2 character. one character is from user and another character is a dash "-". i tried to compare it like this : if (testelement.compareTo("-")>0|testelement.compareTo("-")<0) then if its true, it will add a value of 1 to counter but, i got this error when i try to compile it : error : char cannot ...

19. comparing characters

Could someone please explain why the following code prints the characters to be tested and the index but fails to actually perform the test that I need. I want to have something that compares characters to look for valid characters. I have a test characters, which is a String and valid characters which is also a String. I then want to ...

20. How to compare each individual char of 2 string

Hi I need to compare the each individual char of 2 string. String a = "Ae12"; String b = "aE12"; // I tried the followings: // a String c = a.substring(0,0); String d = a.substring(1,1); String e = a.substring(2,2); String f = a.substring(3,3); // b String c1 = b.substring(0,0); String d1 = b.substring(1,1); String e1 = b.substring(2,2); String f1 = b.substring(3,3); ...

21. Speical Character in String comparison

This is to do with URL Encoding, so a space character is replaced with %20% (there are a bunch of these conversions). I'm not sure if the JDK/JEE SDK come with a method to convert these back and forth, does anyone else know? e.g. Take the actual URL string and run it through the converter, then do the comparisons. If there ...

22. how to compare characters between two strings?

I haven't seen what other people have suggested thus what I suggest may be same. If it is then please ignore my post. One approach could be by taking s1 and removing occurrence of all s1s characters from s2. Thus what you get after this removal would be the output you are looking for.

23. comparing character in java: uppercase, lowercase, or symbol?

import java.util.Scanner; public class test { public static void main (String[] args) { Scanner keyboard = new Scanner (; System.out.println("please enter a character to test"); char example = keyboard.nextLine().charAt(0); printTypeOfChar(example); } public static void printTypeOfChar(char example) { if("a" <= example && example<= "z") System.out.println ("This is a lowercase character"); else if("A" <= example && example<= "Z") System.out.println("This is an uppercase character"); ...

24. How to compare char

Hello I'm trying to read a single character input and then decide what character it actually is. I can only figure out how to read a string input but not just a character input. Also, once I have that character I don't know the syntax to decide if that character is an A or B or whatever. Thanks, Scott

25. Comparing a character with characters within a string

When I run the applet that uses this piece of code I get the following error: ALERT: java/lang/ClassFormatError: Bad version information. public void checkInput() { //variables char gletter = 'x'; String sword; //split String into stringArray char[] stringArray; stringArray = sword.toCharArray(); readyDataframe(); //set receiver //compare chars byte[] swordBytes = sword.getBytes(); for(int i = 0; i < sword.length; i++) { if(i < ...

26. Compare substring to char

27. Comparing characters.

import java.util.Scanner; class choice { public static void main(String[] args) { System.out.println("(y/n):"); Scanner scan = new Scanner(; String ch1 = scan.nextLine(); char ch = ch1.charAt(0); //turns string into char if (ch=='y') { System.out.println("you chose yes (y)"); } else if (ch=='n') { System.out.println("you chose no (n)"); } else { System.out.println("rrrrrrrrrgggggg, i hate you"); } } }

28. Comparing a char to " ' "

So my code takes a user input such as setq x '(yellow) The first part removes 'setq ', and initializes the program to find the variable name (which will be x). What i am working on now is to find the variable name so in this case 'x'. Java Code: public String var (String s){ if (s.length () == 0){ return ...

29. Comparing Characters

30. compare two char

31. comparing chars

Hi, Ok, the 3==3 is evaluating to false and I thought it would evaluate to true! The error message that I am receiving is 'String index out of range: 3' and I am trying to capture the '3' so that I can return my custom error message. What do I have to do? Many thanks, R

32. Comparing a Char?

33. What easier than comparing 2 one character strings?

What easier than comparing 2 one character strings? But, I can't do it! I've declared String t which prints out as "A". if ( t=="A") doesn't give a true value. If I declare a String initialized to "A", that doesn't fire either. t.equals("A") gives a Null Pointer exception at runtime. If I try to convert them to char, I'm told that ...

34. character comparison

35. Comparing chars...

Well, I thought I did. I've just tried it using that again, because I can understand why it would work, and so was curious to see if it did or not. It worked using that method too this time lol, so there must have been another fault in my code somewhere last time I tried that which somehow caused it not ...

36. Comparing a character at a certain position

Hello, I'm a student in still new to java. I'm trying to compare a character at a certain position to another character. Here is the snippet of code I have but it's not working thus far: else (inputDate.charAt(2) !='/') System.out.println("Please use a forward slash when separating the parts of the date"); For my variable inputDate, at position two, it should contain ...

38. Character comparison problem

i did a big mistake, when debugging, there is a for loop on the if clause, and another if clause under the if it does compares truly with the if clause but, it does not true for if clause under the if clasuse, then debugger goes to for loop.i thought that, upper f clause compares falsely.thank you for least ...

39. How do I compare chars?

40. Compare individual char of 2 string

// and I tried the followings: // a char c = a.charAt(0); char d = a.charAt(1); char e = a.charAt(2); char f = a.charAt(3); // b char c1 = b.charAt(0); char d1 = b.charAt(1); char e1 = b.charAt(2); char f1 = b.charAt(3); if(c==c1&&d==d1&&e==e1&&f==f1) // cannot be compared. Both cases cannot be compared. How can I fix it? Because I want to ...

41. Help comparing Chars

42. comparing character in string

43. Comparison with Special Characters

44. Can't read in string and compare to a character

int die1; // The number on the first die. int die2; // The number on the second die. int roll; // The total roll (sum of the two dice) int rollct; //count the number of rolls int rollz; int games; //# of games played int wins; int losses; String x; //yes or no for more games boolean flag; rollct = 0; ...

45. Comparing char == ' ' (space)

46. problem comparing the char '\n'

47. Char by char comparing

I have to compare the data that is entered in the textfield of a Servlet so that it should not contain any other symbols other than numerics. HOw can i avail that to work. Actually, this we are doing as a part of validating the Phone Number field of the form. So these validations are to be done in the next ...

48. comparing strings as characters

hello all, just a problem......i have got a assignmt, in whoch i have got some doubts...hope some 1 can help me... 1. first we have to input 1 file and have to store all the data which is in the file....the data contain "FTTFTFTFTF" some thing like this...which is the exact answer key for the true/false questions.......i have done this part ...

49. comparing chars