• 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

java class to parse a command line string

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't understand how to go about coding this:
write a java class (console application) to parse a command line string and evaluate it (only + and - operators allowed). The string may or may not contain spaces.

I don't understand what this means, is this the output:
java ParseEval 3.5 - 7.0 + 8.1

how would one code this....
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"RawJava",
Please check your PMs for a message from me.
 
Ranch Hand
Posts: 694
Mac OS X Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome,

I don't understand what this means, is this the output:
java ParseEval 3.5 - 7.0 + 8.1



I believe that:

java ParseEval 3.5 - 7.0 + 8.1

is what is typed into the command-line. Your task is to write a Java source-file called ParseEval.java such that when it is executed with the command on the command-line, it calculates the answer to the expression:

3.5 - 7.0 + 8.1

Hint: It seems that you can assume that each part of the expression to be evaluated is separated by spaces so when you get the parameters in your Java program, they will already be separated into an array for you.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you familiar with how Java applications are run from the command line, and especially the main method that gets passed the command-line parameters in a String array?
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"R. A. W. Java", please check your private messages again.
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Kaydell Leavitt:
Hint: It seems that you can assume that each part of the expression to be evaluated is separated by spaces



I don't think he can. The spec says 'The string may or may not contain spaces', so I think
java ParseEval 3.5-7.0+8.1
would be a valid command line.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The part of the assignment to pay attention to is this one (in terms of providing the biggest hint) ...

(only + and - operators allowed)



"+" and "-" has the same precedence. And without the other operators, there is no need to worry about the Order of Evaluation. The parsing can simply be done by going left to right, using a scanner, and a case statement in a loop.

Henry
[ February 04, 2008: Message edited by: Henry Wong ]
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"R. Aw",
Please check your private messages.
-Ben
 
You may have just won ten million dollars! Or, maybe a tiny ad.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic