diff options
Diffstat (limited to 'blaconvert.java')
-rw-r--r-- | blaconvert.java | 70 |
1 files changed, 60 insertions, 10 deletions
diff --git a/blaconvert.java b/blaconvert.java index f559683..e628319 100644 --- a/blaconvert.java +++ b/blaconvert.java @@ -28,8 +28,10 @@ 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
-
+ 1.2 - Replace crazy non unicode spaces with commas
+ TODO
+ - Raw output option - result only with no spaces/commas/units
*/
@@ -47,13 +49,31 @@ public class blaconvert { String sourceUnit = "";
String targetUnit = "";
String amount = "";
+ String version = "1.2";
+
/* Build query from query or arguments, depending on whether
arguments were given. */
if (args.length == 0) {
try {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] split = br.readLine().split(" ");
+ int start = 0;
int to = 0;
+ int output = 0;
+ if (split[0].startsWith("-"))
+ {
+ if (split[0].equals("-v") || split[0].equals("--version"))
+ {
+ System.out.println("v"+version);
+ System.exit(0);
+ } else if (split[0].charAt(1) == 'r')
+ {
+ //reserved for raw output option
+ output = 1;
+ start = 1;
+ }
+ }
+
for(int i = 0; i < split.length; i++)
{
if (split[i].toLowerCase().equals("to") || split[i].toLowerCase().equals("in"))
@@ -61,9 +81,9 @@ public class blaconvert { to = i;
}
}
- amount = split[0];
+ amount = split[start];
- for (int i = 1; i < to; i++)
+ for (int i = start+1; i < to; i++)
{
sourceUnit += split[i]+"%20";
}
@@ -76,6 +96,12 @@ public class blaconvert { System.exit(1);
}
} else {
+
+ if (args[0].equals("-v") || args[0].equals("--version"))
+ {
+ System.out.println("v"+version);
+ System.exit(0);
+ }
amount = args[0];
sourceUnit = args[1];
targetUnit = args[2];
@@ -138,10 +164,9 @@ public class blaconvert { }
String[] parts = rhs.split(" ");
- String result = "";
-
+ String result = fixCrazySpaces(parts[0]) + " ";
//Convert weird output into something that makes sense
- for (int i = 0; i < parts.length; i++)
+ for (int i = 1; i < parts.length; i++)
{
//System.out.println(parts[i]);
if (parts[i].equals("\\x26#215;"))
@@ -152,14 +177,21 @@ public class blaconvert { String[] exp = parts[i].split("\\\\");
parts[i] = "10^"+exp[2].substring(3, exp[2].length());
}
- result += parts[i]+" ";
+
+ result += parts[i] + " ";
}
-
+ parts = lhs.split(" ");
+ lhs = "";
+ parts[0] = fixCrazySpaces(parts[0]);
+ for (int i = 0; i < parts.length; i ++)
+ {
+ lhs += parts[i]+" ";
+ }
// Print results
- if(error.isEmpty()) {
- System.out.println(lhs+" = "+result);
+ if(error.isEmpty()) {
+ System.out.println(lhs+"= "+result);
System.exit(0);
@@ -177,5 +209,23 @@ public class blaconvert { }
}
+
+ private static String fixCrazySpaces(String string)
+ {
+ char[] chars = string.toCharArray();
+ String fixed = "";
+ for (int i = 0; i < chars.length; i++)
+ {
+ if (Character.getNumericValue(chars[i]) < 0 && chars[i] != '.')
+ {
+ fixed += ",";
+ } else {
+ fixed += chars[i];
+ }
+ }
+
+ return fixed;
+ }
+
}
|