0

I was trying to solve this excercice but got little bug. I have to reformat this text with spaces and length of every lines must be not exceed 60 characters.

"Java was originally developed by James Gosling at Sun Microsystems (which has since been acquired by Oracle) and released in 1995 as a core component of Sun Microsystems' Java platform."

There is my code:

static String text;
public String reformatLines(String text) {  
    if(text == null || text.isEmpty()); 
       return text;
}   
public static void main(String[] args) {        
    String text = "Java    was      originally developed   by    James   Gosling at Sun Microsystems (which has since been acquired by Oracle) and released in 1995 as a core component of Sun Microsystems' Java platform.";
    String str = text.replaceAll("[\n]{2,}", " ");
    int length = 60;
    String jump = new String();
    while(text.length()>length){
        int jp = str.lastIndexOf(" ", length);
        jump = jump.concat(str.substring(0, jp));
        jump = jump.concat("\n");
        str = str.substring(jp, str.length());
    }   
}   

}

I actually, have no idea what to do...

studyjava
  • 3
  • 4
  • 2
    Does this answer your question? [Java how to replace 2 or more spaces with single space in string and delete leading and trailing spaces](https://stackoverflow.com/questions/2932392/java-how-to-replace-2-or-more-spaces-with-single-space-in-string-and-delete-lead) – OH GOD SPIDERS Jul 14 '21 at 12:07

3 Answers3

1

Replace whitespace with a single space

text.replaceAll("\\s+", " ");

Sean AH
  • 486
  • 4
  • 8
1
text = text.replaceAll("\\s{2,}", " ").replaceAll("(.{1,60}).*", "$1");

would be a quick approach

g00se
  • 3,207
  • 2
  • 5
  • 9
  • Ok, i've run your code, in sysout it looks like "Java was originally developed by James Gosling at Sun Micros" and nothing else – studyjava Jul 14 '21 at 14:28
  • Yes, because your question title contains >and 60 characters< so the output stops at… 60 characters – g00se Jul 14 '21 at 15:16
  • The excercise is to format this text in next way: get full text without whitespace and length of every lines should not exceed 60 characters. Maybe i did mistake in theme of this question :? – studyjava Jul 14 '21 at 15:21
  • I was going by the question title and the fact that the string in your code is one line – g00se Jul 14 '21 at 15:47
  • sorry... i really interested about this excercice – studyjava Jul 14 '21 at 16:09
  • I think what you're being asked might be to 'wrap' the text lines at 60 characters. Since the exercise text invokes the history of Java, you could not do much better than to look at one of its [earliest classes](https://docs.oracle.com/javase/10/docs/api/java/text/BreakIterator.html), which would be a good candidate for solving a word wrapping problem. – g00se Jul 14 '21 at 16:29
0

How about splitting the text on whitespace and rebuilding it by inserting single spaces and newlines to meet the line length constraint?

static String format(String text, int length)
{
    StringBuilder b = new StringBuilder();
    
    int curr = 0;
    for (String word : text.split("\\s+"))
    {
        int nextLen = curr + word.length();
        
        if(curr > 0) 
            nextLen += 1;       
        
        if (nextLen > length)
        {
            b.append(System.getProperty("line.separator"));
            curr = 0;
        }

        if (curr > 0)
        {
            b.append(" ");
            curr += 1;
        }

        b.append(word);
        curr += word.length();
    }
    b.append(System.getProperty("line.separator"));
    
    return b.toString();
}

Test:

String text = "Java    was      originally developed   by    James   Gosling at Sun Microsystems (which has since been acquired by Oracle) and released in 1995 as a core component of Sun Microsystems' Java platform.";
System.out.print(format(text, 60));

Output:

Java was originally developed by James Gosling at Sun
Microsystems (which has since been acquired by Oracle) and
released in 1995 as a core component of Sun Microsystems'
Java platform.
RaffleBuffle
  • 5,396
  • 1
  • 9
  • 16