By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,800 Members | 1,300 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,800 IT Pros & Developers. It's quick & easy.

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

P: 1
Hi

I'm a beginner on Java

so I hope you guys can help me

Expand|Select|Wrap|Line Numbers
  1. int a1 = str.charAt(0)+1;
  2. int a2 = str.charAt(1)+1;
  3. int a3 = str.charAt(2)+1;
  4. int a4 = str.charAt(3)+1;
  5.  
  6.  
  7. System.out.print("output :");
  8. System.out.print((char)a1);
  9. System.out.print((char)a2);
  10. System.out.print((char)a3);
  11. System.out.println((char)a4);
  12.  
I want to know how to write (char)a1,(char)a2 ... (char)a4 in one System.out.print
also I want to know how can I write (char)a1 outside of the System.out.print, I mean
???? = (char)a1

please help :)
Sep 28 '13 #1
Share this Question
Share on Google+
2 Replies


P: 94
Answering your first question, I think you can do this:

Expand|Select|Wrap|Line Numbers
  1. 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?
Sep 28 '13 #2

Expert 100+
P: 1,043
Lines 1..4 can be done like this:
Expand|Select|Wrap|Line Numbers
  1. anArray = new int[4];
  2. for (int x=1; x<=4; x++) {
  3.   int a[x] = str.charAt(x-1)+1;
  4. }
  5.  
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.
Sep 28 '13 #3

Post your reply

Sign in to post your reply or Sign up for a free account.