Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-31-2007, 10:18 PM
Member
 
Join Date: Jul 2007
Posts: 40
Rep Power: 0
mathias is on a distinguished road
Default 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:

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]);
}
}

}
Basically I'm trying to write a class which accepts input and breaks the words in the input into an array, one array slot for each word. So far, as I said before,
Code:
sntncArray[wrdCnt]=sntnc.substring(0,x-1);
always equals null, in spite of the fact that
Code:
sntnc.substring(0,x-1);
is a valid string.

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 of
Code:
sntnc.substring(0,x-1);
("passer") and tried
Code:
sntncAray[wrdCnt]=passer;
but with the same result:Null.
Thanks.
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 08-03-2007, 11:52 AM
hardwired's Avatar
Senior Member
 
Join Date: Jul 2007
Posts: 1,577
Rep Power: 4
hardwired is on a distinguished road
Default
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");
    }
}
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to Parse int to a string variable raj reddy Java Servlet 10 01-09-2009 08:41 PM
how to Parse int to a string variable (pls hlp) raj reddy Threads and Synchronization 5 06-10-2008 07:32 AM
make a variable name from a string? Kinnikinnick New To Java 3 11-13-2007 04:54 PM
Help with variable assigment to String silvia New To Java 1 08-07-2007 06:43 AM
String Variable Eric Advanced Java 1 06-06-2007 05:30 AM


All times are GMT +2. The time now is 09:13 AM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org