-3

My problem is I have 4 arrays, a[1]=1, a[2]=3, a[3]=4, a[4]=5, and want to save as new string/ char, so the output will be s[ ]={1345}

I try to define like this, but it doesn't works

char s[]= new char [5];
s={'a[1]','a[2]','a[3]','a[4]'};
luk2302
  • 55,258
  • 23
  • 97
  • 137
nana
  • 3
  • 1
  • 1
    I have no idea what exactly you're trying there. but you should know that 'a[1]' is not a valid char. try with: s = {'1', '2', '3' ,'4'}; – Stultuske May 24 '19 at 11:47
  • if you remove the quotes around the a[], it would probably work – Camden May 24 '19 at 11:50
  • If my guess is right, you want the array variable values in `s`? Your code would be `s={a[1],a[2],a[3],a[4]};` then, without quotes – Kaddath May 24 '19 at 11:50
  • You should loop over all the arrays, and build a string from the values. – Yassin Hajaj May 24 '19 at 11:51
  • "I have 4 arrays, a[1]=1, a[2]=3, a[3]=4, a[4]=5" - that is a single array, with 4 values in it. Just to make things clear. – Amongalen May 24 '19 at 11:53

2 Answers2

0

In Java the concept of String is fairly simple. You dont have to define it as a character array. Just take a String variable and concat the array values to it. The below is how you can do it.

public static void main(String[] args) {
int[] a = {1,2,3,4};
String output = a[0]+a[1]+a[2]+a[3];
System.out.println(output);
}

Hope it shall work for you.

0

Instead of initialising the char array s[] and then setting the value in the next line, you can directly initialize the array like: char s[] = {a[0], a[1], a[2], a[3]};

JDC
  • 4,247
  • 5
  • 31
  • 74
userTN
  • 86
  • 1
  • 7