aspose file tools*
The moose likes Java in General and the fly likes java class to parse a command line string Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "java class to parse a command line string" Watch "java class to parse a command line string" New topic
Author

java class to parse a command line string

R. Aw
Greenhorn

Joined: Feb 02, 2008
Posts: 4
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

Joined: May 26, 2003
Posts: 30752
    
156

"RawJava",
Please check your PMs for a message from me.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Kaydell Leavitt
Ranch Hand

Joined: Nov 18, 2006
Posts: 689

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
Marshal

Joined: Mar 22, 2005
Posts: 42268
    
  64
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?


Ping & DNS - my free Android networking tools app
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61413
    
  67

"R. A. W. Java", please check your private messages again.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3646
    
  15
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.


Joanne
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

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 ]

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

"R. Aw",
Please check your private messages.
-Ben


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: java class to parse a command line string