• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Need help with an application

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?

 
Bartender
Posts: 1844
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1844
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 867
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Jeremiah
According to Joel given

It may be better.
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic