0

I'm trying to create a String of an array {1, 2, 3, 4, 5, 6, 7} but I can't seem to get the right answer when I run it.

int [] arr = {1, 2, 3, 4, 5, 6, 7};
String s = " ";

for(int i = 0; i < arr.length; i++){
  s = (arr[i] + ", ");
}

System.out.println(s);

The answer I get is 7 when I was actually hopping to get 1, 2, 3, 4, 5, 6, 7

Can someone please explain to me why this code below gives me the correct answer but when I use a string it doesn't and how I should correct my code above.

int [] arr = {1, 2, 3, 4, 5, 6, 7};

for(int i = 0; i < arr.length; i++){
  System.out.print(arr[i] + ", ");
}
5
  • 1
    How about Arrays.toString()? Commented Feb 3, 2015 at 15:59
  • just replace = with += Commented Feb 3, 2015 at 16:01
  • In addition to the technical debugging of your code, when you're constructing a string like that in a loop, use a StringBuilder instead. Java String objects are immutable, and it's expensive to create and discard lots of intermediate copies; StringBuilder lets you add all the characters you need and then turn it into a String. Commented Feb 3, 2015 at 16:02
  • Per the close reasons, voting to close as a typo Commented Feb 3, 2015 at 16:02
  • @Everv0id that works too thanks but since I'm new to programming I'm trying to stay away from built in codes Commented Feb 3, 2015 at 16:11

7 Answers 7

3

You are reinitializing s at every iteration s = arr[i] + " " so when the loop is done s will be the last value in the array.

To add all the values to s you have to use += instead of =

For example s += String.valueOf(arr[i]) + " "

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

2 Comments

Thanks for the explanation, I didn't realized that strings worked that way.
@stargazer: To be perfectly correct, it aint Strings that works that way. It is the assignment operator '=' that works that way.
1

You need to append to your string. That is, you need to add another number on the end (using +=) instead of just re-assigning the string for each number (using =).

for (int i = 0; i < arr.length; ++i){
    s += arr[i] + " ";
}

Or more concisely:

for (int n : arr) {
    s += n + " ";
}

Or, if you don't want to get told off by people worried about performance, use a StringBuilder:

StringBuilder sb = new StringBuilder();
for (int n : arr) {
    s.append(n).append(' ');
}
String s = sb.toString();

Or just use Arrays.toString(arr) to do the whole thing.

Comments

0

Change your code to this:

for(int i = 0; i < arr.length; i++){
  s += (arr[i] + " ");   <<---- you we're missing the '+' 
}

Comments

0

You overwrite your String, if you use s = (arr[i] + " ");.

Better use s += (arr[i] + " "); or s = s.concat(arr[i] + " ");

Comments

0

The problem with the first code is object s is override on each iteration of for loop...that's why it is printing the last overrided value which is 7 To print all the values you can use s = s+(arr[i] + ", ");

Comments

0

If you want the value to be printed out each time you get it from the array, you should put System.out.println(s); inside of your for loop, instead of after the for loop. If you are looking to print it all in one line, then you should use s.concat(arr[i] + ", ");. This will add the value from the array to the end of the string.

Comments

0

Basically, for what you're looking for, just change s = (arr[i] + ", "); to s = s.concat(arr[i] + ", ");. That will give you a list, printed out. To make it look nicer, you could then take a character count of the string and use a substring to remove the last comma.

I hope this helps.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.