View Single Post
  #2 (permalink)  
Old 08-03-2007, 11:52 AM
hardwired hardwired is offline
Senior Member
 
Join Date: Jul 2007
Posts: 1,189
hardwired is on a distinguished road
always equals null
Move "wrdCnt++;" to the bottom of the loop. It is incrementing to the next (null) element before you get to the println statement.
Code:
// <applet code="SA" width="400" height="200"></applet> import java.applet.*; import java.awt.*; import java.awt.event.*; import java.lang.String.*; public class SA extends Applet implements ActionListener { TextField theSntnc = new TextField(60); Button pressMe=new Button("parse to array"); String fillIt; String checkIt; Label fillMe; String[] sntncArray= new String[30]; public void init() { System.out.println(getLayout().getClass().getName()); setLayout(new BorderLayout()); add(theSntnc, "North"); add(pressMe, "South"); pressMe.addActionListener(this); } public void actionPerformed(ActionEvent pressMe) { fillIt=new String (theSntnc.getText()); fillMe=new Label("FillIt"+fillIt); add (fillMe); invalidate(); validate(); brkSntnc(fillIt); printArray(); } public void brkSntnc(String sntnc) { int x=0; int wrdCnt=0; int myLen; while(x != -1) { myLen=sntnc.length(); x=sntnc.indexOf(" "); if(x > -1) sntncArray[wrdCnt]=sntnc.substring(0, x); else sntncArray[wrdCnt]=sntnc; System.out.println("Before:"+sntnc); sntnc=sntnc.substring(x+1,myLen); System.out.println("After:"+sntnc); System.out.println("Array["+wrdCnt+"] is:"+sntncArray[wrdCnt]); wrdCnt++; } } private void printArray() { System.out.print("sntncArray = ["); for(int j = 0; j < sntncArray.length; j++) { if(sntncArray[j] == null) continue; System.out.print(sntncArray[j]); if(j < sntncArray.length-1) System.out.print(", "); } System.out.print("]\n"); } }
Reply With Quote