0

I have a class in my Application where int values are stored:

Characters.class:

public int charPunch(int q) {

    int[] charPunch = {
        15, 
        10, 
        20, 
        25, 
        20, 
        20, 
        15, 
        20, 
        20, 
        25
    };
    return charPunch(q);
}

The q value is decided by user character selection. I'm trying to understand the code and so just posted the code as it currently is.

In the same class file I have an array of Strings which I can then convert (in another .class file) with .toString();

Game.class:

oneName = Ch.charName(q).toString();

This gives playerOne's oneName the array value and converts the String array result to a single String and works!

My question is: Am I able to do the exact same thing to an array of int values?

  • Would changing the int array to a String array, converting the String array to a single String and then the String to an int be terrible programming but my best solution?

    String onePunch = charPunch(q).toString();
    int charPunchInt = Integer.parseInt(charPunch);
    

I currently get StackOverflowError on the Characters.class array's return line until the process gives up.

2
  • If you converted the int array to a single integer. What would that integer be? Commented Mar 19, 2014 at 16:05
  • I don't understand the question, however you get StackOverflowError because inside your method you call recursively charPunch without an exit condition Commented Mar 19, 2014 at 16:09

2 Answers 2

2

I currently get StackOverflowError on the Characters.class

This is because you're calling the same method over and over without stopping anytime. Basically, this is what your code looks like (apart from the rest of the code it has):

public int charPunch(int q) {
    return charPunch(q);
}

So it will call itself with the same argument and will do nothing more than fill up the stack memory until you get the error you indicate.

A possible solution may be adding some logic in your method to stop. Or, probably you wanted to access to an element of the array:

public int charPunch(int q) {
    int[] charPunch = {
        15, 
        10, 
        20, 
        25, 
        20, 
        20, 
        15, 
        20, 
        20, 
        25
    };
    return charPunch[q]; //<- using brackets [] instead of parenthesis ()
}

Note that now the current implementation of charPunch method may throw an IndexOutOfBoundsException if the value of q is less than 0 or bigger than the size of the array used.


If you try to execute this code:

String onePunch = charPunch(q).toString();
int charPunchInt = Integer.parseInt(charPunch);

It won't compile since you return an int from charPunch. An int is a primitive type and doesn't have any method at all. So, you can change your method to return an Integer instead and you will have access to toString method, but by doing that, the code above will be converting an integer into a string to convert the string into an integer (again), which seems meaningless.


Am I able to do the exact same thing to an array of int values?

Define what you really want to do, then you will get the expected help.

Sign up to request clarification or add additional context in comments.

1 Comment

That's embarrassingly simple. Thank you. I had tried using [q] but it had been generating other errors. Now it works.
0

A couple questions to iron out to fully understand your question.

Are the integer values inside your function charPunch(int q) always going to be the same?

Are you trying to convert that entire int array to a String or just the value passed via the function? What are you doing with the value after assignment?

Either way you may want to look at array lists and enhanced for loop syntax (for each loop).

Example

// if values are immutable (meaning they cannot be changed at run time)
static final int charPunches [] = {15,10,20,25};

// function to return string value
protected String getCharPunchTextValue(int q){
    String x = null;
    for(int i: charPunches){ // iterate through the array
        if(q == i){// test equality
            x = Integer.toString(i);// assign string value of integer
        }
    }
    return x; // Return value, what ever needs this value may check for null to test if value exists
}    

2 Comments

@ Patty P: 1) The integer values inside the method always stay the same. 2) I don't really want to convert any Integer values to String, I was hopeful there was another solution. 3) I use the initial values to set a variable, subtract from them until I get a result and then reuse the values to reset the variables and start over. I keep them in another .class simply to reduce clutter. I like your answer.
Thanks I guess you will want some type of constants class that contains static final copies of your values to reset your values once your are done with them.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.