• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Another method of parsing a command line string

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much. That helped.
 
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic