File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String to char

 
Phil Campaigne
Greenhorn
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am storing a flag in the database as type char, but it is set from a web page request object as a string. I need to convert it from type String to char in java. I tried String.charAt(0) which seems to convert to a char type untill I assign it to a variable of type char:
char processed = processed_string.charAt(0);
when I execute this I get the error message: type undefined.

Any ideas what I am doing wrong?
Thanks in advance,

Phil
 
Luke Shannon
Ranch Hand
Posts: 239
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am sure there is an easier way to do this. However, I just wrote something that went from a String to char sequence. Pretty easy to get a char from the sequence if you know what the position will be:

CharSequence inputStr = "1";
char number = inputStr.charAt(0);

Luke
 
Cay Horstmann
author
Ranch Hand
Posts: 137
12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Phil--you are basically doing the right thing by calling charAt. There must be some other reason for the error message. Maybe you can post a bit more of the code or even a small program that shows the problem?

Cheers,

Cay
 
Anton Golovin
Ranch Hand
Posts: 476
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
char oneChar = (char) aString.charAt(0);

But you're doing it correctly, as per the API... What could be wrong? Is the message from the database or from the Java program?
[ November 17, 2004: Message edited by: Anton Golovin ]
 
Rick Portugal
Ranch Hand
Posts: 243
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This works for me.It prints "a" as I would expect. You must have something else wrong in your code. Hope that helps.
 
Phil Campaigne
Greenhorn
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I pull the object from the database as follows:
(String) result1.getObject(9)--> "F";
next, I pull the char at [0] from the string:
char processed_string.charAt(0) --> 'F' 70
Now, when I assign that result to a char variable it crashes
char processed = processed_string.charAt(0) --> undefined

Is the 70 confusing things. What does it mean?
Phil
 
Rick Portugal
Ranch Hand
Posts: 243
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Assuming result1 is a ResultSet, you might want to do a result1.getString. Hope that helps.
 
somkiat puisungnoen
Ranch Hand
Posts: 1312
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is the 70 confusing things. What does it mean?


Char --> F , after convert to int equal 70.
 
Nicholas Cheung
Ranch Hand
Posts: 4982
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, which result you expected? a char (F) or an int (70 as an ASCII code of F)?

Nick
 
Phil Campaigne
Greenhorn
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for assuring me that I was on the right track and that 70 is the ascii value for 'F'. This led me to discover that the problem was elsewhere in my constructor and variable assignment.

I'm all set now.
Thanks again,
Phil
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic