// CSE 143, Autumn 2013 // This program demonstrates the use of stacks and queues. // It reads in a file of students' exam scores in reverse order, such as: // // Zahn William 92 // Yu Vivian 95 // Wolf Adam 87 // // And first strips the ones who got 100 on the exam using a queue, // then prints the rest in reverse order using a stack. import java.util.*; import java.io.*; public class Exams { public static void main(String[] args) throws FileNotFoundException { Stack tas = new Stack(); // read the lines from the file (in reverse ABC order) into a stack Scanner input = new Scanner(new File("exams.txt")); while(input.hasNextLine()) { tas.push(input.nextLine()); } // filter out the students who got 100 on the exam Queue q = new LinkedList(); while(!tas.isEmpty()) { String s = tas.pop(); if(!s.endsWith("100")) { q.add(s); } } // print all of the students' information in ABC order int size = q.size(); for(int i = 0; i < size; i++) { String line = q.remove(); System.out.println(line); q.add(line); } } }