Computer Science Canada

How do you convert Strings to chars?

Author:  xLockx [ 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...

Author:  wtd [ Wed Nov 30, 2005 2:29 pm ]
Post subject: 

Java:
myString.charAt(0)

Author:  xLockx [ Wed Nov 30, 2005 6:49 pm ]
Post 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

Author:  wtd [ Wed Nov 30, 2005 6:51 pm ]
Post subject: 

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

Author:  xLockx [ Wed Nov 30, 2005 7:35 pm ]
Post 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.

Author:  wtd [ Wed Nov 30, 2005 7:42 pm ]
Post 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);

Author:  JackTruong [ Wed Nov 30, 2005 8:27 pm ]
Post subject: 

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

Author:  MysticVegeta [ Wed Nov 30, 2005 11:37 pm ]
Post 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?

Author:  wtd [ Wed Nov 30, 2005 11:42 pm ]
Post 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."

Author:  MysticVegeta [ Thu Dec 01, 2005 9:09 am ]
Post 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

Author:  Illidan Stormrage [ Thu Dec 01, 2005 2:40 pm ]
Post 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.

Author:  MysticVegeta [ Thu Dec 01, 2005 2:43 pm ]
Post 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

Author:  JackTruong [ Thu Dec 01, 2005 4:19 pm ]
Post 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.

Author:  Dan [ Thu Dec 01, 2005 4:26 pm ]
Post 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".

Author:  Illidan Stormrage [ Thu Dec 01, 2005 5:23 pm ]
Post 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

Author:  Dan [ Thu Dec 01, 2005 5:44 pm ]
Post subject: 

Illidan Stormrage wrote:

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.


Ether way i/this site dose not condone such coments in the help sections. At a least they whould be spam since you did not contrubit at all to the descuction.

Quote:

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.


Yes you are right if we are talking about progaming abliity but i was refuring to being a newbe to this site.

Quote:

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


Also any "veteran" of this site can tell you that insuating my spelling can lead down bad paths (or very funny one depedning on if u mean it jokingly or not).

I am not trying to be an ass or anything i am just trying to keep the fourms runing smoothly and firendely. We whont an everiment where peoleop can ask questions with out worrying about being made fun of for it. To aviod spaming this topic any fruther pleases reply to any coments about admin/moding forms and rules and relguations of the stie to my PM or E-mail.

Thank you.

Author:  Illidan Stormrage [ Fri Dec 02, 2005 12:02 am ]
Post subject: 

'Tis all jokes. My friend actually pushed me to "troll" him on the site. Anyways, why bother converting? Why not just use char instead of string and save you the hassle of converting? But I can tell him later today.

Author:  MysticVegeta [ Fri Dec 02, 2005 12:09 pm ]
Post subject: 

Illidan Stormrage wrote:
And I fixed your spelling mistakes. Wink


if you actually bothered to read what he says on the first page... you wouldnt say that.


: