How can I convert the numbers of ASCII to character by easier way?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Raghad94
    New Member
    • Sep 2013
    • 1

    #1

    How can I convert the numbers of ASCII to character by easier way?

    Hi

    I'm a beginner on Java

    so I hope you guys can help me

    Code:
    int a1 = str.charAt(0)+1;
    int a2 = str.charAt(1)+1;
    int a3 = str.charAt(2)+1;
    int a4 = str.charAt(3)+1;
    
    
    System.out.print("output :");
    System.out.print((char)a1);
    System.out.print((char)a2);
    System.out.print((char)a3);
    System.out.println((char)a4);
    I want to know how to write (char)a1,(char) a2 ... (char)a4 in one System.out.prin t
    also I want to know how can I write (char)a1 outside of the System.out.prin t, I mean
    ???? = (char)a1

    please help :)
  • stdq
    New Member
    • Apr 2013
    • 94

    #2
    Answering your first question, I think you can do this:

    Code:
    System.out.print( (char)a1 + ", " + (char)a2 + ", " + (char)a3 + ", " + (char)a4 )
    Regarding your second question, you want to assign (char)a1 to a char variable?

    Comment

    • Luuk
      Recognized Expert Top Contributor
      • Mar 2012
      • 1043

      #3
      Lines 1..4 can be done like this:
      Code:
      anArray = new int[4];
      for (int x=1; x<=4; x++) {
        int a[x] = str.charAt(x-1)+1;
      }
      this seems longer, but when you do it more than 4 times it gets shorter (than the original way) ;)

      Also your lines 8..11 can be done insize a 'for'-loop.

      Comment

      Working...