In: Computer Science
**JAVA** Exercise
•Consider an input file of test scores in reverse ABC order:
Yeilding Janet 87
White Steven 84
Todd Kim 52
Tashev Sylvia 95...
•Write code to print the test scores in ABC order using a stack.
–What if we want to further process the tests after printing?
Thank you for any and all help! :)
import java.io.*; 
import java.io.FileNotFoundException; // for exception handling
import java.util.*; 
public class Print_in_ABC_format {
  public static void main(String[] args) {
    Stack<String> stack = new Stack<String>(); // creating a new empty stack 
    try {
      File myObj = new File("aniket.txt");   // creating a file object
      Scanner myReader = new Scanner(myObj);   // Reading the file
      while (myReader.hasNextLine()) {         // This loop runs till the file has data in next lines
        String data = myReader.nextLine();     // Actually getting the data in the data variable
        stack.push(data);                      // pushing the data in the stack
      }
      myReader.close();                        // closing the resource after using
      
      while(!stack.empty()){                   // loop will run until stack is not empty
         String ss=stack.peek();              
         System.out.println(ss);               // printing the element
         stack.pop();                          // removing the top element
      }
      
    } catch (FileNotFoundException e) {
      System.out.println("Error:File Not Found"); // if file not found this portion executes
      e.printStackTrace();
    }
  }
}
Above is the java code for the given Question.
I have written comments against every line so that it's easy to understand the code.