Welcome to Dream.In.Code
Become a Java Expert!

Join 149,478 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,685 people online right now. Registration is fast and FREE... Join Now!




Character to String

 
Reply to this topicStart new topic

Character to String, "+" to '+'

Niha
21 Apr, 2007 - 12:07 AM
Post #1

New D.I.C Head
*

Joined: 20 Apr, 2007
Posts: 1


My Contributions

CODE

if(sign == "+")
   C = (float) A + B;
                if(sign == "-")
                C = (float) A - B;
                if(sign == "*")
                C = (float) A * B;
                if(sign == "/")
                C = (float) A / B;
        


How do i convert "+" to '+' in order to compare values or can Java compare strings?
User is offlineProfile CardPM
+Quote Post

1lacca
RE: Character To String
21 Apr, 2007 - 12:37 AM
Post #2

code.rascal
Group Icon

Joined: 11 Aug, 2005
Posts: 3,822



Thanked: 12 times
My Contributions
Comparing Strings is possible with the Object.equals(Object)method ex.: if(sign.equals("+"){.
Getting the char value of a String like "+" is possible via String.charAt(0).
User is offlineProfile CardPM
+Quote Post

Ellie
RE: Character To String
21 Apr, 2007 - 12:45 AM
Post #3

D.I.C Regular
Group Icon

Joined: 17 Jan, 2007
Posts: 427



Thanked: 4 times
Dream Kudos: 150
My Contributions
The Sting class has a compare method which returns 0 if the strings are equal
CODE

if (sign.compareTo("+") == 0)
.....


Otherwise you can cast your string to a char by declaring a new char variable, and casting the String into it.

CODE
char csign = (char)sign;


Edit: 1lacca got there first

This post has been edited by Ellie: 21 Apr, 2007 - 12:46 AM
User is offlineProfile CardPM
+Quote Post

1lacca
RE: Character To String
21 Apr, 2007 - 12:50 AM
Post #4

code.rascal
Group Icon

Joined: 11 Aug, 2005
Posts: 3,822



Thanked: 12 times
My Contributions
But it's always good to have more options, and looks-like our solutions don't have any intersections smile.gif
User is offlineProfile CardPM
+Quote Post

Ellie
RE: Character To String
21 Apr, 2007 - 01:13 AM
Post #5

D.I.C Regular
Group Icon

Joined: 17 Jan, 2007
Posts: 427



Thanked: 4 times
Dream Kudos: 150
My Contributions
QUOTE(1lacca @ 21 Apr, 2007 - 09:50 AM) *

But it's always good to have more options, and looks-like our solutions don't have any intersections smile.gif


Yeah, I guess it's true there's many ways to skin a cat biggrin.gif
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 03:28PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month