Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Improve this?  RSS feed

 
Stephen Foy
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anyway i can improve this code? Make it more robust and shorter.

 
Scott Selikoff
author
Bartender
Posts: 4093
21
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is this a homework problem or real code from somewhere?
 
Stephen Foy
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Scott Selikoff:
Is this a homework problem or real code from somewhere?


Its my complete assignment, its working, im just looking for tips on how i could make it more robust etc.
 
Gerardo Tasistro
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Comments>

- call int minustwo as minusTwo, just looks better and more in standard

- that if ( if (number <=2 && number >=1) { line3 = line4;}) executes on 1 & 2
could be written as if (number ==2 || number ==1) { line3 = line4;}, IMHO it looks clearer

- this
System.out.println("\n"+ number +line1);
System.out.println(number + line2);
System.out.println(line3);
System.out.println();

could be
System.out.println("\n"+ number +line1+"\n"+number + line2+"\n"+line3+"\n");
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well...

You have a for loop that isn't doing anything. You could either do:


OR
 
Brian Cole
Author
Ranch Hand
Posts: 949
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Gerardo Tasistro:
- this
System.out.println("\n"+ number +line1);
System.out.println(number + line2);
System.out.println(line3);
System.out.println();

could be
System.out.println("\n"+ number +line1+"\n"+number + line2+"\n"+line3+"\n");


It could be, but it shouldn't be. Five calls
to println() is the preferred way, but if you
really want to do it in a single call I guess
you could do it like this:

String sep = System.getProperty("line.separator");
System.out.println(sep + number + line1 + sep + number + line2 + sep + line3 + sep);

or:

System.out.print(sep + number + line1 + sep + number + line2 + sep + line3 + sep + sep); // make final newline explicit

There's a good FAQ answer out there somewhere that touches
on this, but I couldn't find it when I searched briefly.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!