Results 1 to 17 of 17
  1. #1
    bigsonny is offline Senior Member
    Join Date
    Nov 2010
    Posts
    164
    Rep Power
    4

    Default 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:

    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))));
    	}
    }
    When I use the following, I get this output. So I get a number of 156 instead of RJ:
    (oh, feel free to ignore the first System.out.println. I was just tinkering with things.

    Java Code:
     ----jGRASP exec: java MyInitials
    
    5
    R
    156
     ----jGRASP: operation complete.
    However if split the output over two lines, I get the desired outcome:
    CODE:

    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 get this output, the desired one:
    Java Code:
     ----jGRASP exec: java MyInitials
    
    5
    R
    RJ
    
     ----jGRASP: operation complete.
    I don't understand why this happens.

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,789
    Rep Power
    7

    Default

    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.

  3. #3
    bigsonny is offline Senior Member
    Join Date
    Nov 2010
    Posts
    164
    Rep Power
    4

    Default

    Quote Originally Posted by Junky View Post
    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.
    Got it. Thank you!

  4. #4
    bigsonny is offline Senior Member
    Join Date
    Nov 2010
    Posts
    164
    Rep Power
    4

    Default

    So here is a different version:

    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);
    	}
    }
    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?

  5. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,789
    Rep Power
    7

    Default

    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.

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,789
    Rep Power
    7

    Default

    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".

  7. #7
    bigsonny is offline Senior Member
    Join Date
    Nov 2010
    Posts
    164
    Rep Power
    4

    Default

    Quote Originally Posted by Junky View Post
    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".
    It worked. It returned RJ. Am I missing something? What are you called the suggested solution?

  8. #8
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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.

  9. #9
    bigsonny is offline Senior Member
    Join Date
    Nov 2010
    Posts
    164
    Rep Power
    4

    Default

    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.

  10. #10
    bigsonny is offline Senior Member
    Join Date
    Nov 2010
    Posts
    164
    Rep Power
    4

    Default

    Oh and I have not yet covered regular expressions...I had to google regex.

  11. #11
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,789
    Rep Power
    7

    Default

    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.
    Java Code:
    String text = "one two three";
    String[] words = text.split(" "); //split on a space
    That will give you an array with "one" as the first element, "two" as the second and "three" as the third.

  12. #12
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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]);
    }

  13. #13
    bigsonny is offline Senior Member
    Join Date
    Nov 2010
    Posts
    164
    Rep Power
    4

    Default

    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

  14. #14
    bigsonny is offline Senior Member
    Join Date
    Nov 2010
    Posts
    164
    Rep Power
    4

    Default

    Quote Originally Posted by sunde887 View Post
    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]);
    }
    Oh I get it now. This is right now. Thanks.!

  15. #15
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,789
    Rep Power
    7

    Default

    OT

    Does anyone else see only the top left hand quadrant for some of the smilies?


  16. #16
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    I see the full smilies for all the ones you just posted.

  17. #17
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,789
    Rep Power
    7

    Default

    I meant in the reply box where you can select them. the ones in the above post was so that I could see them.

Similar Threads

  1. Txt to ArrayList only prints []
    By Quasi Urbane in forum New To Java
    Replies: 4
    Last Post: 04-27-2011, 07:16 PM
  2. Canvas Prints
    By canvas02 in forum Reviews / Advertising
    Replies: 0
    Last Post: 02-18-2011, 05:33 PM
  3. switch case with integers and strings...
    By JavaComplient in forum New To Java
    Replies: 2
    Last Post: 10-21-2010, 06:52 AM
  4. Convert integers into ACSII characters.
    By Valkyrie in forum New To Java
    Replies: 4
    Last Post: 11-24-2009, 02:39 AM
  5. Replies: 1
    Last Post: 12-11-2008, 08:50 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •