File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Another  method of parsing a command line string Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Another  method of parsing a command line string" Watch "Another  method of parsing a command line string" New topic
Author

Another method of parsing a command line string

Randy Helene
Greenhorn

Joined: Apr 16, 2002
Posts: 21
I would like to thank the previous person who helped me with this.
I am writing a java application to parse a command line string such as java ParseEval 3.5 - 7.0 + 8.1. Only + and - operations are allowed. The string can contain spaces. I am at the point where I can find the position of the + or minus signs, but I am having trouble putting the numbers to be evaluated into a statement that can be evaluated.
I tried the method Gautam gave me and it worked. Thank you, because I learned something new. However, I am taking a java class and we haven't gone over push yet. Therefore I need to do this problem using charAt and then putting the numbers between the signs in a substring. I keep getting stuck here, when trying to specify each of the positions in the substring to get the calculation. We also need to allow for the case where the 1st argument is a sign. I'll show you what I have so far. Thank you for your help.
class ParseEval{
public static void main (String args[]) {
if (args.length==0) return;
String s = new String ();
char ch=0;
for (int i = 0; i < args.length; i++)
s=s.concat(args[i]);
for(int j=0; j < s.length();j++){
ch = s.charAt(j);
if ( ch=='+' || ch=='-'){
System.out.println("Ch is " +ch);
System.out.println("position is " +j);
}
}
}
}
Thanks again.
Peter Allen
Greenhorn

Joined: Apr 13, 2002
Posts: 13
This is an ugly way of doing it, but maybe it'll give you an idea. It works as long as the input is always given in that way.

I hope that'll help you.
[ May 03, 2002: Message edited by: Dirk Schreckmann ]
Doanh Nguyen
Ranch Hand

Joined: Dec 02, 2000
Posts: 45
Originally posted by Randy Helene:
However, I am taking a java class and we haven't gone over push yet.

class Java {
static double operand1;
static double operand2;
static String operator = "+";
public static void calc () {
if ((operand1 == 0) && (operand2 == 0)) return;
if (operator.equals("-")) operand1 -= operand2;
else operand1 += operand2;
operator = "+";
operand2 = 0;
}
public static void main (String args[]) {
if (args.length==0) return;
for (int i = 0; i < args.length; i++) {
String s = args[i].trim();
if (s.equals("-")) operator = s;
else if (s.equals("+")) operator = s;
else {
operand2 = Double.parseDouble(args[i]);
calc();
}
}
calc();
System.out.println(operand1);
}
}
Randy Helene
Greenhorn

Joined: Apr 16, 2002
Posts: 21
Thank you very much. That helped.
Sayed Ibrahim Hashimi
Ranch Hand

Joined: May 17, 2001
Posts: 148
If you are using SDK 1.4 then you can use the new Regular Expression package...java.util.regex
This will problably help you a lot if you know about Regular expressions.


SCJP 1.4<br /><a href="http://www.cise.ufl.edu/~sih" target="_blank" rel="nofollow">www.cise.ufl.edu/~sih</a>
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Another method of parsing a command line string
 
Similar Threads
question about Array index - ojective 7.6 on K&B SCJP book
application to parse a command line string and evaluate it.
using getopt() in Java
Enigma!
Ecryption program question