Question

In: Computer Science

**JAVA** Exercise •Consider an input file of test scores in reverse ABC order: Yeilding Janet 87...

**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! :)

Solutions

Expert Solution

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.


Related Solutions

C++ Programming Consider an input file that lists test participants in alphabetical order and their scores....
C++ Programming Consider an input file that lists test participants in alphabetical order and their scores. Each line has the following format: LastName FirstName score Load the input file into an array of structs, knowing there is a maximum of 50 participants. Output to output.txt the participants with the top 5 scores, in decreasing order of scores. Each output line should have the following format: FirstName,LastName,score Notes: The name of the input file is acquired through standard input. If the...
C++ Programming Consider an input file that lists test participants in alphabetical order and their scores....
C++ Programming Consider an input file that lists test participants in alphabetical order and their scores. Each line has the following format: LastName FirstName score Load the input file into an array of structs, knowing there is a maximum of 50 participants. Output to output.txt the participants with the top 5 scores, in decreasing order of scores. Each output line should have the following format: FirstName,LastName,score Notes: The name of the input file is acquired through standard input. If the...
IN JAVA!!! In this project, you will use radix.txt as the input file, and output the...
IN JAVA!!! In this project, you will use radix.txt as the input file, and output the integers SORTED USING RADIX SORT. You may assume all your input consists of integers <=9999. Your main program will input the integers and put them into a QUEUE. It will then pass this queue to a method called radixSort which will sort the numbers in the queue, passing the sorted queue back to main. The main program will then call another method to print...
Write a java program to reverse element of a stack. For any given word (from input),...
Write a java program to reverse element of a stack. For any given word (from input), insert every character (from the word) into a stack. The output from the stack should be the same as the input. Your program should be using a stack and a queue to complete this process. 1. Push into stack 2. Pop from stack 3. Enqueue into queue 4. Dequeue from queue 5. Push into stack 6. Pop from stack and display java
JAVA Assignment: Project File Processing. Write a program that will read in from input file one...
JAVA Assignment: Project File Processing. Write a program that will read in from input file one line at a time until end of file and output the number of words in the line and the number of occurrences of each letter. Define a word to be any string of letters that is delimited at each end by either whitespace, a period, a comma or the beginning or end of the line. You can assume that the input consists entirely of...
Write a C program, called reverse, using standard I/O functions, to take a file as input...
Write a C program, called reverse, using standard I/O functions, to take a file as input then copies it to another file in reverse order. That is, the last byte becomes the first, the byte just before the last one becomes the second, etc. The program call should look like: reverse fileIn fileOut
Java. Given an input file with each line representing a record of data and the first...
Java. Given an input file with each line representing a record of data and the first token (word) being the key that the file is sorted on, we want to load it and output the line number and record for any duplicate keys we encounter. Remember we are assuming the file is sorted by the key and we want to output to the screen the records (and line numbers) with duplicate keys. We are given a text file and have...
Java Write a program that will only accept input from a file provided as the first...
Java Write a program that will only accept input from a file provided as the first command line argument. If no file is given or the file cannot be opened, simply print “Error opening file!” and stop executing. A valid input file should contain two lines. The first line is any valid string, and the second line should contain a single integer. The program should then print the single character from the string provided as the first line of input...
This program must be in java, use printf, and request user input for a file name....
This program must be in java, use printf, and request user input for a file name. NameSubsLast3 (30 points) Write a program that does the following. Write a program that does the following. Requests the name of an input file and uses it to create an input file Scanner. Requests the name of an output file and uses it to create a PrintWriter. I have posted firstNames.txt and outNames.txt in the homework for Lesson 6. Calls createArray to create a...
c++ Given the test scores of 7 students are 96, 88, 83, 76, 90, 91, 87....
c++ Given the test scores of 7 students are 96, 88, 83, 76, 90, 91, 87. Write a program that will calculate the average and the standard deviation. ( Standard deviation is given by sqrt[ sum(score-avg)^2/(N-1) ]  )
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT