Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Need help with an application

 
Jere Johnson
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
This java application will fill an array with randomly chosen uppercase letters of the alphabet. Program will write letters
to a file and display them. I cant seem to get the characters to show on the black output screen. I get weird little icons instead of characters. Am i doing something wrong?

 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your line of code:
character = (char)(Math.random()*26);
should read:
character = (char)(Math.random()*26) + 65;

Reason:
ASCII capital characters start with 65 ('A' = 65, not 'A' = 0) What you were displaying on your screen were mostly NPCs --> 0 through CTRL-Y
 
Jere Johnson
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joel,
I get an possible loss of precision error
when i put + 65 at the end of the statement
like this:
character = (char)(Math.random()*26)+65;
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oops. Try:
character = (char)(Math.random()*26 + 65);
You have to cast the whole expression to a char, not just the Math.random()*26.
 
Francis Siu
Ranch Hand
Posts: 867
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Jeremiah
According to Joel given

It may be better.
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You also may wish to use char literals to make your intent more clear:

In fact, you can also generalize this to create a handy helper method:

The implementation is left as the proverbial exercise for the reader. Such a function could be easily used for generating a random char as well. Or you can write an overloaded version for char.
Layne
[ April 29, 2003: Message edited by: Layne Lund ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic