-2

A method called printIndented() that takes a String, an integer and a boolean. The integer represents the size of an indentation from the left margin in spaces, so the method should first p rint that many spaces. Then the string should be printed. Then, if the boolean is true, a newline should be printed. So for example, printIndented(3, "Hello", false) would print:

˽˽˽
Hello
...with no newline at the end.

I'm stuck with this. my version is incomplete:

int printIndented(int size, String word, boolean n) {
    String spaces = ("");

    print(spaces);

    print(word);
    if(n == true)  println("");    
    else 
    if(n == false)  print("");    
    return(size);
Emii Khaos
  • 9,983
  • 3
  • 34
  • 57
user2771350
  • 37
  • 1
  • 7
  • 2
    I would take a look at [the for statement](http://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html) for more details, but essentially, you want to loop `size` times and print that many spaces... – MadProgrammer Sep 12 '13 at 06:45
  • 2
    You're not printing spaces, you're just printing empty string `""`. Add some spaces between the quotes. – Jesper Sep 12 '13 at 06:45
  • 3
    @RoyDictus I believe the homework tag has being depreciated – MadProgrammer Sep 12 '13 at 06:45
  • Give some thought you will do it.... – Jayaram Sep 12 '13 at 06:47
  • Notice that when a boolean isn't true, is false, the second check is completely unnecessary. You can use if(n){...}else{...} instead – Amin Abu-Taleb Sep 12 '13 at 06:48
  • For many different ways to print the indentation spaces, see http://stackoverflow.com/questions/1235179/simple-way-to-repeat-a-string-in-java – Gabe Sep 12 '13 at 06:51
  • i do not find any need to return int in printIndented it can be void printIndented(int size, String word, boolean newline) {..} – upog Sep 12 '13 at 06:53

5 Answers5

1

This is my version:

void printIndented(int size, String word, boolean n)
{
    print(StringUtils.repeat(" ", size));

    print(word);
    if(n)
       println("");    
}

The function, as I see it, doesn't need to return anything. Just returning the size parameter tells the caller nothing new.

You can find the StringUtils class in the Apache Commons Lang library. See http://commons.apache.org/lang/api-2.5/org/apache/commons/lang/StringUtils.html.

Without StringUtils.repeat, you can replace the first line by:

for(int i = 0; i < size; i++) print(" ");
Roy Dictus
  • 32,551
  • 8
  • 60
  • 76
1

Since it's a homework, I will not show you a full solution, but I'll try to guide you.

You should print space size times, you are not doing that. Instead, you're printing "" (Which is not a space) one time.

Tips:

  • Use a for loop - Print space (" ") size times (String space = " "; Also note that it should be named space instead of spaces since it represents only one space.
  • A new line is represented as \n - You might also want to see this too.
  • When you check if a boolean is true, it's better to do if(n) instead of if(n == true).
  • The parenthesis around the return statement are redundant, you can remove them.
Maroun
  • 94,125
  • 30
  • 188
  • 241
0

change

String spaces = (""); 

to

String spaces = " ";

and Make your print(spaces) in a loop so that it prints the spaces equlas to the integer size.

  for(int i=0;i<size;i++){
         print(spaces);
    }
Prabhaker A
  • 8,317
  • 1
  • 18
  • 24
0
void printIndented(int i, String s, boolean b) {
    for(int j = 0; j < i; j++){
        System.out.print(", ");//Comma is to see how many spaces in output
    }
    System.out.print(s);
    if(b){
        System.out.println();
        System.out.print("NewLine");//to see that new line was added
    }

}
Sergo
  • 1
  • 1
0

Loops? We don't need no stinkin' loops!

public void printIndented(int size, String word, boolean n) {
    System.out.printf("%" + size + "s%s" + (n ? "%n" : ""), "", word);
}

* ducks *

Oak
  • 26,231
  • 8
  • 93
  • 152