Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: printStrings

  1. #1
    Junior Member
    Join Date
    Oct 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default printStrings

    I'm having a hard time with this problem, this is what I have, but I can not use two integers, I have to use one integer and a string...

    This is the question:

    Write a method called printStrings that accepts a String and a number of repetitions as parameters and prints that String the given number of times. For example, the call:

    printStrings("abc", 5);

    will print the following output:

    abcabcabcabcabc

    This is what I attempted:

    public class printStringsproject {
    public static void printStrings(int abc, int number) {
    for (int i = 1; i <= number; i++) {
    System.out.print("abc");
    }
    }
    public static void main(String[] args) {
    printStrings(1, 5);
    }
    }

  2. #2
    Member
    Join Date
    Aug 2013
    Posts
    95
    Thanks
    3
    Thanked 14 Times in 14 Posts

    Default Re: printStrings

    You need to accept a String as the first parameter in your method
    You have the right idea, but you want something like this

    public static void printStrings(String abc, int number) {
    for (int i = 1; i <= number; i++) {
    System.out.print(abc);
    }
     
    public static void main(String[] args) {
    printStrings("Whatever You want to print 5 times goes here", 5);
    }

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: printStrings

    Welcome to the forum! Please read this topic to learn how to post code in code or highlight tags and other useful info for new members.

    Please post your code correctly using code or highlight tags per the above link.

  4. #4
    Junior Member
    Join Date
    Oct 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: printStrings

    Quote Originally Posted by camel-man View Post
    You need to accept a String as the first parameter in your method
    You have the right idea, but you want something like this

    public static void printStrings(String abc, int number) {
    for (int i = 1; i <= number; i++) {
    System.out.print(abc);
    }
     
    public static void main(String[] args) {
    printStrings("Whatever You want to print 5 times goes here", 5);
    }

    Wow thank you so much for leading me to the right direction....

  5. #5
    Junior Member
    Join Date
    Oct 2014
    Posts
    16
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: printStrings

    In the parameters of printString method u have 2 integers, u should put String abc because its text and not a number. Then you have to do something like:

    System.out.println(abc);
     
    And not:
     
    System.out.println("abc");

    Because if u use "" it will print out abc ony and not the text u want if u want any other text. Also dont use <= because that is less than or equal to, and i think u only want to print it out if its less than 5 so after it printed out the 5th time it stops. Then u can call the method by typing:

    printString("String u wanna print", amount of time);
    Last edited by Resantic; October 4th, 2014 at 05:51 PM.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: printStrings

    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Oct 2014
    Posts
    16
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: printStrings

    Quote Originally Posted by Norm View Post
    I thought if he put the 2 codes next to each other its easy to see whats wrong but i will explain it to him..