-3

Input

preBody = The AThe CThe IThe O{

I am trying to get the output as

The A
The C
The I
The O

and ignore a value less than 5(bodyCnt)[ { ]

So far What I did is.

int bodyCnt = 5
int cnt = 0;
String eachBody;
int extra = 0;
int limit = preBody.length();
while(limit >= bodyCnt){
 eachBody = preBody.substring((cnt+extra), (bodyCnt+cnt));
 cnt = eachBody.length();
 extra = 1;
 limit = limit - eachBody.length();
 System.out.println("eachBody : -----"+eachBody);
}

Output

eachBody :----- The A
eachBody :----- The C
eachBody :----- The C
eachBody :----- The C

After 2 nd loop the data are same.

Am I doing anything wrong.

Or Can we approach the same in a better way?

USB
  • 6,019
  • 15
  • 62
  • 93

5 Answers5

2

try this: cnt += eachBody.length(); The problem is that cnt will always be 5 in your code so cnt+extra will always stay 6 after the second run leading to the same substring after the first run since the range is exactly the same.

mhafellner
  • 458
  • 3
  • 9
1

The error is in cnt = eachBody.lenght(). In this way you are always assigning 5 to cnt therefore the substring, after first iteration, gives always prebody.subString(6,11) which is "The C". To resolve it just use cnt += eachBody.lenght().

I would suggest you to take another approach such as the String#split method with a regex (crf Alan Moore answer):

preBody.split("(?<=\\G.{5})")

This will return an array of String each with lenght = 5 (lenght of every "The x" string).

The code becomes:

String preBody = "The AThe CThe IThe O";
String[] eachBody = preBody.split("(?<=\\G.{5})");
for(int i=0;i<eachBody.lenght();i++){
  System.out.println("eachBody : -----"+eachBody[i]);
}

Also, as said, you shoul try to make your code less.. messy!

Community
  • 1
  • 1
Narmer
  • 1,414
  • 7
  • 16
1

My approach was to remove the 'used' parts of the string each iteration; that way, I only need to print out the first five characters of the remaining string each time. It's nice to keep things simple:

    String preBody = "The AThe CThe IThe O";

    while (preBody.length()>0) {
        String part = preBody.substring(0, 5);
        System.out.println(part);
        if (preBody.length()>5) {
            preBody = preBody.substring(5);
        } else {
            preBody = "";
        }
    }
NickJ
  • 9,380
  • 9
  • 51
  • 74
1

apparently your forgot to += your cnt = eachBody.length(); so it always reverts back to 5

here is a working code

public class HelloWorld{

 public static void main(String []args){
 String   preBody = "The AThe CThe IThe O";
  int bodyCnt = 5;
int cnt = 0;
String eachBody;
int extra = 0;
int limit = preBody.length();
while(limit >= bodyCnt){
 eachBody = preBody.substring((cnt+extra), (bodyCnt+cnt));
 cnt += eachBody.length();
 extra=0;
 limit = limit - eachBody.length();
 System.out.println("eachBody : -----"+eachBody);

} } }

Yaje
  • 2,753
  • 18
  • 32
0
public class Test{
  public static void main(String[] s){ 
    int bodyCnt = 5;
    int cnt = 0;
    String eachBody;
    String preBody = "The AThe CThe IThe O";
    int extra = 0;
    int limit = preBody.length();
    while(limit >= bodyCnt){
      eachBody = preBody.substring((cnt+extra), (bodyCnt+cnt));
      cnt = cnt + eachBody.length();
      extra = 0;
      limit = limit - eachBody.length();
      System.out.println("eachBody : -----"+eachBody);
    }   
  }

}

execute:

~$ javac Test.java 
~$ java Test
eachBody : -----The A
eachBody : -----The C
eachBody : -----The I
eachBody : -----The O

If you have options, I would suggest read up Regex some, this might help:

public class Test{
  public static void main(String[] a){ 
    String preBody = "The AThe CThe IThe O";
    String[] parts = preBody.split("(?=The)");
    for (String s : parts){
      if( null != s && !"".equals(s)){
        System.out.println(">> " +s );
      }   
    }   

  }

}

Output?

~$ javac Test.java 
~$ java Test
>> The A
>> The C
>> The I
>> The O
Nishant
  • 54,584
  • 13
  • 112
  • 127