1

I am trying to make a loop that prints "*" as a string a certain number of times, but I can't get it to work. Everything in the main method was given to me and must be used. Everything else I added and I don't know if I am on the right track or not. The end result is suppposed to print "*" seven times horizontally. Then each time it adds a "*", adds one to count, and compares to see if count is greater than or equal to the value I set. Then if it is true it ends the loop and if not it repeats the loop until true. I just don't know how express this in code.

    public class LoopPractice
    {
        public String ast = "*";    

        public static void main(String[] args)
        {

            LoopPractice lp = new LoopPractice();
            System.out.println(lp.getAstWhile(7));
        }   

        public String getAstWhile()  
        {
            int count = 0;
            while (count <= 6)
            {
                System.out.print(count++);
            }
            return ast;
        } 
    }
1
  • When you call lp.getAstWhile(7) in the println() function, you're passing an argument but getAstWhile() doesn't have any parameters. Commented Oct 18, 2015 at 23:24

2 Answers 2

1

You are passing a value "7" to a function that accepts no values, call lp.getAstWhile(); instead of lp.getAstWhile(7);

public String getAstWhile(int maxValue)
{ 
     int count = 0; 
     while (count < maxValue) 
     { 
         system.out.print(count++); 
      } 
}
Sign up to request clarification or add additional context in comments.

3 Comments

Actually if you want it to print 7 times horizontally, change your getAstwhile to the following public String getAstWhile(int maxValue){ int count = 0; while (count < maxValue) { system.out.print(count++); } }
If you want to add to your post, edit it. Also, uses code blocks when typing code.
No even in the comments. Surround code you want to be part of a sentence with back-ticks (to the left of the "1" key, same key as tilde).
0

Use a StringBuffer.

public String getAstWhile()  
    {
        StringBuffer buf = new StringBuffer();
        int count = 0;
        while (count <= 6)
        {
            buf.append('*');
            count++;
        } 
        return buf.toString();
    } 

getAstWhile was not returning a String in your original code.

API documentation: http://docs.oracle.com/javase/7/docs/api/java/lang/StringBuffer.html Tutorial on StringBuffer: http://www.tutorialspoint.com/java/java_string_buffer.htm

4 Comments

I'm not sure, as a student, this OP will understand the code you provided. Plus, I'm sure the instructor will notice the degree of change.
Surely the student can look up the StringBuffer class. This is the correct and recommended way to build a string in Java.
I suggestion you provide links and comments to help the OP.
I added some references. I would however like to point out that if someone introduces a new class, concept etc. to a good student, the first thing that student should be doing is googling for more information. Spoonfeeding the student, especially on such a trivial home-work style quesiton is not a good idea.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.