Results 1 to 7 of 7
  1. #1
    Alex_ is offline Member
    Join Date
    Jan 2012
    Posts
    3
    Rep Power
    0

    Default Displaying a message five times

    Is there any key-word that multiplies the message or do i have to write System.out.println("message") five times?
    Thanks.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Displaying a message five times

    I think that you're stuck with using a for loop. Unless I'm mistaken, I don't think that core Java has that bit of syntactic sugar.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,086
    Rep Power
    20

    Default Re: Displaying a message five times

    You could loop over the Sysout call 5 times rather than writing the line 5 times.

    ETA: Ninja'd by a matter of seconds!

  4. #4
    Alex_ is offline Member
    Join Date
    Jan 2012
    Posts
    3
    Rep Power
    0

    Default Re: Displaying a message five times

    I don't think it's about loops.I'm just starting to learn Java and the exercise is from the chapter 1 of a book(Introduction to Java Programming-Liang).In chapter 1 I didn't read about loops.
    here's the text: 'Write a program that displays Welcome to Java 5 times'

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Displaying a message five times

    Quote Originally Posted by Alex_ View Post
    I don't think it's about loops.I'm just starting to learn Java and the exercise is from the chapter 1 of a book(Introduction to Java Programming-Liang).In chapter 1 I didn't read about loops.
    here's the text: 'Write a program that displays Welcome to Java 5 times'
    OK, then you are the loop, and this can easily be accomplished by having 5 System.out.println(...) statements.

  6. #6
    Alex_ is offline Member
    Join Date
    Jan 2012
    Posts
    3
    Rep Power
    0

    Default Re: Displaying a message five times

    tought so,but I also thought it was some trick I didnt know.
    tnx.

  7. #7
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,651
    Blog Entries
    7
    Rep Power
    21

    Default Re: Displaying a message five times

    Other languages can 'multiply' a String by an int (Python comes to mind) but Java is not one of them; if you don't know how to implement a (simple) loop, you either have to print your message five times (your editor can do the boring job), or you have to apply a little trick:

    Java Code:
    String message= <your message>;
    String message2= message+message;
    String message4= message2+message2;
    String message5= message+message4;
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Repeating too many times.
    By trishtren in forum New To Java
    Replies: 4
    Last Post: 11-11-2011, 12:20 AM
  2. Displaying error message if no text found
    By jdg951 in forum New To Java
    Replies: 3
    Last Post: 01-17-2011, 10:52 PM
  3. Replies: 6
    Last Post: 06-04-2010, 08:43 PM
  4. JComboBox displaying variable two times, why?
    By ecliptical in forum New To Java
    Replies: 6
    Last Post: 01-24-2010, 06:09 PM
  5. Urgent-Imp-Displaying message with respect to system time
    By garinapavan in forum New To Java
    Replies: 1
    Last Post: 08-03-2007, 02:17 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •