Reputation: 626
I was thinking about this and wondering if it is possible for a string and an integer to be on the same input line. For example, the user puts in "A=2", "B=3", and "C= A+B", with C equaling 5. If so, what type of technique would I have to know? What would I have to look up?
Upvotes: 0
Views: 149
Reputation: 1148
If you're trying to detect command line arguments then I would consider something like this approach:
First you'll want to make sure the user actually inputs some amount of arguments:
public static void main(String[] args)
{
if(args.length > 0)
{
//Check each set of arguments.
}
else
{
System.out.println("Invalid number of arguments");
System.exit(1);
// Here you can either do a try-catch and throw an exception or just simply exit if the user doesn't input the `enter code here`correct number of arguments.
}
}
The tricky part will be determining whether the user has input A or B or C, and that will cause some amount of parsing. But you will need to know where in the input String it lies, either by telling the user the usage format, or searching the string.
Let's say you have the user to use the following method to input the parameters:
[program] A=2 B=3 C=A+B
Since WChargin pointed out that args is space-delimted, which slipped my mind, I decided to break up each set of arguments into their own string array. For A and B I split the string by the delimiter by the character "=" as so:
if(args[i].toUpperCase().startsWith("A"))
{
resultA = args[i].split("="); //Split by the delimter "="
a = Double.parseDouble(resultA[1]);
}
Which for A and B will produce the array {A,2}, {B,3}. C I will split twice. First by the character "=" which will produce {C,A+B} and then split each string, which will produce { ,A,+,B}. Note that split() produces an empty string in resultC[0], so we start iterating at 1.
We will simply check the length of args, and iterate through to find the parameters values:
public static void main(String[] args)
{
double a = 0;
double b = 0;
double c = 0;
String[] resultA = null;
String[] resultB = null;
String[] resultC = null;
String[] result = null;
if(args.length > 0)
{
for(int i=0; i < args.length; i++)
{
if(args[i].toUpperCase().startsWith("A")) // Implemented startsWith() thanks to WChargin
{
resultA = args[i].split("="); //Split by the delimter "="
a = Double.parseDouble(resultA[1]);
}
else if(args[i].toUpperCase().startsWith("B"))
{
resultB = args[i].split("=");
b = Double.parseDouble(resultB[1]);
}
else if(args[i].toUpperCase().startsWith("C"))
{
result = args[i].split("="); //We don't need C, split all of the arguments
resultC = result[1].split(""); //This way we have an array of strings for each value to iterate through
// The only problem with split("") is that we will get an empty string at the beginning of the array
for(int j=1; j < resultC.length; j++)
{
if(resultC[j].toUpperCase().startsWith("A"))
{
if(resultC[j+1].equals("+"))
{
if(resultC[j+2].toUpperCase().startsWith("A"))
{
c = a + a;
break;
// Once we get out answer, break otherwise we'll get a ArrayIndexOutOfBoundsException because the program will continue iterating
}
else if(resultC[j+2].toUpperCase().startsWith("B"))
{
c = a + b;
break;
}
else
{
System.out.println("Argument parse error");
}
}
else if(resultC[j+1].equals("-"))
{
if(resultC[j+2].toUpperCase().startsWith("A"))
{
c = a - a;
break;
}
else if(resultC[j+2].toUpperCase().startsWith("B"))
{
c = a - b;
break;
}
else
{
System.out.println("Argument parse error");
}
}
else if(resultC[j+1].equals("*"))
{
if(resultC[j+2].toUpperCase().startsWith("A"))
{
c = a * a;
break;
}
else if(resultC[j+2].toUpperCase().startsWith("B"))
{
c = a * b;
break;
}
else
{
System.out.println("Argument parse error");
}
}
else if(resultC[j+1].equals("/"))
{
if(resultC[j+2].toUpperCase().startsWith("A"))
{
c = a / a;
break;
}
else if(resultC[j+2].toUpperCase().startsWith("B"))
{
c = a / b;
}
else
{
System.out.println("Argument parse error");
}
}
}
else if(resultC[j].toUpperCase().startsWith("B"))
{
if(resultC[j+1].equals("+"))
{
if(resultC[j+2].toUpperCase().startsWith("A"))
{
c = b + a;
break;
}
else if(resultC[j+2].toUpperCase().startsWith("B"))
{
c = b + b;
break;
}
else
{
System.out.println("Argument parse error");
}
}
else if(resultC[j+1].equals("-"))
{
if(resultC[j+2].toUpperCase().startsWith("A"))
{
c = b - a;
break;
}
else if(resultC[j+2].toUpperCase().startsWith("B"))
{
c = b - b;
break;
}
else
{
System.out.println("Argument parse error");
}
}
else if(resultC[j+1].equals("*"))
{
if(resultC[j+2].toUpperCase().startsWith("A"))
{
c = b * a;
break;
}
else if(resultC[j+2].toUpperCase().startsWith("B"))
{
c = b * b;
break;
}
else
{
System.out.println("Argument parse error");
}
}
else if(resultC[j+1].equals("/"))
{
if(resultC[j+2].toUpperCase().startsWith("A"))
{
c = b / a;
break;
}
else if(resultC[j+2].toUpperCase().startsWith("B"))
{
c = b / b;
break;
}
else
{
System.out.println("Argument parse error");
}
}
}
else
{
System.out.println("Argument error in C");
System.exit(1);
}
}
}
}
}
else
{
System.out.println("Invalid number of arguments");
System.exit(1);
}
System.out.printf("A: %f\nB: %f\nC: %f\n", a, b, c);
}
Please note I probably did not account for all possibilities.
There are definitely easier ways to parse through command line arguments. I am giving you the overly long method.
I hope this helps you!
Upvotes: 1
Reputation: 16037
A good place to start would be a HashMap and String.split.
You might try something like this:
// In main function
HashMap<String, Integer> map = new HashMap<String, Integer>();
for (String arg : args) {
String[] split = arg.split(Pattern.quote("="));
if (split.length > 1) {
// It had an equals sign.
String var = split[0], value = split[1];
map.put(var, Integer.parseInt(value));
System.out.println(var + " is now " + value);
}
}
This won't account for A+B
, but I'll leave that as a puzzle to you.
Hint: map.containsKey, map.get.
EDIT
To answer your questions:
for (int i=0; i<args.length; i++) {
String arg = args[i];
// do something with arg
}
is the same as
for (String arg : args) {
// do something with arg
}
The latter is called "for-each" or "for-in." The args
variable contains the command-line arguments. I'm assuming that this code is in the main method, which should have a method signature of public static void main(
String[] args
)
.
The put
method adds the key/value pair to the HashMap. Basically, a hashmap is an association (map) of keys to values. Each key can have one value, and put
overwrites, so if you run
map.put("a", 1);
map.put("b", 2);
map.put("a", 3);
then the following statements are true:
map.get("a") == 3
map.get("b") == 2
map.get("c") == null
map.get("asdf") == null
For more info, check out the map tutorial.
Upvotes: 1
Reputation: 424
If you use Java 6+ then scripting is quite useful:
public static void main(String[] args) {
try {
ScriptEngineManager mgr = new ScriptEngineManager();
ScriptEngine scripEngine = mgr.getEngineByExtension("js");
scripEngine.eval("A=2; B=3; C=A+B");
System.out.println("" + scripEngine.get("A") + "+" + scripEngine.get("B") + "=" + scripEngine.get("C"));
} catch (ScriptException e) {
e.printStackTrace();
}
}
Output: 2.0+3.0=5.0
Upvotes: 0
Reputation: 67
first you must to do string matching. where position number in backside '=' is '2' and where position string in front side '=' is 'A' and save the string 'A' to into number index array, and save the number '2' into value index array A. then technic to use B=2 same like that. the last you detect with string matching. on backside and front side operator, example '=' is 'A' and 'B'. and put index's array that same this string. and sum value. then to save sum value into variable string C same like save variable A and B
Upvotes: 0