I have a program in which I am reading from a text file. Below is an extract of the text file. What I want to do is read the first 3 lines and output the unit number (TD684) to console
|12.233 |2016001 |TD684 |Prof. P |04/01/2016 |01
|12.233 |2016035 |TD684 |Prof. P |04/01/2016 |01
|12.233 |2016455 |TD684 |Prof. P |04/01/2016 |01
|10.456 |2016783 |BT473 |Dr. G |07/01/2016 |01
Below is the program I use to do this. I have initialized the variable switched to false. The first time the program is run it is supposed to assign TD684 to variable unit. After that it is supposed to check if the next line also had TD684. So ideally it should output TD684 3 times to console. But it only happens once which leads me to believe that the loop is not working.
while (switched == false){
String line = sc.nextLine();
String[] parts = line.split(Pattern.quote("|"));
if (timesrun == 0){
unit = parts[3];
timesrun++;
}
if (parts[3] == unit){
System.out.println(parts[3]);
}else{
switched = true;
}
}
Could someone please tell me the reason why my program is not looping?