-6

Say I have allot of code like..

    "
    System.out.println("This is code");
    System.out.println("This is code");
    System.out.println("This is code");
    System.out.println("This is code");
    "

This same code keeps coming up in my program can I shorten it all into one line I can just use to call it in the future??

user2314737
  • 27,088
  • 20
  • 102
  • 114
Ammar
  • 19
  • 1
  • 1
  • 2
  • 6
    Look into _methods_. – Sotirios Delimanolis Jan 20 '14 at 21:36
  • 5
    .... and loops. Shoot, just read the first 2 chapters of pretty much any Java text. In fact this question suggests that you are suffering from the dreaded "Java textbook deficiency syndrome". This unfortunately isn't treatable on StackOverflow or other similar sites but luckily responds well to buying and reading a Java book. – Hovercraft Full Of Eels Jan 20 '14 at 21:37
  • .... .... and concatination of strings :) just wanted to add something.. – Orel Eraki Jan 20 '14 at 21:37

2 Answers2

2

Why not use a for loop for that, and a method while you're at it:

public void doStuff()
{
    for(int i = 0; i < 20; i++){
        System.out.println("This is code");
    }
}
pcnThird
  • 2,362
  • 2
  • 19
  • 33
  • That should be `doStuff()`. It's up to you, of course, but I'd recommend following at least the [basic naming conventions](https://en.wikipedia.org/wiki/Naming_convention_%28programming%29#Java). – Tobias Jan 20 '14 at 21:40
0
public void printStuff()
{
   print things inside a loop
}
z atef
  • 7,138
  • 3
  • 55
  • 50