How to print Specific enum value from a loop??


I created a Two D array and accessing Enum values through it...
I wanted to assign the 3rd enum value whose ordinal is '2'
to the following rows and coloumn...[1][0] and [2][4]
i tried this,
DnP[1][0] = DnP [2][4] = Periods.Period1;

but it is not working.....i also tried name() function but it is printing all the values.

here is the code....

class Test {
 enum Days {Monday,Tuesday,Wednesday,Thursday,Friday}
 enum Periods {Period1,Period2,Period3,Period4,Period5}
 public static void main (String [] args)
 {
  String DnP [][] = new String [8][5];
    for (Days dys : Days.values())
     {
      DnP [0][dys.ordinal()] = dys.name();
      System.out.print (DnP[0][dys.ordinal()] + "\t    ");
     }
    System.out.println ("\n");
    for (Periods pRds : Periods.values())
    DnP[1][0] = DnP [2][4] = Periods.Period1;
    System.out.print (DnP[1][0]);
 }
}

Do you get any error messages when you compile this code?
Please copy and paste them here if you do.

If there are no errors, show the output from the program and explain what is wrong with it and show what you want the output to be.

Try the following code, it should work.

DnP[1][0] = DnP [2][4] = Periods.Period1.name()

How to print Specific enum value from a loop??


I created a Two D array and accessing Enum values through it...
I wanted to assign the 3rd enum value whose ordinal is '2'
to the following rows and coloumn...[1][0] and [2][4]
i tried this,
DnP[1][0] = DnP [2][4] = Periods.Period1;

but it is not working.....i also tried name() function but it is printing all the values.

here is the code....

class Test {
 enum Days {Monday,Tuesday,Wednesday,Thursday,Friday}
 enum Periods {Period1,Period2,Period3,Period4,Period5}
 public static void main (String [] args)
 {
  String DnP [][] = new String [8][5];
    for (Days dys : Days.values())
     {
      DnP [0][dys.ordinal()] = dys.name();
      System.out.print (DnP[0][dys.ordinal()] + "\t    ");
     }
    System.out.println ("\n");
    for (Periods pRds : Periods.values())
    DnP[1][0] = DnP [2][4] = Periods.Period1;
    System.out.print (DnP[1][0]);
 }
}

Why are you trying to put the names of enum element into a String array?
What good does it do to have the enum if you are working with Strings?

Try the following code, it should work.

DnP[1][0] = DnP [2][4] = Periods.Period1.name()

This code works without a loop , i want my program to take specific value from a loop
for example , if i want to print Period1 from a loop how will i do this ?

for(int i=0; i < 2; i++)
  System.out.println("Period1"); // print string in a loop

Your question makes no sense. You need to explain in a lot more detail about what you are trying to do.
For example
Why are you trying to put the names of enum element into a String array?
What good does it do to have the enum if you are working with Strings?

Edited 6 Years Ago by NormR1: n/a

here is the explanation,

consider a loop of enum values,

for (Days days : Days.values() )

now consider a 2D array of Strings,

String Days_Periods [][] = new String [5][5]

Now i want to store first day to the First Row, 2nd day to the Second Row , third Day to the third row , forth day to the fourth and fifth day to the fifth row and i successfully done this,
here is the code

Days_Periods [0][0] = Monday.name();
    Days_Periods [1][0] = Tuesday.name();
    Days_Periods [2][0] = Wednesday.name();
    Days_Periods [3][0] = Thursday.name();
    Days_Periods [4][0] = Friday.name();

but i want to store it using loop , H0w can i achieve the above requirement using loops ?

I want to convert the above code in something like this,

Days_Periods [i][0] = k.name();

is that possible to do something like this ??

Days_Periods [0] = k.name();
That line of code assigns the same value to each element indexed by i.

If you put the days into an array, then you could index that array in parallel:


Days[] days = {Days.Monday, Days.Tuesday, Days.Wednesday};
      for(int i=0; i < days.length; i++)
        System.out.println("day " + i + ", name=" + days[i].name());
//day 0, name=Monday
//day 1, name=Tuesday
//day 2, name=Wednesday
Comments
ty :)

one more question...
for 1 Dimensional Array you've done this

Days[] days = {Days.Monday, Days.Tuesday, Days.Wednesday};

i tried this for 2D array,

Days_Periods [0][] = {{Days.Monday}, {Days.Tuesday}, {Days.Wednesday}};

but the program not running with this code

Edited 6 Years Ago by Xufyan: n/a

i am trying to store all Days in first Row (0)

DnP [0][Days.ordinal()] days = {
                                            {Days.Monday},
                                            {Days.Tuesday}, 
                                            {Days.Wednesday}
                                            };

but this is giving error,


C:\Program Files\Java\jdk1.6.0_20\bin>javac Test.java
Test.java:9: not a statement
Days_Periods [0][Days.ordinal()] days = {
^
Test.java:9: ';' expected
Days_Periods [0][Days.ordinal()] days = {
^
Test.java:9: illegal start of expression
Days_Periods [0][Days.ordinal()] days = {
^
Test.java:10: not a statement
{Days.Monday},
^
Test.java:10: ';' expected
{Days.Monday},
^
Test.java:10: illegal start of expression
{Days.Monday},
^
Test.java:11: not a statement
{Days.Tuesday},
^
Test.java:11: ';' expected
{Days.Tuesday},
^
Test.java:11: illegal start of expression
{Days.Tuesday},
^
Test.java:12: not a statement
{Days.Wednesday}
^
Test.java:12: ';' expected
{Days.Wednesday}
^
Test.java:62: class, interface, or enum expected
}
^
12 errors

Your posted code does not agree with the posted error messages.

Your wasting everyone's time by not posting the code that goes with the error messages.

i am not wasting anyone's time...when u asked me to post the original code i already updated the code that is why i was not able to provide you the require code because line numbers were changed.

i wanted to store Days from enum in first row of String Array using loop,
like this.

Currently i am storing days manually like this,

Days Mon = Days.Mon;
    Days Tue = Days.Tue;
    Days Wed = Days.Wed;
    Days Thu = Days.Thu;
    Days Fri = Days.Fri;
  // Storing Days in first Row of the Array
    Dys_Prds [0][0] = Mon.name();
    Dys_Prds [0][1] = Tue.name();
    Dys_Prds [0][2] = Wed.name();
    Dys_Prds [0][3] = Thu.name();
    Dys_Prds [0][4] = Fri.name();

but i don't know how to store them in first row using loops.

i also tried this ,

Dys_Prds[0][] = new String [] {Days.Mon,Days.Tue,Days.Wed,Days.Thu,Days.Fri};

bit getting this error

Test.java:10: illegal start of expression
Dys_Prds[0][] = new String [] {Days.Mon,Days.Tue,Days.Wed,Days.Thu,Days.Fri};
^
1 error

Edited 6 Years Ago by Xufyan: n/a

Test.java:12: cannot find symbol
symbol : variable length

for (int i=0; i<day.length; i++)

day is not an array.It does not have a length attribute.

Test.java:13: cannot find symbol
symbol : variable Days

The variable Days is not defined in the second code segment you show.

You MUST show the whole program where there are errors. Just showing segments of the code does not allow anyone to know what is in the rest of your code. The compiler sees all of you code and doesn't like it (gives errors). No one can help you if you hide some of your program.

how to store them in first row using loops.

To use a loop you must have all the values stored in another array that you can copy them from.
For example:

Days[] days = {Days.Monday, Days.Tuesday, Days.Wednesday};  // define an array of Days
      for(int i=0; i < days.length; i++) {
        System.out.println("day " + i + ", name=" + days[i].name());  // show contents of array
       }

Edited 6 Years Ago by NormR1: n/a

Here is the complete code of my pr0gram...

class Test {
 enum Days {Mon,Tue,Wed,Thu,Fri}
 enum Periods {Islam,Java,AutoCAD,Elec,DIFF_EQ,English}
 public static void main (String [] args)
 {
  int Day_Ordinal,Periods_Row,Periods_Coloumn;
  System.out.println ("\n      *** Time Table T.E 2A 2010 ***\n\n");
  // Creating 2D Array of 8 rows and 6 coloumns
  String Dys_Prds [][] = new String [8][6];
Dys_Prds[0][] = new String [] {Days.Mon,Days.Tue,Days.Wed,Days.Thu,Days.Fri};
  // Initializing variable for each Day

  // Printing all values stored in First Row of the Array
    for (Day_Ordinal=0; Day_Ordinal<6; Day_Ordinal++)
        System.out.print (Dys_Prds[0][Day_Ordinal] + "\t");
        System.out.println ("\n");
  // Storing Time for the start of Each Period
    Dys_Prds [1][0] = "9:00";
    Dys_Prds [2][0] = "9:50";
    Dys_Prds [3][0] = "10:40";
    Dys_Prds [4][0] = "11:30";
    Dys_Prds [5][0] = "12:20";
    Dys_Prds [6][0] = "1:10";
    Dys_Prds [7][0] = "2:40";
  // Initializing variable for each Period
    Periods Islam = Periods.Islam;
    Periods Java = Periods.Java;
    Periods AutoCAD = Periods.AutoCAD;
    Periods Elec = Periods.Elec;
    Periods DIFF_EQ = Periods.DIFF_EQ;
    Periods English = Periods.English;
  // Storing Period in Array according to their Positions
    Dys_Prds[2][1] = Dys_Prds [3][5] = Islam.name();
    Dys_Prds[5][2] = Dys_Prds [7][4] = Dys_Prds [5][5] = English.name();
    Dys_Prds[3][1] = Dys_Prds [4][1] = Dys_Prds [4][2] = Dys_Prds [4][5] = DIFF_EQ.name();
    Dys_Prds[2][2] = Dys_Prds [3][2] = Dys_Prds [6][4]= Elec.name();
    Dys_Prds[6][2] = Dys_Prds [4][4] = Dys_Prds [5][4]= Java.name();
    Dys_Prds[1][4] = Dys_Prds [2][4] = Dys_Prds [3][4] = AutoCAD.name();
    Dys_Prds [1][3] = Dys_Prds [2][3] = Dys_Prds [3][3] = Elec.name() + ".PR";
    Dys_Prds [4][3] = Dys_Prds [5][3] = Dys_Prds [6][3] = Java.name() + ".PR";
  // Printing Periods using Nested for Loop
    for (Periods_Row=1; Periods_Row<7; Periods_Row++){
      for (Periods_Coloumn=0;Periods_Coloumn<6;Periods_Coloumn++)
      System.out.print (Dys_Prds[Periods_Row][Periods_Coloumn] + "\t");
      System.out.println ("");      
    }
  // Break Time at 2:00PM i.e after 6 Periods
     System.out.println ("\n2:00\t\t \"BREAK\" \n");
  // Printing Periods after the Break Time
    for (Periods_Row=7; Periods_Row<8; Periods_Row++){
        for (Periods_Coloumn=0;Periods_Coloumn<6;Periods_Coloumn++)
      System.out.print (Dys_Prds[Periods_Row][Periods_Coloumn] + "\t");
      System.out.println ("");
    }
  }
}

The error is,

Test.java:10: illegal start of expression
Dys_Prds[0][] = new String [] {Days.Mon,Days.Tue,Days.Wed,Days.Thu,Days.Fri};
^
1 error


i want to store all Days in first row of Array using Loop but i don't know how to do this

Change line 10 to:

Dys_Prds[0] = new String [] {Days.Mon.name(),Days.Tue.name(),Days.Wed.name(),Days.Thu.name(),Days.Fri.name()};

Edited 6 Years Ago by NormR1: n/a

You can do this..

public class Test {

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		int MAX_DAYS = 7;//Number of max days in week
		Days[][] Days_Period = new Days[MAX_DAYS][1]; 
		int i = 0; //Counter for Days_Period array
		
		for(Days day: Days.values()){//Get each day from enum starting with Monday

			Days_Period[i][0] = day;   
			System.out.println(Days_Period[i][0]);//Test print the days after each add
			i++;
		}
	}

}

As mentioned your code for Days in an array is not valid because Days is of type enum not of type String, so you use the .name method to return it as a string.

The way I illustrate it, for each enum value , it puts its value to an array on each iteration.
The type of the array is of type enum Days.
If you wanted strings you would have to change the type for the array to String and call a method on each enum value to get either it's name or a string representation of the enums value.

Edited 6 Years Ago by yasuodancez: n/a

This article has been dead for over six months. Start a new discussion instead.