summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--blaconvert.java57
1 files changed, 52 insertions, 5 deletions
diff --git a/blaconvert.java b/blaconvert.java
index c493df8..f559683 100644
--- a/blaconvert.java
+++ b/blaconvert.java
@@ -14,9 +14,22 @@
*/
/*
+ blaconvert - Java program for converting units of measurement and currency using Google APIs
+ by blatech
+
Copyright 2011 Joe Robinson <joe@lc8n.com>
based on code by Jamie Walters <jagw@jagw.co.uk>
based on code by Joe Robinson <joe@lc8n.com>
+
+ Version 1.1
+
+ CHANGELOG
+ 1.0 - First version
+ 1.1 - Allow units with more than one word eg "light year"
+ - Added "same unit" error
+ - Ensure standard imperial measurements are used for some units - google defaults to US
+
+
*/
@@ -40,10 +53,24 @@ public class blaconvert {
try {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] split = br.readLine().split(" ");
+ int to = 0;
+ for(int i = 0; i < split.length; i++)
+ {
+ if (split[i].toLowerCase().equals("to") || split[i].toLowerCase().equals("in"))
+ {
+ to = i;
+ }
+ }
amount = split[0];
- sourceUnit = split[1];
- targetUnit = split[3];
-
+
+ for (int i = 1; i < to; i++)
+ {
+ sourceUnit += split[i]+"%20";
+ }
+ for (int i = to+1; i < split.length; i++)
+ {
+ targetUnit += split[i]+"%20";
+ }
} catch (Exception e) {
System.err.println("Error:" + e.getMessage());
System.exit(1);
@@ -53,10 +80,25 @@ public class blaconvert {
sourceUnit = args[1];
targetUnit = args[2];
}
-
+
+ String[] imperial = {"pint", "gallon", "gal", "cup", "teaspoon", "tablespoon", "fl oz", "fluid ounce"};
+
+ for(int i = 0; i < imperial.length; i++)
+ {
+ if (sourceUnit.startsWith(imperial[i]))
+ {
+ sourceUnit = "Imperial%20"+sourceUnit;
+ }
+
+ if (targetUnit.startsWith(imperial[i]))
+ {
+ targetUnit = "Imperial%20"+targetUnit;
+ }
+ }
//URL which provides XML response
URL url = new URL("http://www.google.com/ig/calculator?hl=en&q="+amount+sourceUnit+"=?"+targetUnit);
+ //System.out.println("http://www.google.com/ig/calculator?hl=en&q="+amount+sourceUnit+"=?"+targetUnit);
URLConnection urlc = url.openConnection();
BufferedReader br = new BufferedReader(new InputStreamReader(urlc.getInputStream()));
@@ -113,16 +155,21 @@ public class blaconvert {
result += parts[i]+" ";
}
+
+
// Print results
if(error.isEmpty()) {
-
System.out.println(lhs+" = "+result);
+
System.exit(0);
} else {
if(error.equals("4"))
{
error = "Unknown unit";
+ } else if (error.equals("0"))
+ {
+ error = "Both units are the same";
}
System.out.print("Error: "+error);
System.out.println("\n");