6 Replies - 101875 Views - Last Post: 24 August 2012 - 11:40 AM Rate Topic: -----

#1 Sunhay  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 03-July 07

comparing char values

Posted 16 July 2007 - 07:53 PM

id like to know how i can compare 2 char values? i tried (char1.compareTo(char2) >0) and (char1>char2) but none are working. also i was getting errors such as variable is not of reference type.
Is This A Good Question/Topic? 0
  • +

Replies To: comparing char values

#2 bernard.assaf  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 1
  • View blog
  • Posts: 41
  • Joined: 15-July 07

Re: comparing char values

Posted 16 July 2007 - 10:45 PM

View PostSunhay, on 16 Jul, 2007 - 07:53 PM, said:

id like to know how i can compare 2 char values? i tried (char1.compareTo(char2) >0) and (char1>char2) but none are working. also i was getting errors such as variable is not of reference type.



make sure your chars are initialized then use direct comparison because these are primitive data type --> no methods for them


	   char c1='f'; // what you have as input for char1
	   char c2='g'; // what you have as input for char2

		   if(c1>c2)
		   System.out.println("c1 > c2");
	   else if (c1==c2)
		   System.out.println("c1 = c2");
	   else
		   System.out.println("c1 < c2");


This post has been edited by bernard.assaf: 16 July 2007 - 10:47 PM

Was This Post Helpful? 1

#3 chris.tkd  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 74
  • Joined: 26-September 06

Re: comparing char values

Posted 17 July 2007 - 01:50 AM

You could use,

if(c1 == c2)
	//Do something



Where c1 and c2 are charachters defined as
char c1 = 'x';
char c2 = 'y';


or
if('x' > 'y')
	//Do something



This follows normal lexographic ordering

Or use the method

c1.compareTo(c2)
which returns an int value. For more information http://java.sun.com/.../Character.html
Was This Post Helpful? 0
  • +
  • -

#4 udugal2  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 5
  • Joined: 12-July 07

Re: comparing char values

Posted 17 July 2007 - 09:42 PM

View Postbernard.assaf, on 16 Jul, 2007 - 10:45 PM, said:

View PostSunhay, on 16 Jul, 2007 - 07:53 PM, said:

id like to know how i can compare 2 char values? i tried (char1.compareTo(char2) >0) and (char1>char2) but none are working. also i was getting errors such as variable is not of reference type.



make sure your chars are initialized then use direct comparison because these are primitive data type --> no methods for them


	   char c1='f'; // what you have as input for char1
	   char c2='g'; // what you have as input for char2

		   if(c1>c2)
		   System.out.println("c1 > c2");
	   else if (c1==c2)
		   System.out.println("c1 = c2");
	   else
		   System.out.println("c1 < c2");






	   char ch1='f'; // your  input for first character
	   char ch2='g'; // your  input for second character

		 
	   else if (ch1==ch2)
		   System.out.println("matched");
	   else
		   System.out.println("unlucky not matched");


Was This Post Helpful? 0
  • +
  • -

#5 Programmist  Icon User is offline

  • CTO
  • member icon

Reputation: 252
  • View blog
  • Posts: 1,833
  • Joined: 02-January 06

Re: comparing char values

Posted 18 July 2007 - 05:34 AM

Another specialized way to compare characters is to use a switch block. This is only useful if you want to match certain characters and then do something different on each match.


char c = 'b';

// This block prints "B"
switch ( c ) {
   case a:
   System.out.println("A");
   break;

   case b:
   System.out.println("B");
   break:

   case c:
   System.out.println("C");
   break;
}

This post has been edited by alcdotcom: 18 July 2007 - 05:34 AM

Was This Post Helpful? 0
  • +
  • -

#6 comoco  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 18-July 07

Re: comparing char values

Posted 18 July 2007 - 04:29 PM

View PostSunhay, on 16 Jul, 2007 - 07:53 PM, said:

id like to know how i can compare 2 char values? i tried (char1.compareTo(char2) >0) and (char1>char2) but none are working. also i was getting errors such as variable is not of reference type.



Sunhay, you are using Java, right? I ask because (char1 > char2) works as expected in all versions of Java.

It's true that char1.compareTo(char2) doesn't compile, but the following works in Java 5:

Character.valueOf(c1).compareTo(c2)

and the following works in all Java:

Character.valueOf(c1).compareTo(Character.valueOf(c2))

Hope this helps!

Corey
Was This Post Helpful? 0
  • +
  • -

#7 2145  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 24-August 12

Re: comparing char values

Posted 24 August 2012 - 11:40 AM

View Postbernard.assaf, on 16 July 2007 - 10:45 PM, said:

View PostSunhay, on 16 Jul, 2007 - 07:53 PM, said:

id like to know how i can compare 2 char values? i tried (char1.compareTo(char2) >0) and (char1>char2) but none are working. also i was getting errors such as variable is not of reference type.



make sure your chars are initialized then use direct comparison because these are primitive data type --> no methods for them


	   char c1='f'; // what you have as input for char1
	   char c2='g'; // what you have as input for char2

		   if(c1>c2)
		   System.out.println("c1 > c2");
	   else if (c1==c2)
		   System.out.println("c1 = c2");
	   else
		   System.out.println("c1 < c2");


what if i want to arrange the names of students in order of their surname?
i.e
user insert random full names and it gets arranged ,based on surname...
can a<b(i.e. first char of surname is compared n the names get arranged) work here?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1