My Question is following: Given a number n as input, return a new string array of length n, containing the strings “0″, “1″, “2″ so on till n-1. If n=0, return an array of length 0.The expected output is- stringArray(4) = {"0","1","2","3"} and the actual output is stringArray(4) = {0,1,2,3}.How can I add numbers in the form of string.
public class ArrayOfNumbers {
static int testcase1=4;
public static void main(String[] args){
ArrayOfNumbers testInstance=new ArrayOfNumbers();
String[] result=testInstance.arrayOfNumbers(testcase1);
System.out.print("{");
for (int i=0;i<result.length;i++){
if (i>0)
System.out.print(",");
System.out.print(result[i]);
}
System.out.println("}");
}
public String[] arrayOfNumbers(int num) {
int n=0;
String n1="n";
String[] arr=new String[num];
for(int i=0;i<num;i++){
arr[i]=n1;
n=n+1;
}
return arr;
}
}
"marks when you print each number?String.valueOf()to parse a number to a string.