• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to print ASCII values of all characters?

 
Anu satya
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all,

I have one assignment to print ASCII values of all characters using java.
i.e.Console application.

Can anyone Please guide me in this regard?
 
Stuart Ash
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Loop through the characters in a FOR loop and print out the value of the char which is cast to int.

Try this.

I am pasting the answer below, but suggest you first try it based on the above explanation, without looking at the code below.

[deleted - Jim]
[ November 17, 2005: Message edited by: Jim Yingst ]
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ahem. At this site we really prefer not to simply give people the answer. So I deleted it from the previous post.

Ashvini: some questions for you to consider:

What is the largest (and smallest) possible value of a char variable in Java?

What is the largest (and smallest) possible ASCII value?

If a char is a type of integer value, what's a way to convert from one type of number to another? For example, how could you convert a double to an int?
 
Stuart Ash
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jim Yingst:
Ahem. At this site we really prefer not to simply give people the answer. So I deleted it from the previous post.



Alright Jim. I was in double mind too, but decided to leave it to the asker the restraint of not looking at the answer. But if this is the preferred approach...
 
Anu satya
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jim,
I really don't know answer for both the questions....I will write the code. Please guide me with the logic and the ASCII concept.



 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Help us understand where you are ...

Are you comfortable with making a one-class program, main() method and getting it running?

Are you comfortable with for loops, say incrementing an int from 0 to some limit?

Are you comfortable with the relationship between int and char?

Do you have a good book or reference to help with things like this? Thinking In Java is a good free online book.
[ November 21, 2005: Message edited by: Stan James ]
 
Anu satya
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
James,
I am an intermediate programmer in java.
Yes, i am comfertable with single class and running it.
I am capable of doing complex loops
I know the convertion of ASCII code to String:


I know the convertion of integer to ASCII code (byte)


Anticipating some guidelines at the earliest....Thanks in advance
 
Stuart Ash
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ashvini, do you find any lack of clarity in the algorithm I suggested above??
 
Anu satya
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ash,

you wrote:
"Loop through the characters in a FOR loop and print out the value of the char which is cast to int."

But,
I am not understanding two concepts:
1) How to input characters,for which ASCII values has to be displayed ?
2) What should be the minimum and maximum limits for the loop?
 
Sri Ram
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ashvini Devi:
Ash,

But,
I am not understanding two concepts:
1) How to input characters,for which ASCII values has to be displayed ?
2) What should be the minimum and maximum limits for the loop?


That lies in Stans post. If u are able to answer both Stans Q, then u have the program. Thats a clue.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think we're homing in on what's missing. Google on 'ASCII character set' to find the range of numbers. Note that quite a few of them do special things when printed, eg tab or newline or ring the bell on the printer (your printer doesn't have a bell?) so it can be a little tricky to print a nice grid of all the characters. Consider a map or something to look up the descriptive names of these special characters.
 
Anu satya
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am trying to write the program.
I am asking user to strike a key on the keyboard. With respect to that, ASCII value has to be printed.
I am first converting the string to int and then int to ASCII value.

But:
my problem is:
1)I am getting exception as String "a" cano't be converted in to int.
2)how to convert string to Char?
3)How to findout, whether the user input is a number or a character ?

Please help! i am posting my code here.


 
Anu satya
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Otherwise,
as Ash said, i don't know how to loop through all the characters to print at a stretch. i want the ASCII values from 32 to 127... (need not be all characters)

Thanks in advance.

:roll:
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you write a loop with a variable which starts at 0 and ends at 10?

Can you write a loop with a variable which starts at 32 and ends at 127?
 
Anu satya
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
YEs.
My program is working now.
Thanks to all
 
santhosh.R gowda
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ashvini Devi wrote:Hello all,

I have one assignment to print ASCII values of all characters using java.
i.e.Console application.

Can anyone Please guide me in this regard?


-----------------------------------------------------------------------------------------------------
hi Ashvini

[Deleted]
 
Maneesh Godbole
Saloon Keeper
Posts: 11196
15
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Santosh,

Do you really think the OP is waiting these many years for the solution? (..and if you have read the OP's last post, she did say she got it).
 
Henry Wong
author
Marshal
Pie
Posts: 21515
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Also ... it is nicer to help someone solve their homework assigment instead of just giving them the solution. (You can't learn if you just get the solution).... I deleted the solution, just in case, another student from the same instructor needs to do the same assigment.

Henry
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic