This site requires JavaScript, please enable it in your browser!
Greenfoot back
manster2008
manster2008 wrote ...

2011/7/29

Auto-Indent & toCharArray() ???

manster2008 manster2008

2011/7/29

#
First of all, when reading another question once, I remember seeing a post about a shortcut to automatically indent all lines as needed in Greenfoot. Could someone please tell me what it is? Next, in an older and very long thread, I asked a question about separating characters of a string into an array or something... mjrb4 suggested to start a new thread and he told me about a method called toCharArray(). I would be grateful if someone could elaborate a bit on this. Are there any parameters? Does it return an array? etc. Thanks...
mjrb4 mjrb4

2011/7/29

#
Hey, First off, the easy part - Ctrl+shift+i is what you're after I think! In terms of the toCharArrayMethod: http://download.oracle.com/javase/6/docs/api/java/lang/String.html#toCharArray() It literally just works on a string object, takes no parameters and returns a character array of all the characters in the string. So:
char[] arr = "Hello".toCharArray();
...would give you a char array called arr containing 'H', 'e', 'l', 'l', 'o'.
manster2008 manster2008

2011/7/30

#
Thanks, I now have a char array of a very long string, in a "for loop", the chars are added to a string in a string array one by one, until the advance is too much, then a new string in the string array is created, and so forth, until all the chars are finished. This works, but I realized I have to check by words, because I can't have "yo" in the first line and "u" in the second line from the word "you". I guess I can figure this out myself. All I want to know is how I can figure out if a char is equal to space?
danpost danpost

2011/7/30

#
I do not know if this is the best way, but:
boolean isSpace = (" ".indexOf(myChar) == 0);
should suffice with myChar being the character you are checking.
manster2008 manster2008

2011/7/30

#
Thanks, danpost, it works, my string is now seperated into an array of words.
davmac davmac

2011/7/30

#
Much simpler would be:
boolean isSpace = myChar == ' ';
That is, to find out if a character is a space, you just - check if it is a space :) You may have been confused because it's not correct to compare strings with '=='. However, it is fine for individual characters (of type 'char').
manster2008 manster2008

2011/7/30

#
Okay, I get what the problem was before... I had been doing myChar == " ". But it is actually myChar = ' '. with one mark on each side instead of two. Thanks. *Edit. Thanks for responses... I was able to make what I wanted...
mjrb4 mjrb4

2011/7/31

#
Okay, I get what the problem was before... I had been doing myChar == " ". But it is actually myChar = ' '. with one mark on each side instead of two. Thanks.
Yup - the double quotes always represent a string literal, for single quotes it's a character literal.
danpost danpost

2011/7/31

#
davmac wrote...
Much simpler would be:
boolean isSpace = myChar == ' ';
That is, to find out if a character is a space, you just - check if it is a space :) You may have been confused because it's not correct to compare strings with '=='. However, it is fine for individual characters (of type 'char').
Thanks, davmac. Though, not confused, just lacking knowledge in working with characters in Java. Was unaware of the use of single quotes for charactes prior to your posting. Thanks again!
You need to login to post a reply.