0

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 ?

Edited by Xufyan: n/a

3
Contributors
7
Replies
8
Views
6 Years
Discussion Span
Last Post by java_programmer
Featured Replies
  • You can use the following logic to get the desired result. Just format the print according to your requirement... [CODE] 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(); … Read More

1

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

Edited by java_programmer: n/a

Comments
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 ?

Edited by Xufyan: n/a

0

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

 }
}

Edited by Xufyan: n/a

0

it is not printing values in a table

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

0

[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

Edited by Xufyan: n/a

0

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
1

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.

Edited by java_programmer: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.