• Post Reply Bookmark Topic Watch Topic
  • New Topic

Printing the total of an arrays variables  RSS feed

 
Brian King
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'm dabbling around and just trying to print out the total of an arrays numbers.  Browsing online it seemed like the best way to do so was to use
a for loop but I can't figure out how to print the total out.  What I've tried just keeps indicating an error but not sure why.

Thanks for any help.

 
Pete Letkeman
Ranch Foreman
Posts: 914
26
Android Chrome IntelliJ IDE Java MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are missing a { for the for loop. If you put that in you will get better results.
 
Brian King
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
God, I'm so dumb.  Thanks.
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Brian King wrote:. . .
There is a much quicker and less error‑prone way to write such arrays: the array initlaiser. It is easier to write and there is no risk of having an array the wrong size. Try this instead:-If, as you are doing here, you declare and initialise the array on the same line, you may omit new int[].

Use spaces not tabs for indenting; you are going one worse, mixing spaces and tabs. We have some suggestions here. You can get a decent text editor to convert a tab to four spaces automatically, so you won't notice you are using spaces not tabs.

There has to be a quicker way to use an IntStream to do your additions. I can think of at least two quick ways to create an IntStream. One is to use the appropriate method of the Arrays class and the other is the factory method of IntStream. I think if you try them, you will find them so much easier to use that you will conclude that the for loop is no longer the correct way to summate such arrays. You will of course find, if you go through the methods for IntStream, that is has a method to calculate the sum of all elements for you.The difference between the two is that in the second case the reference to the array is available for other code to use. Note different indentation convention: make the dots align vertically.
I prefer the Arrays#stream method to Arrays.asList(something).stream(), which in this instance would give you a Stream<int[]> and that would be a bit harder to use.
 
Knute Snortum
Sheriff
Posts: 4284
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Brian King wrote:God, I'm so dumb.  Thanks.

We've all been there, and occasionally we visit too!
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This morning, I wrote:. . . would give you a Stream<int[]> and that would be a bit harder to use.
Actually, I have tried it and got a Stream<Integer>. Still less easy to use than an IntStream in this instance.
 
Brian King
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks everyone!
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's a pleasure
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!