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?
With Regards,
Anu
Stuart Ash
Ranch Hand
Joined: Oct 07, 2005
Posts: 637
posted
0
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 ]
ASCII silly question, Get a silly ANSI.
Jim Yingst
Wanderer
Sheriff
Joined: Jan 30, 2000
Posts: 18670
posted
0
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?
"I'm not back." - Bill Harding, Twister
Stuart Ash
Ranch Hand
Joined: Oct 07, 2005
Posts: 637
posted
0
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
Joined: Mar 17, 2005
Posts: 146
posted
0
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
Joined: Jan 29, 2003
Posts: 8791
posted
0
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 ]
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Anu satya
Ranch Hand
Joined: Mar 17, 2005
Posts: 146
posted
0
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
Joined: Oct 07, 2005
Posts: 637
posted
0
Ashvini, do you find any lack of clarity in the algorithm I suggested above??
Anu satya
Ranch Hand
Joined: Mar 17, 2005
Posts: 146
posted
0
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
Joined: Oct 03, 2005
Posts: 118
posted
0
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
Joined: Jan 29, 2003
Posts: 8791
posted
0
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
Joined: Mar 17, 2005
Posts: 146
posted
0
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
Joined: Mar 17, 2005
Posts: 146
posted
0
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
Joined: Jan 30, 2000
Posts: 18670
posted
0
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
Joined: Mar 17, 2005
Posts: 146
posted
0
YEs. My program is working now. Thanks to all
santhosh.R gowda
Ranch Hand
Joined: Apr 06, 2009
Posts: 296
posted
0
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
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.