Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-31-2007, 10:18 PM
Member
 
Join Date: Jul 2007
Posts: 40
mathias is on a distinguished road
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
Sponsored Links
  #2 (permalink)  
Old 08-03-2007, 11:52 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,029
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"); } }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


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

vB 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 4 06-27-2008 12:58 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 +3. The time now is 05:42 AM.


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