try-catch 필수로 삽입하기
Scanner은 반드시 닫아주기
https://www.javacodeexamples.com/java-print-hashmap-example/2243
미친 문제가 많이 엿같다...
https://github.com/MinKyuSong/2019_Fall_JAVA_CSE3040/blob/master/src/exercise/Level019.java
결론부터 이야기 하자면 나만의 Map 클래스를 만들어 TreeMap을 상속받아야 toString을 수정할 수 있다... 미친
package cse3040mp17;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;
@SuppressWarnings("serial")
class chMap extends TreeMap<String, Double> {
@Override
public String toString() {
String result = "";
Iterator<Map.Entry<String, Double>> it = super.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<String, Double> entry = it.next();
result += (entry.getKey() + " " + entry.getValue() + "\n");
}
return result;
}
}
class MapManager {
@SuppressWarnings("resource")
public static Map<String, Double> readData(String path) {
Map<String, Double> hm = new chMap();
BufferedReader br;
try {
br = new BufferedReader(new FileReader(path));
} catch (IOException e) {
return null;
}
try {
while (true) {
String line = br.readLine();
if (line == null)
break;
String[] buffer = line.split(" ");
hm.put(buffer[0], Double.parseDouble(buffer[1]));
}
br.close();
} catch (IOException e) {
e.printStackTrace();
return null;
} catch (Exception e) {
e.printStackTrace();
return null;
}
return hm;
}
}
public class Problem17 {
public static void main(String args[]) {
Map<String, Double> map = MapManager.readData("input.txt");
if (map == null) {
System.out.println("Input file not found.");
return;
}
System.out.println(map);
}
}
package cse3040mp18;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
@SuppressWarnings("serial")
class chMap extends TreeMap<String, Double> {
@Override
public String toString() {
String ans = "";
Set<Map.Entry<String, Double>> set = super.entrySet();
List<Map.Entry<String, Double>> list = new ArrayList<>(set);
Collections.sort(list, new ValueComparator<Map.Entry<String, Double>>());
Iterator<Map.Entry<String, Double>> it = list.iterator();
while (it.hasNext()) {
Map.Entry<String, Double> ent = it.next();
ans += ent.getKey() + " " + ent.getValue() + "\n";
}
return ans;
}
@SuppressWarnings("unchecked")
class ValueComparator<T> implements Comparator<T> {
@Override
public int compare(Object o1, Object o2) {
if (o1 instanceof Object && o2 instanceof Object) {
Map.Entry<String, Double> e1 = (Map.Entry<String, Double>) o1;
Map.Entry<String, Double> e2 = (Map.Entry<String, Double>) o2;
if (e1.getValue() < e2.getValue())
return -1;
else if (e1.getValue() > e2.getValue())
return 1;
else {
return e1.getKey().compareTo(e2.getKey());
}
}
return -1;
}
}
}
class MapManager {
@SuppressWarnings("resource")
public static Map<String, Double> readData(String str) {
Map<String, Double> map = new chMap();
BufferedReader br;
try {
br = new BufferedReader(new FileReader(str));
} catch (IOException e) {
return null;
}
try {
while (true) {
String line = br.readLine();
if (line == null)
break;
String buffer[] = line.split(" ");
map.put(buffer[0], Double.parseDouble(buffer[1]));
}
br.close();
} catch (IOException e) {
e.printStackTrace();
return null;
} catch (Exception e) {
e.printStackTrace();
return null;
}
return map;
}
}
public class Problem18 {
public static void main(String args[]) {
Map<String, Double> map = MapManager.readData("input.txt");
if (map == null) {
System.out.println("Input file not found.");
return;
}
System.out.println(map);
}
}
package cse3040mp19;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
class BookInfo implements Comparable<BookInfo> {
private String title;
private String author;
private String price;
private int rank = 1;
BookInfo(int rank, String title, String author, String price) {
this.rank = rank;
this.title = title;
this.author = author;
this.price = price;
}
@Override
public int compareTo(BookInfo o) {
return o.rank - this.rank;
}
public String toString() {
return "#" + (rank) + " " + title + ", " + author + ", " + price;
}
}
class BookReader {
private static ArrayList<String> lines = new ArrayList<>();
public static ArrayList<BookInfo> readBooks(String path) {
URL url = null;
String line = "";
BufferedReader br = null;
ArrayList<BookInfo> bk = new ArrayList<>();
try {
url = new URL(path);
br = new BufferedReader(new InputStreamReader(url.openStream()));
while ((line = br.readLine()) != null) {
if (line.trim().length() > 0)
lines.add(line);
}
br.close();
} catch (Exception e) {
e.printStackTrace();
}
int rank = 1;
int status = 0;
String title = "";
String author = "";
String price = "";
try {
for (int i = 0; i < lines.size(); ++i) {
String l = lines.get(i);
if (status == 0) {
if (l.contains("div class=\"col-lg-9 product-info-listView\""))
status = 1;
} else if (status == 1) {
if (l.contains("h3 class=\"product-info-title\""))
status = 2;
} else if (status == 2) {
if (l.contains("href=")) {
int tb = l.indexOf("href=\"") + "href=\"".length();
int begin = l.indexOf(">", tb) + 1;
int end = l.indexOf("</a>");
title = l.substring(begin, end);
status = 3;
}
} else if (status == 3) {
if (l.contains("div class=\"product-shelf-author contributors\"")) {
int tb = l.indexOf("<a href=") + "<a href=".length();
int begin = l.indexOf(">", tb) + 1;
int end = l.indexOf("</a>");
author = l.substring(begin, end);
status = 4;
}
} else if (status == 4) {
if (l.contains("td class=\"buy-new three-column-padding\""))
status = 5;
} else if (status == 5) {
if (l.contains("class=\" current link\"")) {
int tb = l.indexOf("<a title=") + "<a title=".length();
int begin = l.indexOf(">", tb) + 1;
int end = l.indexOf("</a>");
price = l.substring(begin, end);
bk.add(new BookInfo(rank++, title, author, price));
status = 0;
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return bk;
}
}
public class Problem19 {
public static void main(String[] args) {
ArrayList<BookInfo> books;
books = BookReader.readBooks("https://www.barnesandnoble.com/b/books/_/N-1fZ29Z8q8");
Collections.sort(books);
for (int i = 0; i < books.size(); i++) {
BookInfo book = books.get(i);
System.out.println(book);
}
}
}
찾아보니 class 속성명이 띄어쓰기 되어 있으면 다음과 같이하라고 한다.
String value="c2 l n";
Elements Stock_Data_Change = doc.getElementsByClass(value);
Element Stock_Data_Change = doc.select("td.c2.l.n");
Elements를 String으로 바꿀려면 outerHtml()을 사용하면 된다.
String tt = title.eq(i).outerHtml();
int tb = tt.indexOf("<a")+"<a".length();
int begin = tt.indexOf(">",tb)+1;
int end = tt.indexOf("</a>",begin);
package cse3040mp20;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
class BookInfo implements Comparable<BookInfo> {
private String title;
private String author;
private String price;
private int rank = 1;
BookInfo(int rank, String title, String author, String price) {
this.rank = rank;
this.title = title;
this.author = author;
this.price = price;
}
@Override
public int compareTo(BookInfo o) {
return o.rank - this.rank;
}
public String toString() {
return "#" + (rank) + " " + title + ", " + author + ", " + price;
}
}
class BookReader {
public static ArrayList<BookInfo> readBooksJsoup(String path) {
Document doc = null;
ArrayList<BookInfo> bi = new ArrayList<>();
try {
doc = Jsoup.connect(path).get();
Elements data = doc.select("ol.product-shelf-list.product-list-view").select("div.col-lg-9.product-info-listView");
int rank =1;
for(Element list:data) {
String title=list.select("h3.product-info-title").select("a").first().text();
String author = list.select("div.product-shelf-author.contributors").select("a").first().text();
String price = list.select("span.current").select("a").first().text();
bi.add(new BookInfo(rank++,title,author,price));
}
} catch (IOException e) {
e.printStackTrace();
}catch (Exception e) {
e.printStackTrace();
}
return bi;
}
}
public class Problem20 {
public static void main(String[] args) {
ArrayList<BookInfo> books;
books = BookReader.readBooksJsoup("https://www.barnesandnoble.com/b/books/_/N-1fZ29Z8q8");
Collections.sort(books);
for (int i = 0; i < books.size(); i++) {
BookInfo book = books.get(i);
System.out.println(book);
}
}
}