0

I m writing a function to format a string input. The function is to make the text output left indented, with a limit to the number of characters in a line. if there are excess whitespaces,' ', between words; i skip the excess whitespace and only put one whitespace' '. if the word is too long and will go over the character per line limit, i insert whitespaces until the limit and start a new line with that word in the newline.

i am unable to create the padding section of the code. which is the part which checks whether the word will go over the limit and to start a newline with the word in the newline.

as part of the assignment requirements, i am only allowed to use charAt() and length methods on a String

 void runApp(){
     String text = "          Knowing You Jesus        " +
                "                                         Graham Kendrick " +
                "All I once held dear,     built my life upon " +
                "All this world reveres,    and wars to own " +
                "All I once thought gain      I have counted loss " +
                "Spent and worthless now, compared         to this " +
                "Knowing you,       Jesus " +
                "Knowing you, there is          no greater thing " +
                "You're my all, you're the best " +
                "You're my joy, my righteousness " +
                "And I love you, Lord " +
                "Now my heart's desire is to know you more " +
                "To be found in you and known as yours " +
                "To possess by faith what I could not earn " +
                "All-surpassing gift of righteousness " +
                "Oh, to know the    power of your risen life " +
                "And to know You in Your sufferings " +
                "To become like you in           your death, my Lord " +
                "So with you to         live and never die " +
                "Source:  Musixmatch " +
                "Songwriters:  Tim Hughes  /  Ben Cantelon   ";

    //limit = 60
        System.out.println(" `123456789012345678901234567890123456789012345678901234567890");`
        System.out.println(leftTextFormat(text, 60));
        System.out.println();
    }


// This prints out everything left indented, but does not pad.
String leftTextFormat(String text, int limit){
    String formattedText = "";
    int charCount = 0;
    formattedText = formattedText+"[";
    for (int i=0; i<text.length(); i++){
        if (charCount%limit ==0){
            if (text.charAt(i) == ' '){
                continue;
            }else if (text.charAt(i) != ' ' ){
                formattedText = formattedText+text.charAt(i);
                charCount++;
            }
        }else if (charCount%limit != 0){
            if (text.charAt(i) == ' '){
                if (text.charAt(i-1) != ' '){
                    formattedText = formattedText+text.charAt(i);
                    charCount++;
                }else{
                    continue;
                }
            }else if (text.charAt(i) != ' '){
                formattedText = formattedText+text.charAt(i);
                charCount++;
            }
        }
        if (charCount%limit ==0 && charCount!=0){
            formattedText = formattedText+"]\n[";
        }
    }
    return formattedText;
}

The expected output is this: https://drive.google.com/file/d/1uYXtSBo37sFnpwJeBGtjF0MFYNngtZXv/view?usp=sharing

what i managed to do is this: https://drive.google.com/file/d/102zNMe4JhaO2IUoOPCS5GSZ02Pq9aXwX/view?usp=sharing

 123456789012345678901234567890123456789012345678901234567890
[Knowing You Jesus Graham Kendrick All I once held dear, buil]
[t my life upon All this world reveres, and wars to own All I]
[once thought gain I have counted loss Spent and worthless no]
[w, compared to this Knowing you, Jesus Knowing you, there is]
[no greater thing You're my all, you're the best You're my jo]
[y, my righteousness And I love you, Lord Now my heart's desi]
[re is to know you more To be found in you and known as yours]
[To possess by faith what I could not earn All-surpassing gif]
[t of righteousness Oh, to know the power of your risen life ]
[And to know You in Your sufferings To become like you in you]
[r death, my Lord So with you to live and never die Source: M]
[usixmatch Songwriters: Tim Hughes / Ben Cantelon 
2
  • 1
    google is blocked (on my network) and external site may go offline, change content, ... , please include the text in question. Or is the expected output an image? (BTW you are using concatenation in strings, not only length and charAt - would be impossible otherwise) Commented Oct 19, 2019 at 7:36
  • it is expected to return the formatted string. the concatenation of the strings is provided in the exercise and as such is not considered. Commented Oct 19, 2019 at 7:43

1 Answer 1

3

Ok attempt, but your problem is that you only adding characters and never actually establishing if those characters make up words and if those words, which you are adding, are going to go over your "limit". Hence you have no idea when a word will actually cause you to go over your imposed limit and subsequently you are make no attempt to pad your line when you are potentially going to go over that limit.

Essentially you need to work word for word and line by line. Each time you've established a word, check that the length of that word plus the length of your current line is not greater than your limit. If it's greater don't concatenate it to your current line, rather pad the line to your limit and start a new line.

public String formattedLine(String singleLine, int limit) {
    int padding = limit - singleLine.length();
    String pad ="";
    for(int j = 0; j < padding; j ++) {
        pad += " ";
    }
    return  "[" + singleLine + pad + "]\n";
}

public String leftTextFormat(String text, int limit) {
    String word = "";
    String singleLine = "";
    String formattedText = "";
    for(int i=0; i<text.length(); i++) {
        if (text.charAt(i) != ' ') {
            word = word + text.charAt(i);
        } else {
            if(word.length() > 0) {
                if(singleLine.length() + word.length() >= limit) {
                  formattedText = formattedText + formattedLine(singleLine, limit);
                  singleLine = "";
                }
                if(singleLine.length() == 0) {
                    singleLine = word;
                } else {
                    singleLine = singleLine + " " + word;
                }
                word = "";
            }
        }
    }
    formattedText = formattedText + formattedLine(singleLine, limit);
    return formattedText;
}
Sign up to request clarification or add additional context in comments.

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.