File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Selecting characters from a string Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Selecting characters from a string" Watch "Selecting characters from a string" New topic
Author

Selecting characters from a string

Doug Wolfinger
Greenhorn

Joined: Jul 27, 2002
Posts: 18
Hello,
I've copied this from the Beginners forum, as I malignantly posted it there originally.
I am trying to pass a string that represents a postfix expression. The method that accepts it should take each number and place it on the stack. When it encounters an operator, it should pop the top two numbers and evaluate them with the operator.
This is the expression. "abc+-"
a=7, b=3, c=12 So it applies "+" to c and b, then it subtracts (c + b) from a. I have made the string into a string array in my program, but I think the idea is to pass the expression as a regular string as I have it above. I would use substring(), but that doesn't work with a number larger than 2 digits. Thanks!
Sayed Ibrahim Hashimi
Ranch Hand

Joined: May 17, 2001
Posts: 148
If you pass in a String vs. a String array the you have to delimit the numbers somehow. Because otherwise you wouldn't know what numbers are supposed to be together. For example:
a=7, b=3, c=12
7312+-
I could interpret that as:
a=7,b=31,c=2 or a=73,b=1,c=2
But if the numbers are delimited the theres no questions, lets say they are delimited by commas then your argument will be
7,3,12+-, so you know for sure that a=7, b=3, and c=12.
Do you have a choice on how you are getting(generating?) this String input?
If you know for sure that you will be given a String input that will contain numbers that only contain one digit per integer then you can use your current code and just create a String [] from the String.


SCJP 1.4<br /><a href="http://www.cise.ufl.edu/~sih" target="_blank" rel="nofollow">www.cise.ufl.edu/~sih</a>
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
If you can use the same delimiter for operators and operands (spaces, for example: "12 3 4 + -"), you can use a StringTokenizer to decompose the String.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Selecting characters from a string