• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

java class to parse a command line string

 
R. Aw
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • 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....
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 33691
316
Eclipse IDE Java VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"RawJava",
Please check your PMs for a message from me.
 
Kaydell Leavitt
Ranch Hand
Posts: 689
Eclipse IDE Firefox Browser Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Ulf Dittmer
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • 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?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64178
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"R. A. W. Java", please check your private messages again.
 
Joanne Neal
Rancher
Pie
Posts: 3742
16
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Henry Wong
author
Marshal
Pie
Posts: 20831
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • 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 ]
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic