Results 1 to 17 of 17
- 06-22-2011, 02:17 AM #1
Senior Member
- Join Date
- Nov 2010
- Posts
- 150
- Rep Power
- 3
prints characters in one case, but in another it returns integers. Why is that?
Hell again,
so here is the new one. The Java Tutorial that I am following says the following:
"Write a program that computes your initials from your full name and displays them."
so I wrote the following, making up a name for it and using examples previously encountered in the tutorial:
When I use the following, I get this output. So I get a number of 156 instead of RJ:Java Code:public class MyInitials { public static void main(String[] args){ String myName = "Roger Justice"; int gap = myName.indexOf(' '); System.out.println(gap); System.out.println(myName.charAt(0)); System.out.print((myName.charAt(0) + (myName.charAt(myName.indexOf(' ')+1)))); } }
(oh, feel free to ignore the first System.out.println. I was just tinkering with things.
However if split the output over two lines, I get the desired outcome:Java Code:----jGRASP exec: java MyInitials 5 R 156 ----jGRASP: operation complete.
CODE:
I get this output, the desired one:Java Code:public class MyInitials { public static void main(String[] args){ String myName = "Roger Justice"; int gap = myName.indexOf(' '); System.out.println(gap); System.out.println(myName.charAt(0)); System.out.print((myName.charAt(0))); System.out.println((myName.charAt(myName.indexOf(' ')+1))); } }
I don't understand why this happens.Java Code:----jGRASP exec: java MyInitials 5 R RJ ----jGRASP: operation complete.
- 06-22-2011, 02:25 AM #2
ASCII value for R is 82, for J is 74. 82 + 74 = 156.
You get that result because you are using addition. Since chars are represented by integer values under the hood it applies integer addition instead of concatenation like Strings. So to achieve your desired ouput try using different print statements for each initial or use a StringBuilder to create a new String.
- 06-22-2011, 02:30 AM #3
Senior Member
- Join Date
- Nov 2010
- Posts
- 150
- Rep Power
- 3
- 06-22-2011, 02:33 AM #4
Senior Member
- Join Date
- Nov 2010
- Posts
- 150
- Rep Power
- 3
So here is a different version:
This is adapted from the answer provided in the tutorial. My question is how was I supposed to know that? Am I supposed to look through the entire Java API? It's not searchable is it? Is that a normal habit to develop?Java Code:public class MyInitials2 { public static void main(String[] args) { String myName = "Roger Justice"; StringBuffer myInitials = new StringBuffer(); int length = myName.length(); for (int i = 0; i<length; i++) { if (Character.isUpperCase(myName.charAt(i))) { myInitials.append(myName.charAt(i)); } } System.out.println("My initials are: "+ myInitials); } }
- 06-22-2011, 02:44 AM #5
As a n00b your solution would have been what I would have done. Only as you become more experienced will you learn about other classes such as StringBuffer or as I suggested StringBuilder which is basically the same class. The only difference is that one is synchronised and one isn't.
- 06-22-2011, 02:49 AM #6
Just a nitpick. The sugggested solution will not work if the name was "roger justice". Using charAt is a bit more robust. Also you can use the split method instead of using indexOf to find the space. This method is even better as it can handle multiple names such as "James Robert David Smith".
- 06-22-2011, 05:28 AM #7
Senior Member
- Join Date
- Nov 2010
- Posts
- 150
- Rep Power
- 3
- 06-22-2011, 05:37 AM #8
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
He meant that that method only works on capital first letters, it would be much better to print the first letter regardless.
Check out the split API, it takes a regex and splits a string into an array of strings, so the string "bob mike Johnny" would split into an array where each element contains a name. you can then loop through the array getting each character with charAt, or substring. This approach would work on an infinite amount of words in the name and would continue working regardless of case of letters.
- 06-22-2011, 06:09 AM #9
Senior Member
- Join Date
- Nov 2010
- Posts
- 150
- Rep Power
- 3
I can't seem to get it to work. I tried:
System.out.println(split(string)); and I am getting a cannot find symbol error on the split.
- 06-22-2011, 06:10 AM #10
Senior Member
- Join Date
- Nov 2010
- Posts
- 150
- Rep Power
- 3
Oh and I have not yet covered regular expressions...I had to google regex.
- 06-22-2011, 06:15 AM #11
The split method is in the String class so you need to call it on an instance of String. It returns a String array so you cannot just print it (well you can but you won't get anything meaningful displayed). Try assigning it to a variable.
That will give you an array with "one" as the first element, "two" as the second and "three" as the third.Java Code:String text = "one two three"; String[] words = text.split(" "); //split on a space
- 06-22-2011, 06:16 AM #12
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Don't let regex scare you, for split you can generally use very general versions, split on words or spaces by simply using a space or character. Split is a method of the string class, you call it on an instance of a string with a regex as an argument.
" ", "abc", "[a-z]" are all valid regexes, the first matches to any spaces, the second splits on the word abc, and the third splits on characters from a to z.
Java Code:String a = "ex am pl es"; String[] ex = a.split(" "); for(int i = 0; i < ex.length; ++i){ System.out.print(ex[i]); }
- 06-22-2011, 06:16 AM #13
Senior Member
- Join Date
- Nov 2010
- Posts
- 150
- Rep Power
- 3
Thank you....and thank you for improving the world...one idiot at a time!
ETA:This was in response to junky!Last edited by bigsonny; 06-22-2011 at 06:18 AM. Reason: clarification
- 06-22-2011, 06:18 AM #14
Senior Member
- Join Date
- Nov 2010
- Posts
- 150
- Rep Power
- 3
- 06-22-2011, 06:25 AM #15
OT
Does anyone else see only the top left hand quadrant for some of the smilies?
- 06-22-2011, 06:56 AM #16
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
I see the full smilies for all the ones you just posted.
- 06-22-2011, 07:14 AM #17
Similar Threads
-
Txt to ArrayList only prints []
By Quasi Urbane in forum New To JavaReplies: 4Last Post: 04-27-2011, 07:16 PM -
Canvas Prints
By canvas02 in forum Reviews / AdvertisingReplies: 0Last Post: 02-18-2011, 05:33 PM -
switch case with integers and strings...
By JavaComplient in forum New To JavaReplies: 2Last Post: 10-21-2010, 06:52 AM -
Convert integers into ACSII characters.
By Valkyrie in forum New To JavaReplies: 4Last Post: 11-24-2009, 02:39 AM -
spl. case-an mp3 returns getDuration() of -1, other players play it perfectly
By arnab321 in forum CLDC and MIDPReplies: 1Last Post: 12-11-2008, 08:50 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks