Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-12-2005, 10:51 PM   PM User | #1
thehaunt
New to the CF scene

 
Join Date: Jan 2005
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
thehaunt is an unknown quantity at this point
Java: I Can't pass a string variable into array?

I'm new to Java, very very new, and 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:
_________________________
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 (in the form of an applet) and breaks the words in the input into an array, one array slot for each word. So far, as I said before, sntncArray[wrdCnt]=sntnc.substring(0,x-1); always equals null, in spite of the fact that 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 sntnc.substring(0,x-1); ("passer") and tried sntncAray[wrdCnt]=passer; but with the same result:Null. Any assistance is greatly appreciated. Greatly greatly. VERY Greatly greatly.
[Thnx]
//end
thehaunt is offline   Reply With Quote
Old 01-12-2005, 11:29 PM   PM User | #2
Antoniohawk
Senior Coder

 
Join Date: Aug 2002
Location: Kansas City, Kansas
Posts: 1,518
Thanks: 0
Thanked 2 Times in 2 Posts
Antoniohawk will become famous soon enough
Could you please enclose your code with [ code ] tags so that it retain the formatting (I hope that it is formatted) and become easier to read. I want to help, but it's really hard when it takes so much effort just to deciper the thing.
Antoniohawk is offline   Reply With Quote
Old 01-13-2005, 02:05 AM   PM User | #3
cfc
Regular Coder

 
Join Date: Dec 2004
Location: Keswick, Ontario
Posts: 251
Thanks: 0
Thanked 0 Times in 0 Posts
cfc is an unknown quantity at this point
fillIt=new String (theSntnc.getText());

Correct me if I'm wrong, but isn't the "new String( ... )" unnecessary?

sntncArray[wrdCnt]=sntnc.substring(0,x-1);

I believe substring excludes the last index you specify. Therefore, the -1 will probably return one less character than you want per word.

I'm a little out of practice with Java so I can't dissect your code (however small it is) ATM, and my suggestions probably won't fix your problem. I'll continue looking at it though . However, if this is purely for personal experimentation (and not something you plan to put on the internet where nobody is guaranteed to have JRE 1.4), the String object has had a split(String regex) method since 1.4.

Last edited by cfc; 01-13-2005 at 02:26 AM..
cfc is offline   Reply With Quote
Old 01-13-2005, 03:10 PM   PM User | #4
thehaunt
New to the CF scene

 
Join Date: Jan 2005
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
thehaunt is an unknown quantity at this point
yeah the fillIt is unnecessary, I just put it there to make sure the input was accurate, rather than using System.out.println etc...


And above, how will putting [] around the code help it maintain it's formatting?
thehaunt is offline   Reply With Quote
Old 01-13-2005, 03:23 PM   PM User | #5
thehaunt
New to the CF scene

 
Join Date: Jan 2005
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
thehaunt is an unknown quantity at this point
AHA!! Boy those FAQ's are handy!
Here you go, code in a more readable format!
Thanks for the tip.

Code:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.lang.*;


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;

   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)
         wrdCnt++;
         sntnc=sntnc.substring(x,myLen);
         }
      }
}
thehaunt is offline   Reply With Quote
Old 01-14-2005, 12:21 AM   PM User | #6
cfc
Regular Coder

 
Join Date: Dec 2004
Location: Keswick, Ontario
Posts: 251
Thanks: 0
Thanked 0 Times in 0 Posts
cfc is an unknown quantity at this point
Code:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.lang.*;


/* <applet code="SntncArray.class" width="300" height="300"></applet> */

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 = 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);
         wrdCnt++;
         sntnc=sntnc.substring(x, myLen);
         }
      }
}
You're sure this doesn't work for you? I compiled and opened it in appletviewer and it worked for me. (BTW what I meant is that you don't have to create a new String class when putting theSntnc.getText() into a new variable).
cfc is offline   Reply With Quote
Old 01-14-2005, 03:18 AM   PM User | #7
thehaunt
New to the CF scene

 
Join Date: Jan 2005
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
thehaunt is an unknown quantity at this point
Oh man I feel so stupid, after days of looking at this code asking myself endlessly why it didn't work, I looked at what I posted and realized that the reason
Code:
System.out.println(sntncArray[wrdCnt]);
was returning null all the time was that I had placed the system out call AFTER incrementing wrdCnt instead of before. How embarrassing :P
thehaunt is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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

Forum Jump


All times are GMT +1. The time now is 10:42 AM.


Advertisement
Log in to turn off these ads.