Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Beginning Java and the fly likes JAVA StringBuffer line breaking Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "JAVA StringBuffer line breaking" Watch "JAVA StringBuffer line breaking" New topic
Author

JAVA StringBuffer line breaking

Karl Vaicek
Greenhorn

Joined: Mar 01, 2010
Posts: 11
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

Joined: Mar 01, 2010
Posts: 11
By the way, I've already done half of the program:



I Just need to do line breaking(:
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
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

Joined: Oct 13, 2005
Posts: 39436
    
  28
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

Joined: Oct 13, 2005
Posts: 39436
    
  28
And welcome to JavaRanch
Karl Vaicek
Greenhorn

Joined: Mar 01, 2010
Posts: 11
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

Joined: Mar 01, 2010
Posts: 55

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

Change the loop like below, should work,

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19723
    
  20

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-" ?


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Karl Vaicek
Greenhorn

Joined: Mar 01, 2010
Posts: 11
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

Joined: Mar 01, 2010
Posts: 11
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

Joined: Mar 01, 2010
Posts: 55
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

Joined: Mar 01, 2010
Posts: 11
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
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: JAVA StringBuffer line breaking