+ 1

Problem with nested for lop

objDrone1.Sensors[0].Orientation = "SouthEast"; objDrone1.Sensors[0].ID[0] = 'E'; objDrone1.Sensors[0].ID[1] = 'A'; objDrone1.Sensors[0].ID[2] = 'T'; objDrone1.Sensors[0].ID[3] = '4'; objDrone1.Sensors[1].Orientation = "NorthWest"; objDrone1.Sensors[1].ID[0] = 'S'; objDrone1.Sensors[1].ID[1] = 'U'; objDrone1.Sensors[1].ID[2] = 'S'; objDrone1.Sensors[1].ID[3] = '7'; for(int j=0;j<2;j++) { for(int k=0;k<4;k++) { System.out.println(objDrone1.Sensors[j].ID[k]); } } NB: SOMEHOW THE OUTPUT FOR THIS IS SUS7SUS7 AND ITS SUPPOSED TO BE EAT4SUS7, IM REALLY CONFUSED AS TO WHY ITS NOT WORKING PROPERLY, PLEASE HELP

25th Feb 2021, 1:38 PM
Jeffrey Solomon Maluleka
Jeffrey Solomon Maluleka - avatar
3 odpowiedzi
+ 1
Can you share the full code? I think, It works as you expected...
25th Feb 2021, 2:05 PM
Jayakrishna 🇮🇳
+ 1
Its not full code. Where is main method? Where Drone class? For loop is fine but you may have errors in other part of code..
25th Feb 2021, 2:20 PM
Jayakrishna 🇮🇳
0
This is the full code class Main { Drone objDrone1 = new Drone(); Drone objDrone2 = new Drone(); objDrone1.Sensors = new Sensor[2]; objDrone2.Sensors = new Sensor[2]; for(int i=0;i<2;i++) { objDrone1.Sensors[i].ID = new char[4]; } objDrone1.Sensors[0].Orientation = "SouthEast"; objDrone1.Sensors[0].ID[0] = 'E'; objDrone1.Sensors[0].ID[1] = 'A'; objDrone1.Sensors[0].ID[2] = 'T'; objDrone1.Sensors[0].ID[3] = '4'; objDrone1.Sensors[1].Orientation = "NorthWest"; objDrone1.Sensors[1].ID[0] = 'S'; objDrone1.Sensors[1].ID[1] = 'U'; objDrone1.Sensors[1].ID[2] = 'S'; objDrone1.Sensors[1].ID[3] = '7'; for(int j=0;j<2;j++) { for(int k=0;k<4;k++) { System.out.println(objDrone1.Sensors[j].ID[k]); } } }
25th Feb 2021, 2:14 PM
Jeffrey Solomon Maluleka
Jeffrey Solomon Maluleka - avatar