-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem16.java
More file actions
86 lines (80 loc) · 2.12 KB
/
Problem16.java
File metadata and controls
86 lines (80 loc) · 2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
class Element implements Comparable<Element>{
private String fruit;
private double price;
public Element(String fruit, double price) {
this.fruit = fruit;
this.price = price;
}
public String getName() {return this.fruit;}
public double getPrice() {return this.price;}
public int compareTo(Element e) {
if(this.price < e.getPrice()) return -1;
if(this.price > e.getPrice()) return 1;
return this.fruit.compareTo(e.getName());
}
public String toString() {return this.fruit + " " + this.price;}
}
class ElementReader{
public static ArrayList<Element> readElements(String f) {
ArrayList<Element> l = new ArrayList<Element>();
BufferedReader br = null;
//StringBuffer input = new StringBuffer();
String line = "";
String oldtext = "";
try {
br = new BufferedReader(new FileReader(f));
}catch (FileNotFoundException e) {
return null;
}
try {
while((line = br.readLine()) != null) {
oldtext += line+"\r\n";
}
br.close();
String newtext = oldtext.replaceAll("apple", "lemon");
FileWriter writer = new FileWriter(f);
writer.write(newtext);
writer.close();
} catch(IOException e) { }
/*while(true) {
try {
br.
String line = br.readLine();
if(line == null) break;
String[] a = line.split(" ");
if(a[0] == "apple")
{br..readLine().replaceAll("apple", "aaaa");
br.}
//l.add(new Element(a[0],Double.parseDouble(a[1])));
} catch(IOException e) {
e.printStackTrace();
}
}
try {
br.close();
}catch(IOException e) {
e.printStackTrace();
}*/
return l;
}
}
public class Problem16 {
public static void main(String[] args) {
ArrayList<Element> list = ElementReader.readElements("input.txt");
if(list == null) {
System.out.println("Input file not found.");
return;
}
Collections.sort(list);
Iterator<Element> it = list.iterator();
while(it.hasNext()) System.out.println(it.next());
}
}