Results 1 to 2 of 2
  1. #1
    mathias is offline Member
    Join Date
    Jul 2007
    Posts
    40
    Rep Power
    0

    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:

    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]);
    }
    }
    
    }
    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,
    Java Code:
    sntncArray[wrdCnt]=sntnc.substring(0,x-1);
    always equals null, in spite of the fact that
    Java 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
    Java Code:
    sntnc.substring(0,x-1);
    ("passer") and tried
    Java Code:
    sntncAray[wrdCnt]=passer;
    but with the same result:Null.
    Thanks.

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    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.
    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

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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •