Hello,
I'm really confused..
If I have a string in Java, let's say "Phillip" and I wanted to store this in an array... e.g.
name[] = { "P", "H", "I", "L", "L", "I", "P }
would I use a for loop for the string?
Helo?
Hello,
I'm really confused..
If I have a string in Java, let's say "Phillip" and I wanted to store this in an array... e.g.
name[] = { "P", "H", "I", "L", "L", "I", "P }
would I use a for loop for the string?
Helo?
Do you want to take a String an make a char array out of it? If so,
This is one way to do it.
String name = "Phillip";
char[] arrayName = new char[name.length()];
for(int i = 0; i < name.length(); i++)//.length() is a method for the String class, so it has parenthesis
{
arrayName[i] = name.charAt(i);//charAt() is a method that can be called on Strings take a look in the api if you need more info.
}
Then to print out the array:
for(int i = 0; i < arrayName.length; i++)//.length for an array is not a method so it doesn't have parenthesis
{
System.out.print(arrayName[i]);
}
OK... The easiest way is using split() of String method...
public static void main(String[] args) {
String a = "Phillips";
String[] b = a.split("");
for (int i=0; i<b.length; i++) {
System.out.print(b[i]+" ");
}
}
Do you want to take a String an make a char array out of it? If so,
This is one way to do it.String name = "Phillip"; char[] arrayName = new char[name.length()]; for(int i = 0; i < name.length(); i++)//.length() is a method for the String class, so it has parenthesis { arrayName[i] = name.charAt(i);//charAt() is a method that can be called on Strings take a look in the api if you need more info. }
Then to print out the array:
for(int i = 0; i < arrayName.length; i++)//.length for an array is not a method so it doesn't have parenthesis { System.out.print(arrayName[i]); }
You only need a single method call to get the string as a char array someString.toCharArray()
No need to write a loop for it.
Meh, come on guys...attention to detail:
String name = "Phillip";
char[] letters = name.toUpperCase().toCharArray();
Although this gives a char array; the OP is looking for an array of Strings.
String name = "Phillip";
String[] letters = name.toUpperCase().split("");
Close, this gives an array like this { "", "P", "H", "I", "L", "L", "I", "P" }.
That pesky null entry 0! oy!
String name = "Phillip".toUpperCase();
String[] letters = new String[name.length()];
for (int i = 0; i < letters.length; i++)
letters[i] = name.substring(i, i + 1);
Although this gives a char array; the OP is looking for an array of Strings.
Not necessarily. He said "array" not String array. And just because what he posted was a String array neither means that he wants or needs a String array. The OP may simply not no how to "hard-code" a char array, or may not realise that a char array is sufficient for the "problem". This question is obviously a "beginner" question so both of those are easily possibilities.
Masijade -- I don't see how your assumptions contribute to this thread. None of the previous posts result in:
{ "P", "H", "I", "L", "L", "I", "P" }
Masijade -- I don't see how your assumptions contribute to this thread. None of the previous posts result in:
{ "P", "H", "I", "L", "L", "I", "P" }
And you obviously did not read this part of the post
And just because what he posted was a String array neither means that he wants or needs a String array.
Like I said, what he said was "array" not "String array". The only one making assumptions and attempting to lambast others with this "attention to details" high handedness was you.
Edit: And, if it is acceptable, always go for the simplest solution first, which, in this case, is probably toCharArray().
Edit Again: Besides the fact that you can get a String[] using split without that leading blank. It is, after all, regular expression.
split("(?<=.)")
That's an excellent method; do you really think that is suitable for a new developer?
Although you did eliminate the extra null, you still don't capture the capitals.
Attention to detail.
And you obviously did not read this part of the post
That's your assumption. The OP posted an example of a String array.
Feel free to flame elsewhere.
I don't really understand why this thread is so hostile. If you see something that can help the OP, then post. If the you think the other post suck, then post something that doesn't suck. There's really no reason to start calling other people out. I mean, if you want to argue with people login to facebook.
Anyway,
Phil++ I hope at least one of these post have been helpful.
No was flaming until you did. Once again go by what the poster says, until he actually says that he needs a String array and needs it capitalised then you are the one assuming that it does. But to settle your fears
str.toUpperCase().split("(?<=.)")
Not that it was really necessary, since I only posted the modification to the "split" that you had used yourself, so the reposting the toUpperCase was not needed and your mentioning it was pedantic.
Satisfied? No. Because you are incapable of admitting you may have been wrong, either in a technical sense or a social sense.
I'm going with ezzaral and masijade on this ... just because the OP gave as an example
name[] = { "P", "H", "I", "L", "L", "I", "P }
it's fairly possible that his teacher asked him to 'create an array which stores all characters as separate elements'.
If I were new to Java, I might very well have written that as he has.
so: no reason to assume he needs an array of String-objects, and no reason to assume the result has to be all in uppercase.
so, as far as the instruction he has provided us with: the
String name = "Phillip";
char[] arr = name.toCharArray();
as far as I can tell, this may be a test for the OP, whether or not he can go through the api's of String, and find out that there is a method that does what is needed, without writing additional code.
being able to (re-)use what's already there is not less important simply because you can write your own implementation.
P.S. Just so you know, I find nothing wrong with your "solution". What I took issue with was your "high-handedness" passing it out based on assumption. Had you simply said "if that posted example is exactly what the result should look like ..." rather than essentially saying "your all idiots" I would not only have not taken issue with it, I would have supported it. But no, you come here, make an assumption, and then, using that assumption you attempt to browbeat everyone else (some of them very good people) into line with your thinking. Sorry, that doesn't fly. I simply pointed out to you that it was an assumption and that the posted example might not be exactly representative of what the OP really needs and outlined the reasons for that. There wasn't even anything confrontational about the post, but you seemingly took it so.
I'm going with ezzaral and masijade on this ... just because the OP gave as an example
name[] = { "P", "H", "I", "L", "L", "I", "P }
it's fairly possible that his teacher asked him to 'create an array which stores all characters as separate elements'.
If I were new to Java, I might very well have written that as he has.so: no reason to assume he needs an array of String-objects, and no reason to assume the result has to be all in uppercase.
so, as far as the instruction he has provided us with: theString name = "Phillip"; char[] arr = name.toCharArray();
as far as I can tell, this may be a test for the OP, whether or not he can go through the api's of String, and find out that there is a method that does what is needed, without writing additional code.
being able to (re-)use what's already there is not less important simply because you can write your own implementation.
OK, I do not mind if someone flags my posts as a 'bad post' but then at least have the decency to say why.
If this is a 'bad post', obviously you feel I am wrong, so, correct me, tell me where I was wrong so I can learn from it too.
hmmm, my not helicopter view, that's strange, what's going on,
- there is presumsions that these people knows something about ....
- they from their experiencies, im most case ...bla bla bla bla ..
- on any Forum can't be punished for their views, is against democracy, just ... blablabla, pfuuuuj
- in all due respect to some stupid rules, but you are wrong, don't drink, stop with that
beware of some person whom marks some post with, don't drink, stop with that, just strong (not in this case)flamewar can sometimes open the eyes, shows that (always) there are exists more than 3 ways how to do it and better, brrrrr, finally correct way
that nothing about wrong, bad, mistake, there are exists lots of Object and only some methods can give performance ...
again, what's going on, and I want minus flag too, please, please
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.