TomFirth
TomFirth

Reputation: 2364

Convert int array to int

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?

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

Upvotes: 0

Views: 6011

Answers (2)

Patty P
Patty P

Reputation: 351

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
}    

Upvotes: 0

Luiggi Mendoza
Luiggi Mendoza

Reputation: 85779

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.

Upvotes: 2

Related Questions