954,554 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

rare program :s

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 ?

Xufyan
Posting Whiz
309 posts since Mar 2010
Reputation Points: 6
Solved Threads: 7
 

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();
}
java_programmer
Junior Poster
124 posts since May 2006
Reputation Points: 10
Solved Threads: 17
 

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

Xufyan
Posting Whiz
309 posts since Mar 2010
Reputation Points: 6
Solved Threads: 7
 

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 ("");
  }

 }
}
Xufyan
Posting Whiz
309 posts since Mar 2010
Reputation Points: 6
Solved Threads: 7
 
it is not printing values in a table


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

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 

[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
Xufyan
Posting Whiz
309 posts since Mar 2010
Reputation Points: 6
Solved Threads: 7
 

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
java_programmer
Junior Poster
124 posts since May 2006
Reputation Points: 10
Solved Threads: 17
 

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.
java_programmer
Junior Poster
124 posts since May 2006
Reputation Points: 10
Solved Threads: 17
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You