8 Replies - 22191 Views - Last Post: 20 February 2011 - 01:47 PM Rate Topic: -----

#1 davejava  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 03-February 07

Random Letters

Posted 03 February 2007 - 11:07 AM

for(i=0;i<13;i++)
for(j=0;j<12;j++)
board[i][j] = new Letter();

// need to be .setRandomletter

board[12][0].Randomchar();
board[12][1].Randomchar();

public String Randomchar() {
int character = (int)(Math.random()*26);
String s = alphabet.substring(character, character+1);
return s;

I want to print out random letters on the last collum in the grid of 13x12. (13 going from left to right, 12 going down) board[12][0] represents the bottom left corner of the grid where a random letter will be printed. .Randomchar(); is the method im calling from a different class (Letter class) I can simply put in board[12][0].setCharacter (method from letter class) .equals('Z'); and I will get a Z in that position on the grid. Rather I want a random letter. Thanks for the help.

Is This A Good Question/Topic? 0
  • +

Replies To: Random Letters

#2 William_Wilson  Icon User is offline

  • lost in compilation
  • member icon

Reputation: 205
  • View blog
  • Posts: 4,807
  • Joined: 23-December 05

Re: Random Letters

Posted 03 February 2007 - 11:22 AM

you can create random numbers from 65='A' to 90='Z' then cast them as char and you will get the ascii value.
Was This Post Helpful? 0
  • +
  • -

#3 davejava  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 03-February 07

Re: Random Letters

Posted 03 February 2007 - 11:43 AM

View PostWilliam_Wilson, on 3 Feb, 2007 - 11:22 AM, said:

you can create random numbers from 65='A' to 90='Z' then cast them as char and you will get the ascii value.


public int Randomchar() {
rancharacter = 65 + (int) (Math.random()*90);
return (char)rancharacter;
}
thats my method in my letter class.

board[12][0].Randomchar();

I call it in a grid class, but I'm not getting any random letters at that position (bottom left corner of the grid). The grid is right, but I dont see how to assign random letters in the method. Is this closer? Thank you.
Was This Post Helpful? 0
  • +
  • -

#4 William_Wilson  Icon User is offline

  • lost in compilation
  • member icon

Reputation: 205
  • View blog
  • Posts: 4,807
  • Joined: 23-December 05

Re: Random Letters

Posted 03 February 2007 - 12:11 PM

your Randomchar() method creates and returns a character, but you haven't set anything. Without a better explanation of how your program displays the grid, or seeing the code, it is hard to know what you are doing wrong.
Was This Post Helpful? 0
  • +
  • -

#5 davejava  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 03-February 07

Re: Random Letters

Posted 03 February 2007 - 12:38 PM

View PostWilliam_Wilson, on 3 Feb, 2007 - 12:11 PM, said:

your Randomchar() method creates and returns a character, but you haven't set anything. Without a better explanation of how your program displays the grid, or seeing the code, it is hard to know what you are doing wrong.

There is a printboard method that gets the method from the letter class and prints out the board. The board looks like
* * * * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * * (I want this whole row to be random letters. I have board[12][0].Randomchar to be the first * in this row, but I want it to be a random letter. Here is the grid:
public void printBoard(){
for(int i=0;i<13;i++)
{
for(int j=0;j<12;j++)
{
board[i][j].printvalue();
System.out.print(" ");
}
System.out.println();
}
}
So.. I want all these to call that method in Letter class and get random letters that are places on the last row in the grid. Does this make more sense now?
board[12][0].Randomchar();
board[12][1].Randomchar();
board[12][2].Randomchar();
board[12][2].Randomchar();
board[12][3].Randomchar();
board[12][4].Randomchar();
board[12][5].Randomchar();
board[12][6].Randomchar();
board[12][7].Randomchar();
board[12][8].Randomchar();
board[12][9].Randomchar();
board[12][10].Randomchar();
board[12][11].Randomchar();
Was This Post Helpful? 0
  • +
  • -

#6 sanmano  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 20-February 11

Re: Random Letters

Posted 20 February 2011 - 10:32 AM

Hi..
This code will generate a random character between 'A' and 'Z'... Have a happy day. :bigsmile:


int ranChar=0;
ranChar = 65 + (int)(Math.random()*26);
return (char)ranChar;
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Random Letters

Posted 20 February 2011 - 01:40 PM

A lot easier (an clearer) that way

Just get a random number bettween 0 and 26
add 'A' to it

Random ran = new Random();
char[] letter = new char[100];
for(int i = 0; i < letter.length; ++i)
   letter[i] = (char) ran.nextInt(26) + 'A';



Was This Post Helpful? 0
  • +
  • -

#8 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2870
  • View blog
  • Posts: 11,025
  • Joined: 15-July 08

Re: Random Letters

Posted 20 February 2011 - 01:43 PM

Just to let everybody know...this topic was bumped from back in 2007. :)
Was This Post Helpful? 1
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Random Letters

Posted 20 February 2011 - 01:47 PM

View PostDogstopper, on 20 February 2011 - 03:43 PM, said:

Just to let everybody know...this topic was bumped from back in 2007. :)

Sorry, the third times it happens to me this week.
Usually I am more vigilant :)
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1