java - String split not working when there is no text in one side -
trying split "username:password" textarea, textareauser , textareapass, when input "username:" or ":password" stops.
private void jbuttonstartactionperformed(java.awt.event.actionevent evt) { string[] lines = jtextareascrap.gettext().split("\n"); string[] divid = null; string user, pass; jtextareauser.settext(null); jtextareapass.settext(null); (int i=0; i<= lines.length; i++){ if (lines[i].contains(":")) { divid = lines[i].split(":"); user = divid[0]; pass = divid[1]; jtextareauser.append(divid[0]+"\n"); jtextareapass.append(divid[1]+"\n"); } }
stack trace :
exception in thread "awt-eventqueue-0" java.lang.arrayindexoutofboundsexception: 1 @ dorkcreator.dorkcreator.jbuttonstartactionperformed(dorkcreator.java:135) @ dorkcreator.dorkcreator.access$000(dorkcreator.java:14) @ dorkcreator.dorkcreator$1.actionperformed(dorkcreator.java:50) @ javax.swing.abstractbutton.fireactionperformed(abstractbutton.java:2022) @ javax.swing.abstractbutton$handler.actionperformed(abstractbutton.java:2346) @ javax.swing.defaultbuttonmodel.fireactionperformed(defaultbuttonmodel.java:402) @ javax.swing.defaultbuttonmodel.setpressed(defaultbuttonmodel.java:259) @ javax.swing.plaf.basic.basicbuttonlistener.mousereleased(basicbuttonlistener.java:252) @ java.awt.component.processmouseevent(component.java:6525) @ javax.swing.jcomponent.processmouseevent(jcomponent.java:3324) @ java.awt.component.processevent(component.java:6290) @ java.awt.container.processevent(container.java:2234) @ java.awt.component.dispatcheventimpl(component.java:4881) @ java.awt.container.dispatcheventimpl(container.java:2292) @ java.awt.component.dispatchevent(component.java:4703) @ java.awt.lightweightdispatcher.retargetmouseevent(container.java:4898) @ java.awt.lightweightdispatcher.processmouseevent(container.java:4533) @ java.awt.lightweightdispatcher.dispatchevent(container.java:4462) @ java.awt.container.dispatcheventimpl(container.java:2278) @ java.awt.window.dispatcheventimpl(window.java:2750) @ java.awt.component.dispatchevent(component.java:4703) @ java.awt.eventqueue.dispatcheventimpl(eventqueue.java:751) @ java.awt.eventqueue.access$500(eventqueue.java:97) @ java.awt.eventqueue$3.run(eventqueue.java:702) @ java.awt.eventqueue$3.run(eventqueue.java:696) @ java.security.accesscontroller.doprivileged(native method) @ java.security.protectiondomain$1.dointersectionprivilege(protectiondomain.java:75) @ java.security.protectiondomain$1.dointersectionprivilege(protectiondomain.java:86) @ java.awt.eventqueue$4.run(eventqueue.java:724) @ java.awt.eventqueue$4.run(eventqueue.java:722) @ java.security.accesscontroller.doprivileged(native method) @ java.security.protectiondomain$1.dointersectionprivilege(protectiondomain.java:75) @ java.awt.eventqueue.dispatchevent(eventqueue.java:721) @ java.awt.eventdispatchthread.pumponeeventforfilters(eventdispatchthread.java:201) @ java.awt.eventdispatchthread.pumpeventsforfilter(eventdispatchthread.java:116) @ java.awt.eventdispatchthread.pumpeventsforhierarchy(eventdispatchthread.java:105) @ java.awt.eventdispatchthread.pumpevents(eventdispatchthread.java:101) @ java.awt.eventdispatchthread.pumpevents(eventdispatchthread.java:93) @ java.awt.eventdispatchthread.run(eventdispatchthread.java:82)
it's because string split remove "empties".
change to:
divid = lines[i].split(":", -1);