0

I need to write a program that let's the user write 3 words in the console, then the program reprints those 3 words (one in each line) but also fills out the remaining spaces in each line with dots (".") so the total number of characters in each lines becomes a total of 30 characters.

Example:

Input:

Hello 
Me 
Overflow

Output:

.........................Hello
............................Me
......................Overflow

This is the code that I currently have which generates an error. I have been given the code (at the bottom) as part of my assignment and need to write the repeatChar method to make it work.

The first thing I did was to add the following commands in the code, in order to save the 3 words into the array threeWord.

threeWord[1] = wordOne;
threeWord[2] = wordTwo;
threeWord[3] = wordThree;

Next, I had to write the method repeatChar, and I decided to use a for-loop to make it repeat dots for each individual line, but I'm having a hard time making it fit with the rest of the code. Any guidance would be much appreciated, thanks.

import java.util.*;

public class FillDots {
    private static int LineLength = 30;
    public static void main(String[] arg) {
        String[] threeWord = new String [3]; // Defines 3 locations to place strings in the array "threeWord"
        Scanner console = new Scanner(System.in);
        System.out.println("Type in three words:");
        String wordOne = console.next();
        threeWord[1] = wordOne; // Saves first word to array "threeWord"
        String wordTwo = console.next();
        threeWord[2] = wordTwo; // Saves second word to array "threeWord"
        String wordThree = console.next();
        threeWord[3] = wordThree; // Saves third word to array "threeWord"
        for(int i = 0; i < threeWord.length; i++) {
            System.out.println(repeatChar('.', LineLength - threeWord[i].length()) + threeWord[i]);
        }
    }
    public static String repeatChar(String LineLength) {
        for(int j = 0; j < LineLength; j++) {
            System.out.print(".");
        }
    }
}
4
  • 1
    Array indices start at 0 threeWord[0], threeWord[1], threeWord[2] Commented Sep 27, 2018 at 15:09
  • @user7 Good catch, thanks! :) Commented Sep 27, 2018 at 15:11
  • If you just had the repeat method build a string of periods, that would work. However, you're calling a method that prints out with no String return in another System::println; Commented Sep 27, 2018 at 15:18
  • When you ask a question in the future, please be sure to include any errors that you get from your code and indicate which lines cause the errors. Commented Sep 27, 2018 at 15:35

2 Answers 2

1

Besides the index starts from 0, you need return the dots in the repeatChar method:

public static String repeatChar(char repeatChar, int repeatTimes) {
    String result = "";
    for(int j = 0; j < repeatTimes; j++) {
        result += repeatChar;
    }
    return result;
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you. Is there any way that I can keep the code for the loop (with the variable i) as it is, and make changes to the repeatChar method? I'm having a hard time understanding the '.', part of the method in the loop. I'm guessing I shouldn't be printing "." in my repeatChar method?
That works perfectly! Thank you. Is the command String result = ""; basically telling Java to start with "zero dots", and then the result += repeatChar; basically adds one dot character every time it is needed? And how does Java know the length of repeatTime (basically how does it know how many iterations to do in the loop)?
You pass repeatTimes to it and use repeatTimes in the for loop. That's how it works.
0

You can use existing library for doing padding

for(String temp:threeWord) 
  system.out.println(org.apache.commons.lang.StringUtils.leftPad(temp, 10, ".") ); 

this might simplify your code

Comments

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.