Brief description:
I´ve build up a "reinvention-of-the-wheel" solution for parsing Strings
with decimal number values to Float
Objects. Basically i´ve read some data from various HTML tables as String
Objects and parsed 2 Strings
of every htmlTableRow
into numeric values, e.g: (String str = "13,404"
).
I ended up using str.replace("," , ".")
due to NumberFormatException
on every single value and i ask myself, if there are any useful Java libraries for this use case.
How can a "best of practice solution look like?" OR
"What can be done better / easier" in terms of reducing code and decreasing the execution time.
I created the ListValuedMaps essentially to subcategorize the data later (according to other criteria).
Java code (Eclipse):
import java.io.*;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import java.util.*;
import org.apache.commons.collections4.ListValuedMap;
import org.apache.commons.collections4.multimap.ArrayListValuedHashMap;
public class Webdata {
public static void main(String[] args) throws IOException
{
long start = System.currentTimeMillis();
parseData();
System.out.println(secondsElapsed(start)+" seconds processing time");
};
private static void parseData() throws IOException
{
List <String> subdirectories = new ArrayList<>();
String chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
String errorMessage1 = "String formatting problem";
String errorMessage2 = "String object non existent";
for (int i=0; i< chars.length(); i++)
{
subdirectories.add("https://www.tradegate.de/indizes.php?buchstabe="+chars.charAt(i));
}
List <String> stockMetadata = new ArrayList<>();
ListValuedMap <String, List<String>> nonError = new ArrayListValuedHashMap<>();
ListValuedMap <String, List<String>> numFormatError = new ArrayListValuedHashMap<>();
ListValuedMap <String, List<String>> nullPointerError = new ArrayListValuedHashMap<>();
int cap1 = 44;
int cap2 = 56;
for (int suffix = 0; suffix <chars.length(); suffix++)
{
Document doc = Jsoup.connect(subdirectories.get(suffix).toString()).get();
Elements htmlTableRows = doc.getElementById("kursliste_abc").select("tr");
htmlTableRows.forEach( tr->
{
String stockName = tr.child(0).text();
String bid_price = tr.child(1).text();
String ask_price = tr.child(2).text();
String numOrders = tr.child(4).text();
String isin = tr.child(0).selectFirst("a").absUrl("href").substring(cap1,cap2);
try
{
if (calcSpread(bid_price, ask_price) < 12)
{
Collections.addAll(stockMetadata, isin, bid_price, ask_price, calcSpread(bid_price, ask_price).toString());
nonError.put(stockName,stockMetadata);
System.out.printf("%-45s %-10s %-10s %-10s %n",
stockName, bid_price, ask_price, calcSpread(bid_price, ask_price));
}
else if (calcSpread(bid_price, ask_price) > 12)
{
Collections.addAll(stockMetadata, isin, bid_price, ask_price, calcSpread(bid_price, ask_price).toString());
nonError.put(stockName,stockMetadata);
System.out.printf("%-45s %-10s %-10s %-10s %n",
stockName, bid_price, ask_price, calcSpread(bid_price, ask_price));
}
stockMetadata.clear();
}
catch (NumberFormatException e)
{
Collections.addAll(stockMetadata, e.getMessage());
numFormatError.put(stockName, stockMetadata);
stockMetadata.clear();
System.out.printf("%-45s %-10s %-10s %-10s %n",
stockName, bid_price, ask_price, errorMessage1);
}
catch (NullPointerException Ne)
{
Collections.addAll(stockMetadata, Ne.getMessage());
nullPointerError.put(stockName, stockMetadata);
stockMetadata.clear();
System.out.printf("%-45s %-10s %-10s %-10s %n",
stockName, bid_price, ask_price, errorMessage2);
} //end of try-catch
}); //end of for-each loop htmlTableRows
} //end of JSOUP method
} //end of main method
public static Float calcSpread (String arg1, String arg2)
{
try
{
Float bid = Float.parseFloat(arg1.replace("," , "."));
Float ask = Float.parseFloat(arg2.replace("," , "."));
Float spread = ((ask-bid)/ask)*100;
return spread;
}
catch (NumberFormatException e)
{
return null;
}
}
public static Long secondsElapsed(Long start) {
Long startTime = start;
Long endTime = System.currentTimeMillis();
Long timeDifference = endTime - startTime;
return timeDifference / 1000;
}
} //end of class
Maven dependencies / pom.XML
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>TEST</groupId>
<artifactId>TEST</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.11.3</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>4.4</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.0</version>
</dependency>
</dependencies>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<release>18</release>
</configuration>
</plugin>
</plugins>
</build>
</project>
Result:
....
Zincx Resources Corp. 0,0666 0,084 20.714281
Zinnwald Lithium PLC 0,109 0,125 12.800002
Zion Oil & Gas Inc. 0,1648 0,1758 6.2571063
Zions Bancorporation 57,50 57,76 0.45013562
Zip Co. Ltd. 0,54 0,57 5.263153
ZipRecruiter Inc. 17,66 18,165 2.7800772
Zivo Bioscience Inc. 3,02 3,14 3.8216598
Zoetis Inc. CL A 155,84 158,26 1.5291282
Zoom Video Communications Inc. 78,08 79,12 1.3144602
Zoomd Technologies Ltd. 0,22 0,24 8.333332
Zoomlion Heavy Ind.S.&T.Co.Ltd 0,379 0,4286 11.572563
Zovio Inc. 0,236 0,238 0.8403379
Zscaler Inc. 168,40 170,10 0.9994192
ZTE Corp. 2,092 2,158 3.0583868
ZTO Express (Cayman) Inc. 27,40 28,20 2.8368835
ZTO Express (Cayman) Inc. ADRs 25,80 27,60 6.521744
Zumtobel Group AG 6,27 6,34 1.1041036
Zuora Inc. Class A 7,722 7,888 2.104461
Zur Rose Group AG ./. ./. String object non existent
Zurich Insurance Group AG ./. ./. String object non existent
ZW Data Action Technolog. Inc. 0,9788 1,0285 4.832277
Zylox-Tonbridge Medical Tech. 1,19 1,26 5.5555506
Zymergen Inc. 2,607 2,6965 3.3191156
Zymeworks Inc. 5,578 5,698 2.1060002
Zynerba Pharmaceuticals Inc. 1,0172 1,1212 9.275774
8 seconds processing time