Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to create a matrix of the alphabet using a 2 dime-array? 2

Status
Not open for further replies.

hbutt

Programmer
Apr 15, 2003
35
GB
hi there,
i need some help in generating a type of matrix, of the alphabet, using a 2 dimensional array.
example:

ABCDEFGH
BCDEFGHI
CDEFGHIJ
DEFGHIJK

i need it to create a full square for all the alphabet. any help would be much appreciated.

thanx
hgsb
 
class matrix
{
public static void main(String args[])
{
final int MYARRAYSIZE = 8;
char myArray[][] = new char[MYARRAYSIZE][MYARRAYSIZE];
//int i = 'A';
//System.out.println(i);
for (int y = 65; y< 65 + MYARRAYSIZE; y++)
for (int x = 0; x<MYARRAYSIZE; x++)
{
myArray[y-65][x] = (char)(y + x);
}
for (int y=0; y<MYARRAYSIZE; y++)
{
for (int x=0; x<MYARRAYSIZE; x++)
System.out.print(myArray[y][x]);
System.out.println();
}
//Please mark this post as helpful if the code is suitable for you
 
hi,
i needed it so the matrix went from A-Z i.e.

ABCDEFGHIJKLMNOPQRSTUVWXYZ
BCDEFGHIJKLMNOPQRSTUVWXYZA
CDEFGHIJKLMNOPQRSTUVWXYZAB
DEFGHIJKLMNOPQRSTUVWXYZABC
EFGHIJKLMNOPQRSTUVWXYZABCD
FGHIJKLMNOPQRSTUVWXYZABCDE
GHIJKLMNOPQRSTUVWXYZABCDEF
HIJKLMNOPQRSTUVWXYZABCDEFG

IS IT POSSIBLE TO DO THIS
THANX
HGSB



 
hi,
the problem is that it ends up doing the following after i change the array size to 26:

ABCDEFGHIJKLMNOPQRSTUVWXYZ
BCDEFGHIJKLMNOPQRSTUVWXYZ[
CDEFGHIJKLMNOPQRSTUVWXYZ[DEFGHIJKLMNOPQRSTUVWXYZ[\]
EFGHIJKLMNOPQRSTUVWXYZ[\]^
FGHIJKLMNOPQRSTUVWXYZ[\]^_
GHIJKLMNOPQRSTUVWXYZ[\]^_`
HIJKLMNOPQRSTUVWXYZ[\]^_`a
IJKLMNOPQRSTUVWXYZ[\]^_`ab
JKLMNOPQRSTUVWXYZ[\]^_`abc
KLMNOPQRSTUVWXYZ[\]^_`abcd
LMNOPQRSTUVWXYZ[\]^_`abcde
MNOPQRSTUVWXYZ[\]^_`abcdef
NOPQRSTUVWXYZ[\]^_`abcdefg
OPQRSTUVWXYZ[\]^_`abcdefgh
PQRSTUVWXYZ[\]^_`abcdefghi
QRSTUVWXYZ[\]^_`abcdefghij
RSTUVWXYZ[\]^_`abcdefghijk
STUVWXYZ[\]^_`abcdefghijkl
TUVWXYZ[\]^_`abcdefghijklm
UVWXYZ[\]^_`abcdefghijklmn
VWXYZ[\]^_`abcdefghijklmno
WXYZ[\]^_`abcdefghijklmnop
XYZ[\]^_`abcdefghijklmnopq
YZ[\]^_`abcdefghijklmnopqr
Z[\]^_`abcdefghijklmnopqrs

instead of generating an upper case alphabet array.
thanx anyway
hgsb
 
Sorry, you are correct. The following code will fix it. More specific : the line &quot;z = z - 26;&quot;
Code:
public class AlfabetMatrix {

  public AlfabetMatrix() {
  }

  public static void main(String[] args) {
     final int MYARRAYSIZE = 26;
     int z;
     char myArray[][] = new char[MYARRAYSIZE][MYARRAYSIZE];
     for (int y = 65; y< 65 + MYARRAYSIZE; y++)
         for (int x = 0; x<MYARRAYSIZE; x++)
             {
              z = y + x;
              if (z >= 65+26)
                z=z-26;
              myArray[y-65][x] = (char)(z);

             }
     for (int y=0; y<MYARRAYSIZE; y++)
         {
         for (int x=0; x<MYARRAYSIZE; x++)
             System.out.print(myArray[y][x]);
         System.out.println();
         }
  }
}
 
hi there, need some more help i've been trying to pick out letters from the alphabet matrix according to a random string entered by the user. ive been trying to look for the first letter from the user entered string, according to the line in the matrix from which letter starts and find a corresponding letter. e.g. string equals &quot;HELLO&quot; so i check down the first column from where &quot;H&quot; starts and find a letter in a graph type method. see below

ABCDEFGH
BCDEFGHA
CDEFGHAB
DEFGHABC
EFGHABCD
FGHABCDE
GHABCDEF
->HABCDEFG

H = starting point along X and Y = C (on top row) so the corresponding letter will = B.

any help would be much appreciated.
thanx
hgsb
 
sorry i meant &quot;X and Y&quot; in terms of the axis.
 
You should type the complete result of run the program.
e.g. what is the wanted result of java AlfabetMatrix Hello ?
 
i think i'd better tell u wot i have been trying to do for the past feww weeks. first i needed to generate a alfabet matrix then i needed to take a randomly entered phrase or sentence and encode it using a key i.e. a word e.g. black. the phrase could be hello world and the encoded text would = hllpny or woteva it is.
 
hi there, i really need some help now. as i need to now generate an alfabet matrix according to a dynamic string, e.g.
dynamic string = BLACK

matrix =

ABCDEFGHIJKLMNOPQRSTUVWXYZ
BCDEFGHIJKLMNOPQRSTUVWXYZA
LMNOPQRSTUVWXYZABCDEFGHIJK
ABCDEFGHIJKLMNOPQRSTUVWXYZ
CDEFGHIJKLMNOPQRSTUVWXYZAB
KLMNOPQRSTUVWXYZABCDEFGHIJ

COULD NE 1 HELP ME PLEASE?
HGSB
 
as u can seen along the left handside the string letters each start a line in the square.

B.
L..
A.
C..
K..

i know that the string will probably hav to be put into an array but after that i am lost

any help is much appreciated
thanx
hgsb
 
public class Alfabet
{
public Alfabet() { }

public static void main(String[] args)
{
final int MYARRAYSIZE = 26;
int z;
char myArray[][] = new char[MYARRAYSIZE][MYARRAYSIZE];
char tempChar=0;
for (int y = 65; y< 65 + MYARRAYSIZE; y++)
for (int x = 0; x<MYARRAYSIZE; x++)
{
z = y + x;
if (z >= 65+26)
z=z-26;
myArray[y-65][x] = (char)(z);

}
for (int y=0; y<MYARRAYSIZE; y++)
{
for (int x=0; x<MYARRAYSIZE; x++)
System.out.print(myArray[y][x]);
System.out.println();
}
System.out.println();
System.out.println(&quot;Result:&quot;);

for (int whichWord = 0; whichWord<args.length; whichWord++)
for (int whichChar = 0; whichChar<args[whichWord].length(); whichChar++)
{
tempChar = Character.toUpperCase( args[whichWord].charAt(whichChar) );
if ( tempChar<65 || tempChar>90 )
continue;
else
System.out.println(myArray[tempChar-65]);
}
}
}
 
hi there, jus wanted to know what the following code u sent is used for;

for (int whichWord = 0; whichWord<args.length; whichWord++)
for (int whichChar = 0; whichChar<args[whichWord].length(); whichChar++)
{
tempChar = Character.toUpperCase( args[whichWord].charAt(whichChar) );
if ( tempChar<65 || tempChar>90 )
continue;
else
System.out.println(myArray[tempChar-65]);
}
}

thanx
hgsb
 
After you has typed
java Alfabet black cat, args[0] will store &quot;black&quot; and args[1] will store &quot;cat&quot;.
loop through args[0] and args[1], toUpperCase will change lower case letter to upper case. charAt will pick one letter each time.
Ascii code of &quot;A&quot; is 65 and ascii code of &quot;Z&quot; is 90, so tempChar-65 will find the position of array of each letter.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top