Question

In: Computer Science

How to read a text file and store the elements into a linked list in java?...

How to read a text file and store the elements into a linked list in java?

Example of a text file:

CS100, Intro to CS, John Smith, 37, 100.00

CS200, Java Programming, Susan Smith, 35, 200.00

CS300, Data Structures, Ahmed Suad, 41, 150.50

CS400, Analysis of Algorithms, Yapsiong Chen, 70, 220.50

and print them out in this format:

Course: CS100

Title: Intro to CS Author:

Name = John Smith,

Age = 37

Price: 100.0.

And also to print out the Most expensive/cheapest book and the Average price.

Solutions

Expert Solution

import java.util.*;
import java.lang.*;
import java.io.*;


class Solution
{
   public static void main (String[] args) throws java.lang.Exception
   {
       String content = new String();
int count=1;
File file = new File("abc.txt"); //file name
LinkedList<String> list = new LinkedList<String>(); // declare link list
try {
Scanner sc = new Scanner(new FileInputStream(file)); // file input
while (sc.hasNextLine()){
content = sc.nextLine();
list.add(content);
}   
sc.close();
}
catch(FileNotFoundException fnf){
fnf.printStackTrace();
}
catch (Exception e) {
e.printStackTrace();
System.out.println("Program terminated Safely...");
}
TreeMap<Double, String> price = new TreeMap<>(); // tree map to get expensive and cheapest book name
Double sum = 0;
Iterator i = list.iterator();
while (i.hasNext()) {
String[] arrOfStr = ((String)i.next()).split(",", 5);
System.out.println("Course: " +arrOfStr[0]);
System.out.println("Title: " +arrOfStr[1]);
System.out.println("Name: " +arrOfStr[2]);
System.out.println("Age: " +arrOfStr[3]);
System.out.println("Price: " +arrOfStr[4]);
price.put(Double.parseDouble(arrOfStr[4]),arrOfStr[1]);
sum = sum + Double.parseDouble(arrOfStr[4]);
}
double average = sum/(double)price.size(); //calculation of average price
System.out.println("Most Expensive Book Is: " + price.get(price.lastKey()));
System.out.println("Cheapest Book Is: " + price.get(price.firstKey()));
System.out.println("Average Price Is: " + average);
  
   }
}

input file

output:


Related Solutions

Computer Science - Java Programming How do you read a text file and store three different...
Computer Science - Java Programming How do you read a text file and store three different pieces of information in the database when the given text file contains this info.: 12345 Computer Science Bob Stone 23456 Art James G. Ocean? These are written in the format as ID Class Name. I was going to take the three different pieces of information by separating them by spaces, but the number of spaces is random and I don't know how to adjust...
java question: How would you be able to store a matrix from a text file into...
java question: How would you be able to store a matrix from a text file into a linked or doubly linked list, if you cannot use 2D arrays? input example: 1 2 3 4 1 3 2 4 4 2 3 1
JAVA How to make a graph class that uses a linked list class to store nodes...
JAVA How to make a graph class that uses a linked list class to store nodes and linked list within each node to store adjacency list The linked list class has been made already.   import java.util.*; public class LinkedList implements Iterable { private int size = 0; private Node head; private Node tail; private class Node { private T data; private Node prev; private Node next; public Node(T data) { this.data = data; } }    public Iterator iterator() {...
Please write a java program to write to a text file and to read from a...
Please write a java program to write to a text file and to read from a text file.
Could you write a c- program that reads a text file into a linked list of...
Could you write a c- program that reads a text file into a linked list of characters and then manipulate the linked list by making the following replacements 1. In paragraph 1 Replace all “c” with “s” if followed by the characters “e”, “i” or “y”; otherwise 2. In pragraph 2 Replace "We" with v"i" This is the text to be manipulated: Paragraph1 She told us to take the trash out. Why did she do that? I wish she would...
Could you write a c- program that reads a text file into a linked list of...
Could you write a c- program that reads a text file into a linked list of characters and then manipulate the linked list by making the following replacements 1. Replace all “c” with “s” if followed by the characters “e”, “i” or “y”; otherwise 2. Replace "sh" with ph This is the text to be manipulated: Paragraph1 She told us to take the trash out. Why did she do that? I wish she would not do that Paragraph 2 We...
You are required to read in a list of stocks from a text file “stocks.txt” and...
You are required to read in a list of stocks from a text file “stocks.txt” and write the sum and average of the stocks’ prices, the name of the stock that has the highest price, and the name of the stock that has the lowest price to an output file. The minimal number of stocks is 30 and maximal number of stocks in the input file is 50. You can download a input file “stocks.txt” from Canvas. When the program...
Java Generic 2D Linked List Problem How to convert a 1D linked List into multiple linked...
Java Generic 2D Linked List Problem How to convert a 1D linked List into multiple linked lists with sequential values together? //Example 1: [1,1,2,3,3] becomes [[1,1],[2],[3,3]] //Example 1: [1,1,2,1,1,2,2,2,2] becomes [[1,1],[2],[1,1],[2,2,2,2]] //Example 3: [1,2,3,4,5] becomes [[1],[2],[3],[4],[5]] public <T> List<List<T>> convert2D(List<T> list) { // Given a 1D, need to combine sequential values together. }
I need to write a java program (in eclipse) that will read my text file and...
I need to write a java program (in eclipse) that will read my text file and replace specific placeholders with information provided in a second text file. For this assignment I am given a text file and I must replace <N>, <A>, <G>, with the information in the second file. For example the information can be John 22 male, and the template will then be modified and saved into a new file or files (because there will be multiple entries...
write a recursive method that returns the product of all elements in java linked list
write a recursive method that returns the product of all elements in java linked list
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT