You are provided the following list that contains (semi-random) years from modern history. Save the list to a text file named “events.txt” Write a program that:
import java.io.*;
import java.util.*;
public class EventSorter {
public static void main(String[] args) throws FileNotFoundException{
File file =new File("events.txt");
FileReader read = new FileReader(file);
LineNumberReader lines = new LineNumberReader(read);
Scanner readIn = new Scanner(file);
PrintWriter output = new PrintWriter("sorted_events.txt");
try{
//call for the file
//make sure it exists
if(file.exists()){
{
//first write this to determine the number of lines
int lineNumber = 0;
//gets the number of lines
while (lines.readLine() != null){
lineNumber++;
}
int[] event = new int[lineNumber];
int j = 0;
while(readIn.hasNext()){
event[j]=readIn.nextInt();
j++;
}
//sort the array
Arrays.sort(event);
boolean found;
for(int i = 0; i < event.length; i++){
if (event[i] == 1892){
//see if 1892 is on the list
System.out.println("CMU is a historic event");
found = true;
}
else{
addElement(event, 1892);
}
}
int[] sortedEvent = new int[lineNumber];
for(int k = 0; k < event.length; k++){
sortedEvent[k] = event[(event.length-1) - k];
System.out.println(sortedEvent[k]);
}
for(int print = 0 ; print < event.length; print++){
output.println(sortedEvent[print]);
}
}
readIn.close();
output.close();
lines.close();
}else{
System.out.println("File does not exist!");
}
}
catch(IOException e){
e.printStackTrace();
}
}
static int[] addElement(int[] a, int e) {
a = Arrays.copyOf(a, a.length + 1);
a[a.length - 1] = e;
return a;
}
}
I'm going to answer strictly to the title "Adding a value to a sorted array in Java 8". Two options can be:
Create a TreeSet
or any SortedSet
(or a structure based on a Binary Search Tree) from your array. This collection is sorted by default and any new items you add to it will keep it sorted.
Use something similar to binary search to find where in the array the new element should be placed.
This program does exactly what you want and answers the question of the teacher perfectly.
However, it's using several advanced techniques of Java and I strongly doubt that your teacher will give you any credit for it. So use it at your own risk.
The best you can do with this code is to read it, understand its concept and apply them in conjunction to what you know.
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
public class EventSorter {
public static void main(String[] args) {
try {
Path inPath = Paths.get("events.txt");
Path outPath = Paths.get("sorted_events.txt");
int event = 1892;
Comparator<Integer> lastFirst = Comparator.<Integer>naturalOrder().reversed();
List<Integer> events = Files.lines(inPath).map(Integer::valueOf).sorted(lastFirst).collect(Collectors.toCollection(ArrayList::new));
int pos = Collections.binarySearch(events, event, lastFirst);
if (pos < 0) {
events.add(~pos, event);
}
Files.write(outPath, events.stream().map(Object::toString).collect(Collectors.toList()));
} catch (IOException ex) {
System.out.println(ex.getMessage());
}
}
}
events.txt (input)
1982
1821
1934
1809
sorted_events.txt (output)
1982
1934
1892
1821
1809
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.