简体   繁体   中英

Java String split newline

I take a ByteArrayInputStream and using Stringbuilder create a full string (sb).

I then take the sb and move it to a String field, which I then try to split on newline is indicated in this post: Split Java String by New Line

I'm getting an error though on the

 String configLines[] = tempL.split("\\r?\\n");

line as follows:

depends on configLines is neither defined in the script nor in dependencies."

This is the only place in the code configLines is mentioned (so far). Any ideas what is wrong with this single line of code? It should just work shouldn't it?

When I comment out this line the code works, when I uncomment it - it fails.

I've seen the byte code and the bytes are 13, 10 for newline, carriage return.

What am I doing wrong? Thanks in advance.

ByteArrayInputStream str = new ByteArrayInputStream(documentData);
StringBuilder sb = new StringBuilder();

int ch;
while((ch = str.read()) != -1) {
    sb.append(Character.toUpperCase((char) ch));
}
if(debug){dI++; logger.severe(dI+thisTrace+"sb: "+sb.toString());}

 String tempL = sb.toString();
 if(debug){dI++; logger.severe(dI+thisTrace+"tempL: "+tempL);}

 String configLines[] = tempL.split("\\r?\\n");

The full error message is as follows, and it's coming from a groovy interpreter, which is also java.

2015-04-11 19:17:39 org.bonitasoft.engine.execution.work.FailureHandlingBonitaWork 
SEVERE: THREAD_ID=377 | HOSTNAME=Mainframe | TENANT_ID=1 | org.bonitasoft.engine.expression.exception.SExpressionEvaluationException : "PROCESS_DEFINITION_ID=7620684800960500232 | PROCESS_NAME=Configure |     PROCESS_VERSION=3.0 | PROCESS_INSTANCE_ID=18001 | ROOT_PROCESS_INSTANCE_ID=18001  | FLOW_NODE_DEFINITION_ID=-7446352707110997787 | FLOW_NODE_INSTANCE_ID=360005 |     FLOW_NODE_NAME=prepareChosenConfigFile | CONNECTOR_DEFINITION_IMPLEMENTATION_CLASS_NAME=prepareChosenConfigFile | CONNECTOR_INSTANCE_ID=340004 | Expression prepareChosenConfigFile with content = </**

This is a print out of the code module as above.

*/> depends on lines is neither defined in the script nor in dependencies."

org.bonitasoft.engine.expression.exception.SExpressionEvaluationException: PROCESS_DEFINITION_ID=7620684800960500232 | PROCESS_NAME=Configure | PROCESS_VERSION=3.0 | PROCESS_INSTANCE_ID=18001 | ROOT_PROCESS_INSTANCE_ID=18001 | FLOW_NODE_DEFINITION_ID=-7446352707110997787 | FLOW_NODE_INSTANCE_ID=360005 | FLOW_NODE_NAME=prepareChosenConfigFile | CONNECTOR_DEFINITION_IMPLEMENTATION_CLASS_NAME=prepareChosenConfigFile | CONNECTOR_INSTANCE_ID=340004 | Expression prepareChosenConfigFile with content = </**

This is a print out of the code module as above.

*/> depends on lines is neither defined in the script nor in dependencies."
at     org.bonitasoft.engine.expression.impl.GroovyScriptExpressionExecutorCacheStrategy.evaluate(GroovyScriptExpressionExecutorCacheStrategy.java:151)
at org.bonitasoft.engine.expression.impl.ExpressionServiceImpl.evaluate(ExpressionServiceImpl.java:86)
at org.bonitasoft.engine.core.expression.control.api.impl.ExpressionResolverServiceImpl.evaluateExpressionWithResolvedDependencies(ExpressionResolverServiceImpl.java:215)
at org.bonitasoft.engine.core.expression.control.api.impl.ExpressionResolverServiceImpl.evaluateExpressionsFlatten(ExpressionResolverServiceImpl.java:120)
at org.bonitasoft.engine.core.expression.control.api.impl.ExpressionResolverServiceImpl.evaluate(ExpressionResolverServiceImpl.java:83)
at org.bonitasoft.engine.core.connector.impl.ConnectorServiceImpl.evaluateInputParameters(ConnectorServiceImpl.java:352)
at org.bonitasoft.engine.connector.ConnectorServiceDecorator.evaluateInputParameters(ConnectorServiceDecorator.java:99)
at org.bonitasoft.engine.execution.work.ExecuteConnectorWork$EvaluateParameterAndGetConnectorInstance.call(ExecuteConnectorWork.java:198)
at org.bonitasoft.engine.execution.work.ExecuteConnectorWork$EvaluateParameterAndGetConnectorInstance.call(ExecuteConnectorWork.java:162)
at org.bonitasoft.engine.transaction.JTATransactionServiceImpl.executeInTransaction(JTATransactionServiceImpl.java:288)
at org.bonitasoft.engine.execution.work.ExecuteConnectorWork.work(ExecuteConnectorWork.java:122)
at org.bonitasoft.engine.execution.work.failurewrapping.TxInHandleFailureWrappingWork.work(TxInHandleFailureWrappingWork.java:42)
at org.bonitasoft.engine.execution.work.failurewrapping.TxInHandleFailureWrappingWork.work(TxInHandleFailureWrappingWork.java:42)
at org.bonitasoft.engine.execution.work.failurewrapping.TxInHandleFailureWrappingWork.work(TxInHandleFailureWrappingWork.java:42)
at org.bonitasoft.engine.execution.work.failurewrapping.TxInHandleFailureWrappingWork.work(TxInHandleFailureWrappingWork.java:42)
at org.bonitasoft.engine.execution.work.FailureHandlingBonitaWork.work(FailureHandlingBonitaWork.java:66)
at org.bonitasoft.engine.work.BonitaWork.run(BonitaWork.java:56)
at org.bonitasoft.engine.work.SequenceRunnableExecutor.innerRun(SequenceRunnableExecutor.java:47)
at org.bonitasoft.engine.work.BonitaRunnable.run(BonitaRunnable.java:35)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: groovy.lang.MissingPropertyException: No such property: lines for class: BScript8
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:50)
at org.codehaus.groovy.runtime.callsite.PogoGetPropertySite.getProperty(PogoGetPropertySite.java:49)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callGroovyObjectGetProperty(AbstractCallSite.java:231)
at BScript8.run(BScript8.groovy:197)
at org.bonitasoft.engine.expression.impl.GroovyScriptExpressionExecutorCacheStrategy.evaluate(GroovyScriptExpressionExecutorCacheStrategy.java:145)
... 21 more

Thanks guys, in the end I researched Google for more help. I was astonished to find many sites using what I had used:

String configLines[] = tempL.split("\\r?\\n");

which I believe should work, and still do... :)

However I did find another implementation of the split command as follows:

String[]  configLines = tempL.split("\\r?\\n");

which upon testing did work.

So while I'm happy now that my code is working and I can proceed, can someone enlighten me as to why the former did not work in this case, but the latter does?

The sites which lead me to this answer are as follows:

http://javarevisited.blogspot.com/2011/09/string-split-example-in-java-tutorial.html http://www.java-examples.com/java-string-split-example

Many thanks to all, regards

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM