import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLEncoder;
public class DynamicComputationWithYahoo {
public static void main(final String[] args) throws Exception {
final String test = "24/18*2+6^5";
final double retVal = doCompute(test);
System.out.println(retVal);
}
/**
* Lass yahoo die Arbeit machen
*
* @param expression math. ausdruck
* @return Ergebnis
* @throws Exception keine verbindung, Ergebnis nicht zu parsen, HTML-Code
* geändert, expression nicht zu parsen etc. etc.
*/
private static double doCompute(final String expression) throws Exception {
// wenn nötig, proxy setzen
System.getProperties().put("proxySet", "true");
System.getProperties().put("proxyPort", "8080");
System.getProperties().put("proxyHost", "irgendein.proxy");
// erst mal leerzeichen loswerden
final String tmpExpr = expression.replaceAll("\\s", "");
// wir schicken die anfrage an yahoo
final String urlStr = String.format(
"http://de.search.yahoo.com/search?p=%s&fr=yfp-t-501&ei=UTF-8&rd=r1", URLEncoder
.encode(tmpExpr, "UTF-8"));
final URL url = new URL(urlStr);
final BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
// ... und suchen in der Response nach dem Ergebnis
try {
String line = null;
while ((line = in.readLine()) != null) {
line = line.replaceAll("\\s", "");
if (line.contains(tmpExpr + "=")) {
int begIdx = line.indexOf("=", line.indexOf(tmpExpr));
int endIdx = line.indexOf("<", line.indexOf(tmpExpr));
return Double.parseDouble(line.substring(begIdx + 1, endIdx).trim().replaceAll(
",", "."));
}
}
} finally {
in.close();
}
throw new RuntimeException(String.format("Cannot parse expression '%s'", expression));
}
}
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLEncoder;
public class DynamicComputationWithYahoo {
public static void main(final String[] args) throws Exception {
final String test = "24/18*2+6^5";
final double retVal = doCompute(test);
System.out.println(retVal);
}
/**
* Lass yahoo die Arbeit machen
*
* @param expression math. ausdruck
* @return Ergebnis
* @throws Exception keine verbindung, Ergebnis nicht zu parsen, HTML-Code
* geändert, expression nicht zu parsen etc. etc.
*/
private static double doCompute(final String expression) throws Exception {
// wenn nötig, proxy setzen
System.getProperties().put("proxySet", "true");
System.getProperties().put("proxyPort", "8080");
System.getProperties().put("proxyHost", "irgendein.proxy");
// erst mal leerzeichen loswerden
final String tmpExpr = expression.replaceAll("\s", "");
// wir schicken die anfrage an yahoo
final String urlStr = String.format(
"http://de.search.yahoo.com/search?p=%s&fr=yfp-t-501&ei=UTF-8&rd=r1", URLEncoder
.encode(tmpExpr, "UTF-8"));
final URL url = new URL(urlStr);
final BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
// ... und suchen in der Response nach dem Ergebnis
try {
String line = null;
while ((line = in.readLine()) != null) {
line = line.replaceAll("\s", "");
if (line.contains(tmpExpr + "=")) {
int begIdx = line.indexOf("=", line.indexOf(tmpExpr));
int endIdx = line.indexOf("<", line.indexOf(tmpExpr));
return Double.parseDouble(line.substring(begIdx + 1, endIdx).trim().replaceAll(
",", "."));
}
}
} finally {
in.close();
}
throw new RuntimeException(String.format("Cannot parse expression '%s'", expression));
}
}
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLEncoder;
public class DynamicComputationWithGoogle {
public static void main(final String[] args) throws Exception {
final String test = "24/18*2+6^5";
final double retVal = doCompute(test);
System.out.println(retVal);
}
/**
* Lass yahoo die Arbeit machen
*
* @param expression math. ausdruck
* @return Ergebnis
* @throws Exception keine verbindung, Ergebnis nicht zu parsen, HTML-Code
* geändert, expression nicht zu parsen etc. etc.
*/
private static double doCompute(final String expression) throws Exception {
// wenn nötig, proxy setzen
System.getProperties().put("proxySet", "true");
System.getProperties().put("proxyPort", "8080");
System.getProperties().put("proxyHost", "irgendein.proxy");
// erst mal leerzeichen loswerden
final String tmpExpr = expression.replaceAll("\s", "");
// wir schicken die anfrage an yahoo
final String urlStr = String.format(
"http://de.search.yahoo.com/search?p=%s&fr=yfp-t-501&ei=UTF-8&rd=r1", URLEncoder
.encode(tmpExpr, "UTF-8"));
final URL url = new URL(urlStr);
final BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
// ... und suchen in der Response nach dem Ergebnis
try {
String line = null;
while ((line = in.readLine()) != null) {
line = line.replaceAll("\s", "");
if (line.contains(tmpExpr + "=")) {
int begIdx = line.indexOf("=", line.indexOf(tmpExpr));
int endIdx = line.indexOf("<", line.indexOf(tmpExpr));
return Double.parseDouble(line.substring(begIdx + 1, endIdx).trim().replaceAll(
",", "."));
}
}
} finally {
in.close();
}
throw new RuntimeException(String.format("Cannot parse expression '%s'", expression));
}
}
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLEncoder;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class DynamicComputationWithGoogle {
public static void main(final String[] args) throws Exception {
final String test = "24/18*2+6^5";
final double retVal = doCompute(test);
System.out.println(retVal);
}
/**
* Lass yahoo die Arbeit machen
*
* @param expression math. ausdruck
* @return Ergebnis
* @throws Exception keine verbindung, Ergebnis nicht zu parsen, HTML-Code
* geändert, expression nicht zu parsen etc. etc.
*/
private static double doCompute(final String expression) throws Exception {
// wenn nötig, proxy setzen
System.getProperties().put("proxySet", "true");
System.getProperties().put("proxyPort", "8080");
System.getProperties().put("proxyHost", "irgendein.proxy");
// erst mal leerzeichen loswerden
final String tmpExpr = expression.replaceAll("\s", "");
// wir schicken die anfrage an yahoo
final String urlStr = String.format(
"http://de.search.yahoo.com/search?p=%s&fr=yfp-t-501&ei=UTF-8&rd=r1", URLEncoder
.encode(tmpExpr, "UTF-8"));
final URL url = new URL(urlStr);
final BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
// ... und suchen in der Response nach dem Ergebnis
try {
String line = null;
while ((line = in.readLine()) != null) {
line = line.replaceAll("\s", "");
if (line.contains(tmpExpr + "=")) {
int begIdx = line.indexOf("=", line.indexOf(tmpExpr));
int endIdx = line.indexOf("<", line.indexOf(tmpExpr));
return Double.parseDouble(line.substring(begIdx + 1, endIdx).trim().replaceAll(
",", "."));
}
}
} finally {
in.close();
}
throw new RuntimeException(String.format("Cannot parse expression '%s'", expression));
}
}