hello,
i want to write a program that store subjects and days in two enumerations and their values in rows and coloumn using 2D array. how can i do this ?

see the image below:

http://img718.imageshack.us/img718/3967/sufyan.jpg

So far i've done this,

class EnumProg {
  enum Days {Monday, Tuesday, Wednesday, Thursday, Friday}
  enum Periods {Period1, Period2, Period3, Period4, Period5}
  public static void main (String [] args)
  {
   String [][] Timetable2A;
   Timetable2A = new String [Days.Monday.ordinal()][Periods.Period1.ordinal()];
  }
}

i really dont know how to use enumeration and arrays together and how to store values in arrays..i don't have any example program thorugh which i can learn this.

Can anyone give me the idea to do this ?

Recommended Answers

All 7 Replies

You can use the following logic to get the desired result.
Just format the print according to your requirement...

for(Days day : Days.values()) {
    System.out.print(day.name()+ " \t");
}
System.out.println();
Days dys[] = Days.values();
for(Periods prds : Periods.values()) {
    for( int i = 0; i<dys.length;i++) {
	System.out.print(prds.name()+" \t");
    }
    System.out.println();
}
commented: thanks bhai jaaaan :) can i hve your email id +0

but our teacher says that print the values using ordinal() & nested while loop and store them in 2D Array as string ?

i've done this, but it is not printing values in a table , WHY ?
and how to create its 2D array ?

class EnumProg {
  enum Days {Mon, Tues, Wed, Thurs, Fri}
  enum Periods {Period1, Period2, Period3, Period4, Period5}
  public static void main (String [] args)
  {
    for(Days day : Days.values()){
      System.out.print(day + "" + "\t\t");
    }
  for(Days day : Days.values()){
    for ( Periods period : Periods.values() )
    {
    System.out.println (period);
    }
  System.out.println ("");
  }

 }
}

it is not printing values in a table

What is it printing? Please show the program's output.

[Edit]Thanks, Problem Solved
The output is,

Mon             Tues            Wed             Thurs           Fri
Period1
Period2
Period3
Period4
Period5

Period1
Period2
Period3
Period4
Period5

Period1
Period2
Period3
Period4
Period5

Period1
Period2
Period3
Period4
Period5

Period1
Period2
Period3
Period4
Period5

It is not printing in table, bcz u have used println() method instead of print() method in the second for loop.
The println() prints a newline after the contents, hence u r not getting in tabular form.

[Edit]Thanks, Problem Solved
The output is,

Mon             Tues            Wed             Thurs           Fri
Period1
Period2
Period3
Period4
Period5

Period1
Period2
Period3
Period4
Period5

Period1
Period2
Period3
Period4
Period5

Period1
Period2
Period3
Period4
Period5

Period1
Period2
Period3
Period4
Period5

try the following code using ordinal -

String daysPrds [][] = new String[6][5];

for(Days day : Days.values()) {    
    daysPrds[0][day.ordinal()] = day.name();
}
Days dys[] = Days.values();
for(Periods prds : Periods.values()) {    
    for( int i = 1; i<=dys.length;i++) {	
	daysPrds[i][prds.ordinal()] = prds.name();
    }    
}
for(int i = 0; i<daysPrds.length;i++) {
    for(int j = 0; j<daysPrds[i].length; j++) {
	System.out.print(daysPrds[i][j]+"\t");
    }
    System.out.println();
}

It is not printing in table, bcz u have used println() method instead of print() method in the second for loop.
The println() prints a newline after the contents, hence u r not getting in tabular form.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.