Results 1 to 3 of 3
Like Tree1Likes
  • 1 Post By wsaryada

Thread: string printing converted to numbers while using ".charAt(1)+.chatAt(2)"

  1. #1
    noobplus's Avatar
    noobplus is offline Senior Member
    Join Date
    Feb 2012
    Location
    chennai,south of India(south east of asia)
    Posts
    232
    Rep Power
    3

    Red face string printing converted to numbers while using ".charAt(1)+.chatAt(2)"

    Hi,
    I'm back I was to exams, My project is over.. thanks to the moderators..
    My coding is
    Java Code:
    public class stringing {
    
    	public static void main(String[] args)
    	{
    		String a=new String("bcaefdhigklj"); 
    		for(int i=0;i<a.length();i=i+3)
    		{
    			//System.out.println(a.charAt(i+1)+"\n");//here it works fine
    			System.out.println(a.charAt(i+1)+a.charAt(i+2)+a.charAt(i)+"\n");
                                 //but here it prints numbers
    		}
    	}
    }
    output:
    Java Code:
    294
    303
    312
    321
    regards
    dhilip

  2. #2
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    757
    Rep Power
    8

    Default Re: string printing converted to numbers while using ".charAt(1)+.chatAt(2)"

    When you concatenate chars using the (+) operator the value will be converted into the ASCII code that represent the char. For instance a = 97; b = 98; c = 99, etc. To make it concatenate as a string you need to convert the chars into string. The simplest way is to concatenate a string with it, you can write it like:

    Java Code:
    System.out.println("" + a.charAt(i + 1) + a.charAt( i + 2) + a.charAt(i));
    noobplus likes this.

  3. #3
    noobplus's Avatar
    noobplus is offline Senior Member
    Join Date
    Feb 2012
    Location
    chennai,south of India(south east of asia)
    Posts
    232
    Rep Power
    3

Similar Threads

  1. Replies: 1
    Last Post: 05-07-2012, 05:35 PM
  2. Replies: 14
    Last Post: 04-08-2011, 06:01 PM
  3. string comparison with "=" and ".equal"
    By guavajuice in forum New To Java
    Replies: 9
    Last Post: 04-22-2010, 09:01 PM
  4. Replies: 0
    Last Post: 04-02-2009, 07:02 PM
  5. Replies: 1
    Last Post: 10-20-2008, 07:35 AM

Tags for this Thread

Posting Permissions

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