Two Laptop Bag
The moose likes Beginning Java and the fly likes String to char problems Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "String to char problems" Watch "String to char problems" New topic

String to char problems

Kevin P Smith
Ranch Hand

Joined: Feb 18, 2005
Posts: 362

Trying to compare the first charater from SQL return with all the characters from the Alphabet.

I have all the code apart from one problem. The alphabet characters are of type 'char' and the SQL returns of type 'String' (or .substring(0,1) to be exact) but when I try to compare the alphabet letter with the String
( if(char == string)... ) I get a "Incompatible type for char. Can't convert char to java.lang.String" error.

So I need to either find a way to compare a char to a String.substring or a way of converting a String.substring to a char and comparing that to another char.
[ November 30, 2005: Message edited by: Bear Bibeault ]
Ben Souther

Joined: Dec 11, 2004
Posts: 13410

You could compare them as strings.

There is also the String.charAt(int) method...
[ November 30, 2005: Message edited by: Ben Souther ]

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Kevin P Smith
Ranch Hand

Joined: Feb 18, 2005
Posts: 362

Sometimes looking at things from a different angle...

Job done,

Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63548

Moved to Java in General (beginner)

[Asking smart questions] [About Bear] [Books by Bear]
I agree. Here's the link:
subject: String to char problems
It's not a secret anymore!