File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes String to char Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "String to char" Watch "String to char" New topic
Author

String to char

Phil Campaigne
Greenhorn

Joined: Nov 17, 2004
Posts: 8
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

Joined: Sep 30, 2004
Posts: 239
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


Luke
SCJP
Cay Horstmann
author
Ranch Hand

Joined: Nov 14, 2004
Posts: 115
    
  11
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


Author of Java 8 for the Really Impatient
Anton Golovin
Ranch Hand

Joined: Jul 02, 2004
Posts: 476
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 ]

Anton Golovin (anton.golovin@gmail.com) SCJP, SCJD, SCBCD, SCWCD, OCEJWSD, SCEA/OCMJEA [JEE certs from Sun/Oracle]
Rick Portugal
Ranch Hand

Joined: Dec 17, 2002
Posts: 243
This works for me.It prints "a" as I would expect. You must have something else wrong in your code. Hope that helps.


IBM 286, SCJP, SCWCD, EIEIO
Phil Campaigne
Greenhorn

Joined: Nov 17, 2004
Posts: 8
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

Joined: Dec 17, 2002
Posts: 243
Assuming result1 is a ResultSet, you might want to do a result1.getString. Hope that helps.
somkiat puisungnoen
Ranch Hand

Joined: Jul 04, 2003
Posts: 1312
Is the 70 confusing things. What does it mean?


Char --> F , after convert to int equal 70.


SCJA,SCJP,SCWCD,SCBCD,SCEA I
Java Developer, Thailand
Nicholas Cheung
Ranch Hand

Joined: Nov 07, 2003
Posts: 4982
So, which result you expected? a char (F) or an int (70 as an ASCII code of F)?

Nick


SCJP 1.2, OCP 9i DBA, SCWCD 1.3, SCJP 1.4 (SAI), SCJD 1.4, SCWCD 1.4 (Beta), ICED (IBM 287, IBM 484, IBM 486), SCMAD 1.0 (Beta), SCBCD 1.3, ICSD (IBM 288), ICDBA (IBM 700, IBM 701), SCDJWS, ICSD (IBM 348), OCP 10g DBA (Beta), SCJP 5.0 (Beta), SCJA 1.0 (Beta), MCP(70-270), SCBCD 5.0 (Beta), SCJP 6.0, SCEA for JEE5 (in progress)
Phil Campaigne
Greenhorn

Joined: Nov 17, 2004
Posts: 8
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String to char