Results 1 to 2 of 2
- 07-31-2007, 10:18 PM #1
Member
- Join Date
- Jul 2007
- Posts
- 40
- Rep Power
- 0
I can't seem to pass the value of a string variable into a string array
Hi, I have encountered a hair raising problem: I can't seem to pass the value of a string variable into a string array. It's very frustrating when I poll the array and get null as far as the eye can see. The code follows:
Java Code:import java.applet.*; import java.awt.*; import java.awt.event.*; import java.lang.String.*; public class SntncArray 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() { add(theSntnc); add(pressMe); pressMe.addActionListener(this); } public void actionPerformed(ActionEvent pressMe) { fillIt=new String (theSntnc.getText()); fillMe=new Label("FillIt"+fillIt); add (fillMe); invalidate(); validate(); brkSntnc(fillIt); } public void brkSntnc(String sntnc) { int x=0; int wrdCnt=0; int myLen; while (x!=-1) { myLen=sntnc.length(); x=sntnc.indexOf(" "); sntncArray[wrdCnt]=sntnc.substring(0,x-1); wrdCnt++; System.out.println("Before:"+sntnc); sntnc=sntnc.substring(x+1,myLen); System.out.println("After:"+sntnc); System.out.println("Array at "+wrdCnt+" is:"+sntncArray[wrdCnt]); } } }
Java Code:sntncArray[wrdCnt]=sntnc.substring(0,x-1);
Java Code:sntnc.substring(0,x-1);
I'm clearly missing something very obvious, but I can't figure out what. I know that strings are objects and not primitives and so I can't use them as primitives (I think) but how can I pass the value of a given string into an array? I even tried making a string to hold the value ofJava Code:sntnc.substring(0,x-1);
Java Code:sntncAray[wrdCnt]=passer;
Thanks.
- 08-03-2007, 11:52 AM #2
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.
Java 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"); } }
Similar Threads
-
how to Parse int to a string variable
By raj reddy in forum Java ServletReplies: 10Last Post: 01-09-2009, 08:41 PM -
how to Parse int to a string variable (pls hlp)
By raj reddy in forum Threads and SynchronizationReplies: 5Last Post: 06-10-2008, 07:32 AM -
make a variable name from a string?
By Kinnikinnick in forum New To JavaReplies: 3Last Post: 11-13-2007, 04:54 PM -
Help with variable assigment to String
By silvia in forum New To JavaReplies: 1Last Post: 08-07-2007, 06:43 AM -
String Variable
By Eric in forum Advanced JavaReplies: 1Last Post: 06-06-2007, 05:30 AM
Bookmarks