The secret of how to be miserable is to constantly expect things are going to happen the way that they are "supposed" to happen.
You can have faith, which carries the understanding that you may be disappointed. Then there's being a willfully-blind idiot, which virtually guarantees it.
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Greg Sully wrote:this problem has been answered many times buddy.
you use i = i++; its means:
"first assigi(right) to i(left) then plus"
you should use ++i instead and you will be fine
You will get better results with 7 instead of 69.java SequentialPrinterDemo 1 2 69 4 -1 -2 -69 -4
Campbell Ritchie wrote:People are scared of Streams because they think Streams are difficult to use and error‑prone. But that isn't true. So it is possible to write complicated code using Streams, but the simpler uses of Streams are easier to read and write than loops, and harder to get wrong.You will get better results with 7 instead of 69.
Campbell Ritchie wrote:Why do you want to throw an Exception? Since the documentation for rangeClosed() says you can get the same effect with a loop (and similarly for range), surely if you pass 3 to that method, it will terminate with no output?
If you don't have any elements in the Stream, you get [] as your output; that would allow you to test whether you have a 0‑element Stream.java SequentialPrinterDemo 3 6 9 -3 -999
Printing from 4 to 3
Using loop:-
Using IntStream#range:-
[]
Using IntStream#rangeClosed:-
[]
Printing from 4 to 6
Using loop:-
4 5
Using IntStream#range:-
[4, 5]
Using IntStream#rangeClosed:-
[4, 5, 6]
Printing from 4 to 9
Using loop:-
4 5 6 7 8
Using IntStream#range:-
[4, 5, 6, 7, 8]
Using IntStream#rangeClosed:-
[4, 5, 6, 7, 8, 9]
Printing from 4 to -3
Using loop:-
Using IntStream#range:-
[]
Using IntStream#rangeClosed:-
[]
Printing from 4 to -999
Using loop:-
Using IntStream#range:-
[]
Using IntStream#rangeClosed:-
[]
Ryan McGuire wrote:I would bet streams are indeed more difficult to use and error-prone.
Which is why we see so many questions about why loops don't work correctly. In order to get a for loop to work, you have to be able to think low‑level about the indices. If you get a Stream wrong, it will be about the types and you will get a compile‑time error, whereas a loop gone wrong will cause an Exception be thrown, if you are lucky. If you are unlucky, it will silently return a wrong result, which is much more dangerous. Which is easier to understand, the imperative form or the functional form? The first is how I had to learn to fill an array with successive ints. The second is what I now think is the correct way to do it.Shorter and easier to read. Once you know what the range() method does, and I think the documentation is helpful here, it will be easier to predict the range of numbers in the array. Also, you don't have the array full of default values (as in line 2 of the first code block). It isn't too bad with 0s, but would be worse for arrays of a reference type, where the elements default to null.Ryan McGuire wrote:. . . If . . . the instructor hasn't gone over streams . . . I would bet streams are indeed more difficult to use and error-prone.
Liutauras Vilda wrote:
Ryan McGuire wrote:I would bet streams are indeed more difficult to use and error-prone.
Well, I'm not sure if they are error prone or at least more error prone than programing imperative way. ...
Liutauras Vilda wrote:Ryan, I didn't try to make you look... Alright, I might would agree about the part "more difficult", but not about the "error-prone" in either case