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 8 of 8

Thread: What's wrong with this? I want to create a fun code that repeats a phrase I enter a certain number of times that I define in the program

  1. #1
    Junior Member
    Join Date
    Feb 2014
    Posts
    4
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Post What's wrong with this? I want to create a fun code that repeats a phrase I enter a certain number of times that I define in the program

    package loopquestion;

    import java.util.Scanner;

    public class LoopQuestion {
    public static void main(String[] args)
    {
    {
    Scanner input = new Scanner (System.in);
    String z;
    System.out.println("What is your name?");

    z = input.nextLine();

    String y;
    System.out.println("What is your phrase?");

    y = input.nextLine();

    String x;
    System.out.println("Please input how many times you want '" + y + "' to be repeated, lord " + z + ": ");


    System.out.println(y * x);
    }

    }
    }



    //Sorry if i did anything wrong, first time on the site :D :o


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

    Default Re: What's wrong with this? I want to create a fun code that repeats a phrase I enter a certain number of times that I define in the program

    Welcome! Please read this topic to learn how to post code correctly and other useful info for new members.

    Please ask specific questions. Describe the program's existing behavior, the desired behavior, and post errors and sample runs that show the problem(s) you want help with, both copied and pasted from exactly as they appear at your end.

  3. The Following User Says Thank You to GregBrannon For This Useful Post:

    chris123 (February 10th, 2014)

  4. #3
    Junior Member
    Join Date
    Feb 2014
    Posts
    4
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: What's wrong with this? I want to create a fun code that repeats a phrase I enter a certain number of times that I define in the program

    >>It asks me my name
    >>it asks what phrase i want repeated
    >>it asks how many times i want to repeat it
    >>it repeats the phrase
    Thanks!

    --- Update ---

    Here it is:
    package loopquestion;
     
    import java.util.Scanner;
     
    public class LoopQuestion {
    public static void main(String[] args) 
    {
    {
    Scanner input = new Scanner (System.in);
    String z;
    System.out.println("What is your name?");
     
    z = input.nextLine();
     
    String y;
    System.out.println("What is your phrase?");
     
    y = input.nextLine();
     
    String x;
    System.out.println("Please input how many times you want '" + y + "' to be repeated, lord " + z + ": ");
     
     
    System.out.println(y * x);
    }
     
    }

  5. #4
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: What's wrong with this? I want to create a fun code that repeats a phrase I enter a certain number of times that I define in the program

    Quote Originally Posted by chris123 View Post
    >>It asks me my name
    And a nextLine() as you have done is ok.

    Quote Originally Posted by chris123 View Post
    >>it asks what phrase i want repeated
    And a nextLine() as you have done is ok.

    Quote Originally Posted by chris123 View Post
    >>it asks how many times i want to repeat it
    You need an int value here, not a String. You can get an int directly from Scanner or you can get another String and then "parse" it into an int (see parseInt method of Integer). At your choice.

    Quote Originally Posted by chris123 View Post
    >>it repeats the phrase
    You need to do a "loop". If you don't know what are loops .... please, study, starting for example at Control Flow Statements (The Java™ Tutorials > Learning the Java Language > Language Basics)


    And please, give "good" names to variables ..... not x y etc... that are very "ugly".
    Andrea, www.andbin.netSCJP 5 (91%) – SCWCD 5 (94%)

    Useful links for Java beginnersMy new project Java Examples on Google Code

  6. The Following User Says Thank You to andbin For This Useful Post:

    chris123 (February 9th, 2014)

  7. #5
    Junior Member
    Join Date
    Feb 2014
    Posts
    4
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: What's wrong with this? I want to create a fun code that repeats a phrase I enter a certain number of times that I define in the program

    Thanks a lot!!!

    --- Update ---

    Thanks a lot!!!
    *EDIT*
    What can I use instead of x,y,z?

  8. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,163
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: What's wrong with this? I want to create a fun code that repeats a phrase I enter a certain number of times that I define in the program

    What can I use instead of x,y,z?
    Use a variable name that describes what the variable contains.
    If you don't understand my answer, don't ignore it, ask a question.

  9. The Following User Says Thank You to Norm For This Useful Post:

    chris123 (February 10th, 2014)

  10. #7
    Junior Member
    Join Date
    Feb 2014
    Posts
    4
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: What's wrong with this? I want to create a fun code that repeats a phrase I enter a certain number of times that I define in the program

    Thanks, one last question, should i use a ForLoop, or a normal one?

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

    Default Re: What's wrong with this? I want to create a fun code that repeats a phrase I enter a certain number of times that I define in the program

    A for loop is a normal loop. When you know how many times the loop should execute, the for loop is the natural choice. If looping is dependent on the results of the loop body, then a do/while or while loop might be more appropriate. The do/while assures the body of the loop will execute at least once. The while loop does not give the same guarantee.

  12. The Following User Says Thank You to GregBrannon For This Useful Post:

    chris123 (February 10th, 2014)

Similar Threads

  1. Replies: 1
    Last Post: February 27th, 2013, 02:32 AM
  2. Replies: 10
    Last Post: November 8th, 2012, 06:29 AM
  3. Replies: 2
    Last Post: November 7th, 2012, 10:45 PM
  4. How To Ask the User to Enter Another Number Using the Number 1
    By Pettsa in forum Object Oriented Programming
    Replies: 6
    Last Post: May 3rd, 2012, 03:44 AM
  5. can Java create a user define type of classes?
    By LearningJava in forum Java Theory & Questions
    Replies: 5
    Last Post: September 30th, 2011, 10:49 AM

Tags for this Thread