-2

I want each line to split off after 20 characters have gone by, but I want it to split off at the nearest space so the sentence has only whole words.

The answerer said to do this:

str="some long string"
startPos=0, endPos=0
while (startPos < str.length) {
    determine endPos
    print substring from startPos to endPos
    move startPos to endPos+1 // this is the part I am confused about.
}

The code I wrote was this:

System.out.println("Please input a word: ");
            Scanner stringScanner = new Scanner(System.in);
            String input = stringScanner.nextLine();
            
            int startPos = 0;
            int endPos = 0;
    
            while (startPos < input.length()) {
                endPos = 20;
                 System.out.println(input.substring(startPos, endPos));
                 startPos = endPos + 1; //This is the part I am confused about
            }

I am not sure what the answerer means by move startPos to endPos + 1. Any answers would helo, thanks.

Edit:

sorry I forgot to post what my code does:

The program right now give me an error that says this:

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: begin 21, end 20, length 32
    

    at java.base/java.lang.String.checkBoundsBeginEnd(String.java:4602)
        at java.base/java.lang.String.substring(String.java:2705)
        at StarBorder.StarBorder.main(StarBorder.java:18)

This is what I want it to do:

Hello there, I am
doing some coding, 
I need some help
  • 2
    I recommend that you link to the previous answer and even copy/paste the pseudocode in your new question. – Code-Apprentice Mar 08 '23 at 00:45
  • With that said and after looking at the previous answer, I think your interpretation is as good as any. Does this code work? If not, what happens and what do you want it to do instead? – Code-Apprentice Mar 08 '23 at 00:46
  • 2
    Don't do this. Instead, use a comment to ask the person who wrote the answer to clarify the answer. – Stephen C Mar 08 '23 at 00:47
  • @StephenC To be fair, the OP has translated the pseudocode they were given into an actual block of code. Posting that in a comment would be impossible. – Code-Apprentice Mar 08 '23 at 00:48
  • When your program doesn't quite do what you want to do, you will need to figure out why using debugging techniques. I recommend [this article](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) for some tips to get you started. – Code-Apprentice Mar 08 '23 at 00:51
  • 1
    Thanks I will read that – Nick Slack Mar 08 '23 at 00:52
  • 1
    The problem I see is that "determine endPos" in the psudeo code is doing a lot of work. It will likely require a loop of its own. Breaking this off into it's own method will help I think. – markspace Mar 08 '23 at 00:54
  • @NickSlack If you still need help after you've done some debugging, please [edit] your question to show a [mcve]. I had to add the `class Main` and `public void main()` declarations to get it to work. You should include these so we can just copy/paste it and run it ourselves. Also, I get an error with your input different from what you are asking. but I think it's related to what you are trying to figure out. You can see what I did [here](https://replit.com/@codeguru/HonoredArtisticBlogclient) along with the error. – Code-Apprentice Mar 08 '23 at 00:56
  • @markspace For a first attempt, I would recommend getting X number of characters for "determine endPost". But yah, the whole word wrapping on spaces will take more logic. – Code-Apprentice Mar 08 '23 at 00:58
  • The part that the respondent omitted was backtracking from the 20 character mark to find a space to break on. You could use the `lastIndexOf` method to do that. You should also think about what should happen if there's no space character within a block of 20 characters (ie, someone used a 21-letter word). – Dawood ibn Kareem Mar 08 '23 at 01:05
  • Also, the FIRST time you asked this question, it was closed as a duplicate of [somebody else's question](https://stackoverflow.com/q/12296374/1081110). Have you looked at the answers to that duplicate question? (We don't close questions as duplicates because we want them to become triplicates or quadruplicates). – Dawood ibn Kareem Mar 08 '23 at 01:33

1 Answers1

-2

In your code, you try to split a string from the 21st char to the 20th char and it will not be possible. you should set endPos. I figured out that you want the first space after the 20th char. Java has a built-in indexOf method that you can get the index of a char and you can set the starting limit. for example:

String myStr = "Hello planet earth, you are a great planet.";
System.out.println(myStr.indexOf("e", 5));

/// Output 10 

you can use:

endPos = input.indexOf(" ", 20);
Mr.Ziri
  • 35
  • 4