Java implementation error

That’s a link to the Exercise website! What about it?

   public String truncate( String ac ) {
      String outin = "";
      String a[] = ac.split( "" );
      System.out.println( a.length );
      for( int i = 0; i < 5 || i < a.length; i ++ ) {
         outin += a[ i ];
      }
      return outin;
   }
    
   public String truncateA( String ac ) {
      String outin = "";
      String a[] = ac.split( "" );
      System.out.println( a.length );
      for( int i = 0; i < 5 && i < a.length; i ++ ) {
         outin += a[ i ];
      }
      return outin;
   }

truncate works and truncateA creates an infinite loop, this is not possible, since the condition of the “for”, which states that it stops either if “i” is greater than “a.length”, or if it is greater than “5”, does not make sense.

Okay. As you say, this is not possible. What about it? Did you have a question? Is this related to an exercise on the website? Did you just want to show off your code?

I’m fairly sure this is Micro Blog if we’re truncating a string to five or less characters.

When you say truncate works, what does that mean? Are all the tests passing? I suspect they’re not so what error messages are you potentially seeing?

For the record, I don’t believe that truncateA is causing an infinite loop, but that your solution isn’t getting through all the tests in the allocated time for each test run.

Maybe it helps to write a truth table for the conditions in those loops. And then think about what is different between || and && for the outcome of the for-loop.

@MarceP It’s always better to provide all of your code and the exact failure message, so others can help.

@B.N.Andras You are right, the message is that “it takes too long, it could be an infinite loop”, I have done all the tests on my machine, and there is no infinite loop, it does not take a time that justifies the message “it takes too long”, I have not managed to get approval in all tests, I do not know how to do it, but that is not the problem I raise, my question is whether it is a bug in the site, in the implementation of Java.
@isaacg @mk-mxp thank you all for your attention.

Probably not.

If you want help debugging the issue, it would be helpful to see your complete code. It is possible there is a transient error on the website and you can also try rerunning the tests or testing your code locally.

I was able to reproduce that truncateA fails the tests locally, but they all run to completion. But in the online editor, the tests do timeout instead of just failing.

So maybe this is worth investigating more. My preliminary suspicion is that truncateA throws some sort of exception that the test runner isn’t expecting and that makes it not produce a result for the online editor, hence the timeout.

I don’t think I’ll have the time to investigate this further. If anyone wants to have a go at debugging this, I would build the test runner image locally and would try to run this exercise on it to see what happens.