• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

pyramid of numbers

 
Marshal
Posts: 80634
471
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why have you marked all the methods static in the second version?
Why do your methods in the first version return this rather than an int[] or similar? If you are reading into a field of the current object, why not make the method void? There may be a good reason for returning this: you can write
myObject.foo(...).bar(...).baz(...).biz(...).buzz(...);
If you go through the API for StringBuilder you will see that is how must of its methods are designed.
I suggest you indent your Streams code differently:-You can replace the sort() call with this:-It is probably better to sort the IntStream in line 3 than the Stream<String> in line 2, otherwise "19" will come before "2".

For consecutive numbers, consider starting with the IntStream#range() and IntStream#rangeClosed() methods.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I marked all the static methods in the second version to simplify the code.
In my first version, my methods return this because I wanted to write something like object.method1().method(). It was only for that.

Thanks for your suggestions, it makes the code clearer.    
 
Sheriff
Posts: 7126
185
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sai rama krishna wrote:
i wonder why the method returns App like below

public App createArrayOfDigits()


It is so the methods can be chained together like this:

[edit] Oops, didn't see that this had already been answered.
 
Ranch Hand
Posts: 1004
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Saisir les chiffres: 1384
  1
 333
44444
8888888

i am able to see output.

I never saw a method returning its own class object? any other similar use cases or links or examples where method returns its own class object? it looks bit odd as i am familiar with returning String int etc mostly
 
Knute Snortum
Sheriff
Posts: 7126
185
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sai rama krishna wrote:I never saw a method returning its own class object? any other similar use cases or links or examples where method returns its own class object? it looks bit odd as i am familiar with returning String int etc mostly


The builder pattern uses this technique.  I use this pattern a lot.  It's great for creating objects that have many fields or a lot of optional parameters.
 
sai rama krishna
Ranch Hand
Posts: 1004
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sure. I will do that



in above code what is ::

i never seen such operator? please advise
 
sai rama krishna
Ranch Hand
Posts: 1004
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
instead of  scanner.nextLine() can i use scanner.next()?
what is meaning of nextLine() method here?
i am entering input like
23192
etc in the same line right not next line?
 
Campbell Ritchie
Marshal
Posts: 80634
471
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The nextLine method returns from the current position of the cursor to the next instance of line end characters.
The next method returns the net token whether it is on the same line or not.
For the :: operator go through the Java™ Tutorials section and look for the part called method references.
 
Knute Snortum
Sheriff
Posts: 7126
185
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Scanner#nextLine() returns everything up to and including the text separator.  Scanner#next() returns the next token.

In practical terms, next() is the missing nextString() function of Scanner -- with one limitation: it cannot return an empty String.  If you can live with that, only use next().  If you need to be able to get an empty String, things get complicated.  
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic