-2

I'm trying to create an object by converting an 'int' to a 'String'. I'm working on an ADT stack program but I was asked to use data type String.

*I was able to figure out:

int top = -1;
String stkTop = Integer.toString(top);

*But how do I create this int to String?

int[] data;
data = new int[10];
shmosel
  • 49,289
  • 6
  • 73
  • 138

3 Answers3

0

You can use Arrays.toString() to accomplish this

String stkTop = Arrays.toString(data);
dm0275
  • 1
  • 2
0

Once you have the values for the array you can just do a simple for loop.

int[] data = {1,2,3,4,5,6,7,8,9,10};
String numbers = "";

for(int x=0; x<data.length; x++){
    numbers = numbers + data[x] + " ";
}
Corey Bernard
  • 48
  • 1
  • 1
  • 7
0

You can do something like this:

public String intArrayToString(int[] in){

    Stringbuilder sb = new StringBuilder();

    for (int x : data) 
            sb.append(x);

    return sb;
}
Eddie Martinez
  • 13,582
  • 13
  • 81
  • 106