SangoProductions SangoProductions - 1 year ago 32
Java Question

How to pass in a substring of a Char array to another function for Java?

This works just fine right now... it's just that as it stands, I don't like how I use the

(a String) when I turned it in to
(a char[]), and so I want to know know if it's possible to take in the chars from
or however you find the last element of an array, preferably as a String.

I think you can do a loop, perhaps with a StringBuilder. That's a pretty simple way to do it. I can't imagine it's incredibly efficient though. I want to know if there's actually a better way.

The below code is basically just going "Hey, I did stuff."

Hey dudes. I've got a method that starts off like this:

public int evaluate(String readIn)
char[] tokens = readIn.toCharArray();

I take in a string from another function. It should look like "14+5^2" or something the other, because other methods have purified it to an equation. However, this equation could contain variable names (initialized earlier in the execution), like "x" so it could look like "x+5^myVal".

I've got a function from a previous attempt at doing this program, which interprets the variable names, where what is passed in is a substring, starting at where a variable is thought to be (typically, just every single character, that isn't already handled). This does work (given a whole String), I'm just making sure I give all relevant information.

public returnHelp isVariable(String readIn){
int i;
returnHelp returnType = new returnHelp();

for (i = 0; i < name.size(); i++){
//loop through all the variables we have!
if(readIn.length() >= getName(i).length()){
//ensure we don't get an index out of bounds error
if (readIn.substring(0, getName(i).length()-1).equals(getName(i))){
returnType.setStuff(true, i);
return returnType; //we found that it was, in fact, a variable
return returnType;

And this is what my returnHelp class looks like. I made it so that I could return 2 values instead of just 1, because it doesn't help knowing that it is a variable, if I don't know where in the Symbol Table the variable exists. And we'd just have to do the same search loop to find where it is, if we put them in separate functions. Perhaps I'm just lazy.

public class returnHelp {
boolean question = false;
int pointer = 0;

public void setStuff(boolean yes, int num){
question = yes;
pointer = num;


Just so there's no confusion, here's the "name" that is referenced in isVariable

ArrayList<String> name = new ArrayList<String>();
ArrayList<Double> value = new ArrayList<Double>();

However, I am working with a char[], not a String. Right now, I'm just working with the original string (with subString set to where in the loop we are), but that just feels sloppy.

Is there a way to pass in the "rest" of the char[], rather than resorting to just going back to the passed in String.

Say I'm at tokens[i]. Right now, I'm using
. It really feels as though I shouldn't be using that.

Answer Source

The usual pattern to pass "sub-arrays" is to pass offset and count (or from and to) parameters in addition to the array.

For example, Writer has this method:

public abstract void write(char[] cbuf,
     int off,
     int len)
                throws IOException

Writes a portion of an array of characters.