• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JAVA StringBuffer line breaking

 
Karl Vaicek
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Everyone,

First I'd like to say that this is my first JAVA program, so question might look stupid... Sorry for that.

I need to make a program in which 'user' inserts String (as a StringBuffer) and one integer(int) value. Then program outputs the same string, but with line brakes after Int characters, as many times, as it need to. For example, if 'user' inserts:


>How do I look now? Thank you very much!
>4

the answer needs to look like:

>How
>do I
> loo
>k no
>w? T
>hank
> you
> ver
>y mu
>ch!

(example is horrible, I know

Thank you, for any help you can give me!
 
Karl Vaicek
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By the way, I've already done half of the program:



I Just need to do line breaking(:
 
Campbell Ritchie
Sheriff
Pie
Posts: 50196
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Find the System class, and get the property from its getProperty method which I think is called "line.separator". If you can't find that, get the Properties class an iterate through all the properties and find its correct name. Then you can use the insert method. If you want to break it into equal-length strings it may be easier to go backwards from the end of the string. You can use a for-loop counting backwards with i -= 3.

I am not sure what happens if your String has a number of characters dividing exactly by 3 if you want to break into 3-letter Strings. You might end up with appending rather than inserting.

And use StringBuilder in preference to StringBuffer.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50196
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Karl Vaicek wrote:this is my first JAVA program, so question might look stupid...
That looks much more difficult than most people's first ever program.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50196
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to JavaRanch
 
Karl Vaicek
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you! I made a loop, but now I have another problem. The Code looks like this:



And, for example, I enter
>abcdef
>2

But then I get this error message:

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String ind
ex out of range: 17
at java.lang.AbstractStringBuilder.insert(AbstractStringBuilder.java:979
)
at java.lang.StringBuffer.insert(StringBuffer.java:446)
at StringReader.main(StringReader.java:50)


Any suggestions? :/

P.S. That's not my first program, I'm computer science student (2 course), but that's my first program with JAVA
 
Shinelin Samuel
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

You had an additional iteration within the for loop which lead to IndexOutOfBoundException.

Change the loop like below, should work,

 
Rob Spoor
Sheriff
Pie
Posts: 20665
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Karl Vaicek wrote:

Scanner has explicit methods for checking if the input is an integer, without having to catch the NumberFormatException:


What's wrong with "-\n-" ?
 
Karl Vaicek
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for advice, but the problem remains... althought now I can put more characters.

>abcdefg

But when I put:

>abcdefgh


Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String ind
ex out of range: 17
at java.lang.AbstractStringBuilder.insert(AbstractStringBuilder.java:979
)
at java.lang.StringBuffer.insert(StringBuffer.java:446)
at StringReader.main(StringReader.java:50)


That's so annoying
 
Karl Vaicek
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote:

What's wrong with "-\n-" ?


If I'll enter "abcdefg" "2"
It will output ab-\n-cd-\n-ef-\n-g
 
Shinelin Samuel
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is a logical error you get, when you try to change Input values,

Just to avoid the runtime exception (IndexOutOfBoundsException), have the below sanity check.


You may not get desired results (may have to fine tune the logic), of course this would avoid this exception.
 
Karl Vaicek
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
BINGO!

I'm grateful for all of you!

I have been solving this problem for about 3 hours, and now it's over!

THANK YOU
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic