Die letzten 5 Beiträge |
eulerscheZahl |
code: |
1:
|
System.out.print(String.format("%.2f", Math.round(100.0 * rations[i][j]) / 100.0).substring(0, 4) + " |"); |
|
|
UlfMitHand |
Okay, mein Java war ganz schön eingeschlafen. Ich habe einen 2 dim Array eingeführt um die Tabelle zu speichern.
Bei der Ausgabe der Tabelle habe ich jedoch noch ein Problem und zwar sind die Einträge (double Zahlen unter 1000 mit 2 Kommastellen) unterschiedlich lang. Gibt es eine leichte möglichkeit die Länge der Zahl immer auf bis zu 6 Zeichen auszustocken bei der Ausgabe in der Methode printMatrix?
Ich musste das http aus der url entfernen, weil ich keine urls posten darf.
code: |
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:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
public class Main {
public static void main(String[] args) {
// 0 1 2 3 4 5EX 6 7 8 9 10 11 12 13 14 15 16Wis 17Por 18Armo 19Whet 20Baub 21 22 23Mirr 24Eter 25Pera 26Dusk 27Midn 28Dawn 29Noon 30Grief 31Rage 32Hope 33Igno 34 35Eber 36Yrie 37Inya 38Volk Offer
String currency[] = {"Alte", "Fuse", "Alch", "Chao", "Gcp ", null, "Chro", "Jewl", "Chan", "Chis", "Scou", "Bles", "Regr", "Rega", "Divi", "Vaal", null, null, null, null, null, "Tran", "Augm", null, null, null, null, null, null, null, null, null, null, null , "Silv", null, null, null, null, null };
double ratios[][] = new double[currency.length][currency.length];
for ( int i = 0; i < currency.length; i++){
for (int j=0; j < currency.length; j++){
if (currency[j] != null && currency[i] != null) { //unwichtige sollen direkt rausgefiltert werden
String url = "http://currency.poe.trade/search?league=Prophecy&online=x&want="+(i+1)+"&have="+(j+1);
try {
HashMap<String, String> result = read(url);
if (result != null) {
if (result.get("data-sellvalue") != null || result.get("data-sellvalue")!= null ) {
//System.out.println(currency [j]+" to "+ currency[i]); zum testen ob es richtig ausgelesen wird
//System.out.println(result.get("data-sellvalue") + "\t" + result.get("data-buyvalue"));
ratios [i][j]= Double.parseDouble(result.get("data-sellvalue")) / Double.parseDouble(result.get("data-buyvalue")) ;
System.out.println(ratios [i][j]);
} // end of if
}
}catch (IOException e) {
e.printStackTrace();
}
}
}
}
printMatrix(ratios, currency);
} //end of main
public static HashMap<String, String> read(String url) throws IOException {
String[] parts = url.split("[=&]");
if (parts.length < 4)
return null;
HashMap<String, String> result = new HashMap<String, String>();
result.put(parts[parts.length - 4], parts[parts.length - 3]);
result.put(parts[parts.length - 2], parts[parts.length - 1]);
String website = getUrlSource(url);
website = website.substring(website.indexOf("<div class=\"displayoffer\"") + "<div class=\"displayoffer\"".length());
website = website.substring(0, website.indexOf(">"));
website = website.trim();
website = website.replace("\"", "");
parts = website.split("[= ]");
for (int i = 0; i < parts.length - 1; i += 2)
result.put(parts[i], parts[i + 1]);
return result;
}
private static String getUrlSource(String urlString) throws IOException
{
StringBuilder result = new StringBuilder();
URL url = new URL(urlString);
URLConnection spoof = url.openConnection();
spoof.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0; H010818)");
BufferedReader in = new BufferedReader(new InputStreamReader(spoof.getInputStream()));
String line = "";
while ((line = in.readLine()) != null) {
result.append(line + "\n");
}
in.close();
return result.toString();
}
private static void printMatrix (double[][] rations, String[] currency)
{
System.out.print(" ");
for (int i=0; i < currency.length; i++){
if( currency[i] != null ){
System.out.print("| "+currency[i]);
}
}
System.out.println();
for ( int j = 0; j < currency.length; j++){
if (currency[j]!= null) {
System.out.print(currency[j]+" |");
for (int i=0; i < currency.length; i++){
if (currency[i] != null){
System.out.print( Math.round(100.0 * rations[i][j]) / 100.0+" |");
}
}
System.out.println();
}
}
}
} |
|
|
UlfMitHand |
Wow!
Danke eulerscheZahl, das ging ja super schnell und hilft mir viel weiter!
Jetzt noch etwas tüfteln und es sollte genau das sein, was ich möchte.
|
eulerscheZahl |
Die Ausgabe erfolgt auf die Console. Aber das kannst du einfach anpassen, indem du die Ausgabe mit "> ziel.txt" umleitest.
code: |
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:
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
public class Main {
public static void main(String[] args) {
String url = "http://currency.poe.trade/search?league=Prophecy&online=x&want=1&have=2";
try {
HashMap<String, String> result = read(url);
if (result != null) {
System.out.println(result); //alle Werte ausgeben
System.out.println(result.get("data-sellvalue") + "\t" + result.get("data-buyvalue")); //nur bestimmtes ausgeben
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static HashMap<String, String> read(String url) throws IOException {
String[] parts = url.split("[=&]");
if (parts.length < 4)
return null;
HashMap<String, String> result = new HashMap<String, String>();
result.put(parts[parts.length - 4], parts[parts.length - 3]);
result.put(parts[parts.length - 2], parts[parts.length - 1]);
String website = getUrlSource(url);
website = website.substring(website.indexOf("<div class=\"displayoffer\"") + "<div class=\"displayoffer\"".length());
website = website.substring(0, website.indexOf(">"));
website = website.trim();
website = website.replace("\"", "");
parts = website.split("[= ]");
for (int i = 0; i < parts.length - 1; i += 2)
result.put(parts[i], parts[i + 1]);
return result;
}
private static String getUrlSource(String urlString) throws IOException
{
StringBuilder result = new StringBuilder();
URL url = new URL(urlString);
URLConnection spoof = url.openConnection();
spoof.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0; H010818)");
BufferedReader in = new BufferedReader(new InputStreamReader(spoof.getInputStream()));
String line = "";
while ((line = in.readLine()) != null) {
result.append(line + "\n");
}
in.close();
return result.toString();
}
} |
|
|
UlfMitHand |
Eine Zahl auf ähnlichen Internetseiten auslesen und einer Tabelle abspeichern
Hallo liebes Forum,
ich bin eigentlich kein Informatiker, hatte jedoch Java und etwas html in der Schule, also kann ich ein wenig mit Quelltexten und der Struktur anfangen.
So jetzt zu meinen Problem.
ich habe eine URL in der 2 Variablen stecken (zahlen von 1 bis 40).
Jetzt hätte ich gerne ein Programm (oder ähnliches) welches alle Möglichkeiten dieser URL aufruft und von der Seite, immer an der gleichen Stelle, eine Zahl ausliest und sie mir in einer Tabelle speichert. Das dazu gehörige Feld soll entsprechend der beiden Parameter gewählt werden, wobei die Zeile durch das erste Parameter und die Spalte durch das zweite Parameter bestimmt werden soll.
Als Dokument wäre ein .txt völlig ausreichend.
Es geht um diese URL:
//currency.poe.trade/search?league=Prophecy&online=x&want=1&have=2
wobei 1 und 2 die Parameter sind.
Der erste hier im Quelltext erscheinende Wert für data-sellvalue="XXXX"
soll durch den Wert data-buyvalue="XXXX"
Format der Variablen ist eine Kommazahl mit . als Komma geteilt werden und dann passend in einer Tabelle gespeichert werden.
Gibt es eine Möglichkeit dies leicht zu realisieren? Ich hoffe das es überhaupt möglich ist.
liebe grüße |
|
|