Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 How do you convert Strings to chars?
Index -> Programming, Java -> Java Help
Goto page 1, 2  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
xLockx




PostPosted: Wed Nov 30, 2005 2:21 pm   Post subject: How do you convert Strings to chars?

Hey, is there any class I can import from the API That would allow me to conver Strings to a character like if the string was 'a' to conver that to a character... or String 'Apple' would be character 'A'

The reason why, I want to have JOptionPane ask for a certian output, than I want to make an if statement

If(they wanted this than)
Do all this
if(they entered thise than)
do this
else
do this certian action.. something like system.out.println("NOOB");

Ok thanks guys...
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Wed Nov 30, 2005 2:29 pm   Post subject: (No subject)

Java:
myString.charAt(0)
xLockx




PostPosted: Wed Nov 30, 2005 6:49 pm   Post subject: (No subject)

Hmm sorry could you be more specific for example
here is a peice of my code

String tableMaterial = "Oak";
tableMaterial.charAt(0);
System.out.println(tableMaterial);

And I want it just to print out O that way I can just easily do

tableMaterial.charAt(0) = new char variable;

Any ideas? Very Happy thanks
wtd




PostPosted: Wed Nov 30, 2005 6:51 pm   Post subject: (No subject)

Strings in Java are immutable. You can't change their contents.
xLockx




PostPosted: Wed Nov 30, 2005 7:35 pm   Post subject: (No subject)

I dont want to change their contents i just want to make it a character

from String "hello"

to character "h" (the first letter of the string)

Hmm Thanks anywho for your help.
wtd




PostPosted: Wed Nov 30, 2005 7:42 pm   Post subject: (No subject)

xLockx wrote:
I dont want to change their contents i just want to make it a character

from String "hello"

to character "h" (the first letter of the string)

Hmm Thanks anywho for your help.


Java:
String originalString = "hello";
char firstChar = originalString.charAt(0);
JackTruong




PostPosted: Wed Nov 30, 2005 8:27 pm   Post subject: (No subject)

Or:
Java:
String originalString = "hello";
originalString = Character.toString( originalString.charAt(0) );
MysticVegeta




PostPosted: Wed Nov 30, 2005 11:37 pm   Post subject: (No subject)

I dont like this in Java, Everything starts at 0. Even an array of [4] means it is really of 5. 0,1,2,3,4. This concept confuses me a lot since I just switched from turing to Java. Same with character locations. They start at 0 too. Any comments/opinion?
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Wed Nov 30, 2005 11:42 pm   Post subject: (No subject)

MysticVegeta wrote:
I dont like this in Java, Everything starts at 0. Even an array of [4] means it is really of 5. 0,1,2,3,4. This concept confuses me a lot since I just switched from turing to Java. Same with character locations. They start at 0 too. Any comments/opinion?


It's a very very common convention, so get used to it.

When you declare an array with "[4]", you are allocating space for 4 items, not 5.

This becomes more clear if you understand how arrays work in C. Think of the subscript as meaning: "this element is located at the beginning of the array plus some number of spaces."
MysticVegeta




PostPosted: Thu Dec 01, 2005 9:09 am   Post subject: (No subject)

wtd wrote:
MysticVegeta wrote:
I dont like this in Java, Everything starts at 0. Even an array of [4] means it is really of 5. 0,1,2,3,4. This concept confuses me a lot since I just switched from turing to Java. Same with character locations. They start at 0 too. Any comments/opinion?


It's a very very common convention, so get used to it.

When you declare an array with "[4]", you are allocating space for 4 items, not 5.

This becomes more clear if you understand how arrays work in C. Think of the subscript as meaning: "this element is located at the beginning of the array plus some number of spaces."


yeah i have to get used to it, cant make the java people change the language for me now can i? Laughing lol
Illidan Stormrage




PostPosted: Thu Dec 01, 2005 2:40 pm   Post subject: (No subject)

Don't help this guy, he's too much of a n00b. I know how to do it, he just needs to apply his Java skills. This is how he's been getting by this whole course, by other people.
MysticVegeta




PostPosted: Thu Dec 01, 2005 2:43 pm   Post subject: (No subject)

Illidan Stormrage wrote:
Don't help this guy, he's too much of a n00b. I know how to do it, he just needs to apply his Java skills. This is how he's been getting by this whole course, by other people.


I smell flame/anger. By the ways, Not everyone is good at compsci, programming is just one of the things you get by practising and interest, if the person doesn't have an interest, its not his fault is it? Besides, if you are better than him in compsci, he might be better than you in something else like art, math, science, etc.. Also, the word "n00b" is offending for beginners. Have a little respect for new people. Smile
JackTruong




PostPosted: Thu Dec 01, 2005 4:19 pm   Post subject: (No subject)

MysticVegeta wrote:
I dont like this in Java, Everything starts at 0. Even an array of [4] means it is really of 5. 0,1,2,3,4. This concept confuses me a lot since I just switched from turing to Java. Same with character locations. They start at 0 too. Any comments/opinion?
Starting at 0 is just one of those things you have to accept. I mean, why does November have 30 days? Can you change it? What about the year, 2005 is it? It can easily be the year 7574 or 44.

It was agreed upon by many people that the year is 2005, so everybody is in agreeance. Same with Java, arrays start at 0 because it was agreed upon.

If you don't like Java's method, then forget it, Java isn't for you. Switch to VB if you want to start at 1 or whatever number you choose.
Dan




PostPosted: Thu Dec 01, 2005 4:26 pm   Post subject: (No subject)

Illidan Stormrage wrote:
Don't help this guy, he's too much of a n00b. I know how to do it, he just needs to apply his Java skills. This is how he's been getting by this whole course, by other people.


Pleases refrain from insuating peoleop on this site, epsltaly in the help sections. People on this site are not going to do peoleops home work for them but we do offer help on questions about progaming and progaming conspects. I see nothing wrong with his questions and i think that you need to repsected his right to learn. Also since you only have 1 post on this site i do not think you should be calling any one a "n00b".
Computer Science Canada Help with programming in C, C++, Java, PHP, Ruby, Turing, VB and more!
Illidan Stormrage




PostPosted: Thu Dec 01, 2005 5:23 pm   Post subject: (No subject)

Hacker Dan wrote:
Illidan Stormrage wrote:
Don't help this guy, he's too much of a n00b. I know how to do it, he just needs to apply his Java skills. This is how he's been getting by this whole course, by other people.


Please refrain from insulting people on this site, especially in the help sections. People on this site are not going to do people's home work for them but we do offer help on questions about progaming and progaming conspects. I see nothing wrong with his questions and I think that you need to repsected his right to learn. Also since you only have one post on this site I do not think you should be calling anyone a "n00b".


I know this guy personally, and we call each other n00b all the time, it's no big deal. But he does always get other people to basically do his work for him. Today he actually addmitted he is a n00b. Oh, and post count has nothing to do with being a n00b. You could be a "veteran" and still be a n00b. I have encountered several "veterans" who are n00bs. Still, what he wants to do is highly easy, and I can show it to him tomorrow how to do it. And I fixed your spelling mistakes. Wink
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 2  [ 18 Posts ]
Goto page 1, 2  Next
Jump to:   


Style:  
Search: