The Artima Developer Community
Sponsored Link

Java Answers Forum
Copying Strings into an array

1 reply on 1 page. Most recent reply: Mar 23, 2002 11:05 AM by Kishori Sharan

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Simon Humphreys

Posts: 1
Nickname: simonh
Registered: Mar, 2002

Copying Strings into an array Posted: Mar 23, 2002 1:49 AM
Reply to this message Reply
Advertisement
Hi,

I'm, trying to copy a string, read from a file and stored using a
StringTokeniser class, into an array of Strings

e.g. this gets the data from a file
<pre>
int startInt = Integer.parseInt(t.nextToken());
int stopInt = Integer.parseInt(t.nextToken());
String tutorCode = t.nextToken();
</pre>
thenI want the array to contain the same tutorCode for each index between
startInt and stopInt ......

i.e.
<pre>while (startInt != stopInt)
{
//System.out.println("Tutor code = " + tutorCode);
tutors[startInt] = tutorCode;
startInt++;
//System.out.println("StartInt #" + startInt + " tutor = " +
tutors[startInt]);
}
</pre>
The first line displays the right value but the last shows that the
reference has not been picked up. I have also replaced teh assignment .....
tutors[startInt] = new String(tutorCode) .... with the saem result.

Help please ........... I know this must be an easy one to do with copying
of objects ... but I cannot see it just now.

All I'm getting are null values, when I initialise the array with empty Strings, I simply get an empty string on the console!

Thanks


Kishori Sharan

Posts: 211
Nickname: kishori
Registered: Feb, 2002

Re: Copying Strings into an array Posted: Mar 23, 2002 11:05 AM
Reply to this message Reply
It is hard to tell exact error just reading the fragment of your code. However, I can tell you two things:
//System.out.println("Tutor code = " + tutorCode);
tutors[startInt] = tutorCode;
startInt++;
//System.out.println("StartInt #" + startInt + " tutor = " +
tutors[startInt]);

1. When you are printing first time then you print the value of tutorCode. And, since you have read the value of tutorCode before while loop it is printing correctly.
2. In this line
tutors[startInt] = tutorCode;
You are storing the tutorCode at startInt position and then in next line you are incrementing startInt by one. However, in second print you are trying to print the tutorCode at the position defined by new ( I mean incremented) startInt. That is, if you stored tutorCode at 1 then you are trying to print it from 2. Of course, you haven't stored tutor code at 2 yet, it will display null value or throw ArrayIndexOutOfBound if your code moves past the array length. So, just place startInt++; code after your second print and it will display the right value.
For more help, please post all your code.

Thanks
Kishori

Flat View: This topic has 1 reply on 1 page
Topic: Network Programming with TCP Previous Topic   Next Topic Topic: Writer Method

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use