• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

converting ascii number to capital letter

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I generated a random number using currentTimeMillis and I want to convert this to a capital letter equal to the value of the random number. Can someone help me figure this out ?
Thanks
 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why would a random number correspond to a capital letter?


The code above casts the int value 65 to a char, which converts it to an 'A', since 65 is the ASCII value for the 'A'. currentTimeMillis returns a large number (of milliseconds since the epoch) which is not going to correspond to any capital letter

See here for info on the ASCII to character mapping.
 
Ranch Hand
Posts: 161
Firefox Browser Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let's try it...
currentTimeMillis probably returns an int. Convert int to Integer, then grab the string from the Integer. Once you have the string, look at the API from String and see if any of those can help you.

Hope this helps,
Gary
 
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ken Mullins wrote:I want to convert this to a capital letter equal to the value of the random number.



Hello. Can you clarify the statement above? For instance, what capital letter is equal to 2302?

Also, what have you tried so far? Can you post your code?

Best of luck!

Oh, and welcome to the Ranch!
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ken and welcome to the JavaRanch.

System.currentTimeMillis() returns a long and that has a much larger range then letters. So you'll need to reduce the range.
Look at the Character class's methods. You'll find a useful method.
 
Ken Mullins
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

pete stein wrote:

Ken Mullins wrote:I want to convert this to a capital letter equal to the value of the random number.



Hello. Can you clarify the statement above? For instance, what capital letter is equal to 2302?

Also, what have you tried so far? Can you post your code?

Best of luck!

Oh, and welcome to the Ranch!



I have converetd the number returned by using current time in milliSeconds and have generated 2 2 digit number. I would like to convert this number to a capital letter.
 
Ken Mullins
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ken Mullins wrote:

pete stein wrote:

Ken Mullins wrote:I want to convert this to a capital letter equal to the value of the random number.



Hello. Can you clarify the statement above? For instance, what capital letter is equal to 2302?

Also, what have you tried so far? Can you post your code?

Best of luck!

Oh, and welcome to the Ranch!



I have converetd the number returned by using current time in milliSeconds and have generated 2 2 digit number. I would like to convert this number to a capital letter.


public class RandomCharacter
public static void main(String[] args)
long totalMilliSeconds = System.currentTimeMillis();
long totalSeconds = totalMilliSeconds / 1000;
long currentSecond = (int)(totalSe3conds % 60);
double asciiNew = currentSecond;
System.out.print(asciiNew);

This generates a 2 digit number that I want to be cpnverted to a capital letter

 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ken Mullins wrote:
I have converetd the number returned by using current time in milliSeconds and have generated 2 2 digit number. I would like to convert this number to a capital letter.



You still will want to describe to us in one or several sentences just how you intend to convert a number to a letter -- not the code for this, but rather the logic for this -- as there is no one standard way to do this. Your post is still not clear on this point. Or perhaps it would help us if you told us just what you were going to do with the capital letter once obtained. In other words, what is the ultimate goal of this exercise?

Best of luck!
 
Ken Mullins
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

pete stein wrote:

Ken Mullins wrote:
I have converetd the number returned by using current time in milliSeconds and have generated 2 2 digit number. I would like to convert this number to a capital letter.



You still will want to describe to us in one or several sentences just how you intend to convert a number to a letter. Your post is still not clear on this point.

Best of luck!

The code generates a random 2 digit number. I want to convert this number using the Ascii or unicode to the corresponding letter. Am I going down the wrong road here ? I am new at this but I am enjoying it.

Thanks for your help !
 
Ken Mullins
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

pete stein wrote:

Ken Mullins wrote:
I have converetd the number returned by using current time in milliSeconds and have generated 2 2 digit number. I would like to convert this number to a capital letter.



You still will want to describe to us in one or several sentences just how you intend to convert a number to a letter -- not the code for this, but rather the logic for this -- as there is no one standard way to do this. Your post is still not clear on this point. Or perhaps it would help us if you told us just what you were going to do with the capital letter once obtained. In other words, what is the ultimate goal of this exercise?

Best of luck!

The goal here is simply to display the letter.
 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The problem is not how to do it, but how YOU want to do it. if you are generating a random 2-digit number, you're getting values from 00-99. which of those numbers should be displayed as what capital letter?

suppose you get a random number of 12 - what capital letter should it be?

If you want to use ASCII, you would need to generate numbers between 65 and 90 only. 12 is the ASCII code for a Form Feed - which is not any capital letter.
 
Ken Mullins
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

fred rosenberger wrote:The problem is not how to do it, but how YOU want to do it. if you are generating a random 2-digit number, you're getting values from 00-99. which of those numbers should be displayed as what capital letter?

suppose you get a random number of 12 - what capital letter should it be?

If you want to use ASCII, you would need to generate numbers between 65 and 90 only. 12 is the ASCII code for a Form Feed - which is not any capital letter.

I did not think about that. So, I guess I would need to generate a random numer within a range of 65 to 90 and then display that number. Can I designate that this letter be a capital ?
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ken Mullins wrote:
I did not think about that. So, I guess I would need to generate a random numer within a range of 65 to 90 and then display that number. Can I designate that this letter be a capital ?



Now this is do-able. You can convert an int to a char simply by casting it:

 
fred rosenberger
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if you look at the ascii table, you will see that by definition, 65-90 IS a capital letter.

Lower case is defined as 97-122.

To get a range of 65-90, you need to figure out two things - a range and an offset. for example, if i wanted the values 6-10, that is a range of 5 possible values (6,7,8,9 and 10). So, I really need to generate a random number from 0-4, and then 'translate' them to 6-10. That's the offset. 0 maps to 6, 1 maps to 7, etc. each values needs to be 'offset' by 6. So, if i generate values from 0-4, and then add 6 to each, i'll have what I want.

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic