Array Question

Can someone please help me with this code? I'm not to good with arrays yet, and I'm trying I can't figure out why this prints out a line of nulls. When I put the displayArray method within the for loop, it prints out the correct word values, but otherwise I get rows of nulls. Also, if there are any other obvious errors please fix. Thank you so much.

import java.io.*;

import java.util.*;

import java.text.*;

public class ZinniSpeech

{

private static String[] originalZinni;

private static Scanner rawText;

private static StringBuilder rawTextString;

private static StringTokenizer rawTextToken;

private static int wordTotal1;

private static int wordTotalFinal;

public static void main(String args[])throws IOException

{

rawText = new Scanner(new File(

"\\Java\\Zinni\\ZinniSpeechCDI5_04.txt"));

rawText.useDelimiter("\\W+");

rawTextString = new StringBuilder();

while(rawText.hasNext())

{

rawTextString.append(" " + rawText.next());

}

rawText.close();

rawTextToken = new StringTokenizer(rawTextString.toString());

wordTotal1 = rawTextToken.countTokens();

for (int x = 0; x < wordTotal1; x++)

{

String[] tempArray = new String[wordTotal1];

tempArray[x] = rawTextToken.nextToken();

if (!(tempArray[x].equals("")))

if (!(tempArray[x].equals("s")))

if (!(tempArray[x].equals("t")))

if (!(tempArray[x].equals("ve")))

if (!(tempArray[x].equals("re")))

if (!(tempArray[x].equals("m")))

if (!(tempArray[x].equals("ll")))

if (!(tempArray[x].equals("null")))

{

originalZinni = tempArray;

String lowerCase = new String(originalZinni[x]);

originalZinni[x] = lowerCase.toLowerCase();

}

}

displayList(tempArray);

}

private static void displayList(String[] originalZinni)

{

for(String word: originalZinni)

{

System.out.println(word);

}

}

}

Message was edited by:

minkus46

[2096 byte] By [minkus46a] at [2007-11-27 8:56:10]
# 1

There, I cut some of the worthless stuff out.

import java.io.*;

import java.util.*;

import java.text.*;

public class ZinniSpeech

{

private static String[] originalZinni;

private static Scanner rawText;

private static StringBuilder rawTextString;

private static StringTokenizer rawTextToken;

private static int wordTotal1;

private static int wordTotalFinal;

public static void main(String args[])throws IOException

{

rawText = new Scanner(new File(

"\\Java\\Zinni\\ZinniSpeechCDI5_04.txt"));

rawText.useDelimiter("\\W+");

rawTextString = new StringBuilder();

while(rawText.hasNext())

{

rawTextString.append(" " + rawText.next());

}

rawText.close();

rawTextToken = new StringTokenizer(rawTextString.toString());

wordTotal1 = rawTextToken.countTokens();

for (int x = 0; x < wordTotal1; x++)

{

String[] tempArray = new String[wordTotal1];

tempArray[x] = rawTextToken.nextToken();

originalZinni = tempArray;

String lowerCase = new String(originalZinni[x]);

originalZinni[x] = lowerCase.toLowerCase();

}

displayList(tempArray);

}

private static void displayList(String[] originalZinni)

{

for(String word: originalZinni)

{

System.out.println(word);

}

}

}

minkus46a at 2007-7-12 21:18:34 > top of Java-index,Java Essentials,New To Java...
# 2

When you post code, please wrap it in [code][/code] tags. It makes it much easier to read. Also indent it sanely.

This:

> if (!(tempArray[x].equals("")))

> if (!(tempArray[x].equals("s")))

> if (!(tempArray[x].equals("t")))

> if (!(tempArray[x].equals("ve")))

> if (!(tempArray[x].equals("re")))

> if (!(tempArray[x].equals("m")))

> if (!(tempArray[x].equals("ll")))

> if (!(tempArray[x].equals("null")))

is hideous and bug-prone. Better to have one boolean statement connected with && than this degree of nested if statements.

My guess is that your problem derives from this part:

for (int x = 0; x < wordTotal1; x++)

{

String[] tempArray = new String[wordTotal1];

You create this array once per loop iteration, and then apparently only use one position in the array each time. The remaining positions are filled with nulls, of course. You probably want to allocate this array once, outside the loop.

paulcwa at 2007-7-12 21:18:35 > top of Java-index,Java Essentials,New To Java...
# 3

First off, please put the code into [ code ] tags for readability.

for (int x = 0; x < wordTotal1; x++) {

String[] tempArray = new String[wordTotal1];

tempArray[x] = rawTextToken.nextToken();

The issue here is that you're creating a new, blank array every time you go through the loop. Then you insert one value, check it, and convert it to lowercase.

I don't even understand how this doesn't throw an error at compile time:

displayList(tempArray);

Since tempArray is declared only within the for loop, it shouldn't exist at this point.

myncknma at 2007-7-12 21:18:35 > top of Java-index,Java Essentials,New To Java...
# 4

I'm sorry guys, this is a very rough draft. I was playing around with the code. This is really what it's supposed to look like. I wasn't trying to reference the temporary variable. I just put it in there to see if I got the same result as originalZinni did, then I forgot to change it. Still returning nulls like this though.

import java.io.*;

import java.util.*;

import java.text.*;

public class ZinniSpeech

{

private static String[] originalZinni;

private static Scanner rawText;

private static StringBuilder rawTextString;

private static StringTokenizer rawTextToken;

private static int wordTotal1;

private static int wordTotalFinal;

public static void main(String args[])throws IOException

{

rawText = new Scanner(new File(

"\\Java\\Zinni\\ZinniSpeechCDI5_04.txt"));

rawText.useDelimiter("\\W+");

rawTextString = new StringBuilder();

while(rawText.hasNext())

{

rawTextString.append(" " + rawText.next());

}

rawText.close();

rawTextToken = new StringTokenizer(rawTextString.toString());

wordTotal1 = rawTextToken.countTokens();

for (int x = 0; x < wordTotal1; x++)

{

String[] tempArray = new String[wordTotal1];

tempArray[x] = rawTextToken.nextToken();

originalZinni = tempArray;

String lowerCase = new String(originalZinni[x]);

originalZinni[x] = lowerCase.toLowerCase();

}

displayList(originalZinni);

}

private static void displayList(String[] originalZinni)

{

for(String word: originalZinni)

{

System.out.println(word);

}

}

}

minkus46a at 2007-7-12 21:18:35 > top of Java-index,Java Essentials,New To Java...
# 5
Wait, not all nulls. There is a long line of nulls returned, then the last spot in the array is returned correctly.
minkus46a at 2007-7-12 21:18:35 > top of Java-index,Java Essentials,New To Java...
# 6

Thanks for the formatting.

for (int x = 0; x < wordTotal1; x++)

{

String[] tempArray = new String[wordTotal1];

tempArray[x] = rawTextToken.nextToken();

originalZinni = tempArray;

String lowerCase = new String(originalZinni[x]);

originalZinni[x] = lowerCase.toLowerCase();

}

Okay, so the problem is still here. You're creating a new, blank array every time you go through the loop, and then you assign it to originalZinni. When you use the assignment (=) operator on an object, it doesn't create a new object. Instead, both variables refer to the same object. Also, even if it did create a new array, all the nulls in each new tempArray would get copied over to originalZinni everytime anyways.

Just get rid of the tempArray and write directly to originalZinni.

This should work:

originalZinni = new String[wordTotal1];

for (int x = 0; x < wordTotal1; x++)

{

originalZinni[x] = rawTextToken.nextToken().toLowerCase();

}

myncknma at 2007-7-12 21:18:35 > top of Java-index,Java Essentials,New To Java...
# 7
Hey, thanks a lot. Wow, that was kind of a dumb mistake. I need to check more carefully. I've just had a long day and was pretty frustrated. If I have any more questions I'll post on the same thread, but thanks again.
minkus46a at 2007-7-12 21:18:35 > top of Java-index,Java Essentials,New To Java...
# 8

All right, another question. I wrote more code that searches for word repetitions and then creates a parallel area to keep track of word repetitions. But it displays "word" = 1

"word" = 2

"word1" = 1

"word1" = 2

"word1" = 3

And so forth. How do I delete everything except for the last number, and then trim the arrays? Here's my code for the whole thing:

import java.io.*;

import java.util.*;

import java.text.*;

public class ZinniSpeech

{

private static String[] originalZinni;

private static int[] wordCount;

private static Scanner rawText;

private static StringBuilder rawTextString;

private static StringTokenizer rawTextToken;

private static int wordTotal1;

private static int wordTotalFinal;

private static int count = 0;

public static void main(String args[])throws IOException

{

rawText = new Scanner(new File(

"\\Java\\Zinni\\ZinniSpeechCDI5_04.txt"));

rawText.useDelimiter("\\W+");

rawTextString = new StringBuilder();

while(rawText.hasNext())

{

rawTextString.append(" " + rawText.next());

}

rawText.close();

rawTextToken = new StringTokenizer(rawTextString.toString());

wordTotal1 = rawTextToken.countTokens();

originalZinni = new String[wordTotal1];

for (int x = 0; x < wordTotal1; x++)

{

originalZinni[x] = rawTextToken.nextToken();

if(!(originalZinni[x].equals("")))

if(!(originalZinni[x].equals("s")))

if(!(originalZinni[x].equals("t")))

if(!(originalZinni[x].equals("ve")))

if(!(originalZinni[x].equals("re")))

if(!(originalZinni[x].equals("m")))

if(!(originalZinni[x].equals("ll")))

if(!(originalZinni[x].equals("null")))

{

String lowerCase = new String(originalZinni[x]);

originalZinni[x] = lowerCase.toLowerCase();

}

}

Arrays.sort(originalZinni);

wordCount = new int [wordTotal1];

for(int x = 0; x < wordTotal1; x++)

{

if(originalZinni[x].contentEquals(originalZinni[x++]))

{

wordCount[x] = count++;

}

else if(!originalZinni[x].contentEquals(originalZinni[x++]))

{

wordCount[x] = count++;

count = 0;

}

}

for(int x = 0; x < wordTotal1; x++)

{

System.out.println(originalZinni[x] + " = " + wordCount[x]);

}

}

}

minkus46a at 2007-7-12 21:18:35 > top of Java-index,Java Essentials,New To Java...
# 9
Since I haven't seen an adequate discription of what you are trying to do, perhaps you could post that.
floundera at 2007-7-12 21:18:35 > top of Java-index,Java Essentials,New To Java...
# 10

You might want to reconsider your use of the ++ postfix operator.

Keep in mind that it permanently adds to the value of the variable. and only after the statement has executed.

Now, what I would do to get rid of the duplicate entries is create a new array that contains only the unique ones, adding each unique entry and the number of times it appears as it's going through and counting the first array.

Local variables are faster than class variables, by the way. You really don't need to declare class variables unless they're needed outside of the context of a single method.

Oh, and when will I be able to get those dukes, eh? >>

myncknma at 2007-7-12 21:18:35 > top of Java-index,Java Essentials,New To Java...
# 11

Thanks, here's the basic description

Analyze the text of the speech with an alphabetized list of all words and the count of their occurrences. (word = 1) I also need to use parallel arrays. One for the alphabetized list of words, and one for their counts. I also need to calculate the total number of all words, unique words, and the average size(character amount) of all words. I can do those calculations though, I'm just having trouble with the arrays.

Message was edited by:

minkus46

minkus46a at 2007-7-12 21:18:35 > top of Java-index,Java Essentials,New To Java...
# 12
When I change it tox + 1, I get an arrayIndexOutOfBoundsException.
minkus46a at 2007-7-12 21:18:35 > top of Java-index,Java Essentials,New To Java...
# 13
Could I have some help please?
minkus46a at 2007-7-12 21:18:35 > top of Java-index,Java Essentials,New To Java...
# 14

>When I change it to x + 1, I get an arrayIndexOutOfBoundsException.

Think about what you're trying to do. If you change it to x+1, then first it compares array[0] with array[1]. Good. Then it moves on to array[1] with array[2]. Also good. Then eventually it gets to the end of the array, where it tries to compare array[last_index] with array[last_index + 1]. Oops. There's no element after the last one. How might we fix that?

for(int x = 0; x < wordTotal1; x++) {

if(originalZinni[x].contentEquals(originalZinni[x++])) {

wordCount[x] = count++;

}

else if(!originalZinni[x].contentEquals(originalZinni[x++])) {

wordCount[x] = count++;

count = 0;

}

}

Here, if you want to list only once each unique word found, consider the place in the code where you say, "count = 0;." If it reaches that point, then obviously, you've found all copies of a certain word, and are now about to move on to the next unique word. So make another array or list, and stick that unique word into that array, which should only contain each word once. Keep track of the number of words already in there so that you know which index to put the next word in. Number the wordCount to match this new array/list of words. Easy, yes?

myncknma at 2007-7-12 21:18:35 > top of Java-index,Java Essentials,New To Java...
# 15
Thanks again. But what would the code look like to instantiate that array of unique words? And how would I number the wordCount similarly?
minkus46a at 2007-7-21 22:50:14 > top of Java-index,Java Essentials,New To Java...
# 16
At the beginning, when you need to instantiate the arrays, you don't know how large it needs to be. Two solutions to this are making the arrays as large as they could possibly need to be, leaving some empty space at the end, or using a LinkedList, which doesn't need you to specify a
myncknma at 2007-7-21 22:50:14 > top of Java-index,Java Essentials,New To Java...
# 17
I'm sorry, I don't understand, could I have an example?
minkus46a at 2007-7-21 22:50:14 > top of Java-index,Java Essentials,New To Java...
# 18

Ok, here's what I have:

import java.io.*;

import java.util.*;

import java.text.*;

public class ZinniSpeech

{

private static String[] originalZinni;

private static int[] wordCount;

private static int wordTotal1;

private static int wordTotalFinal;

private static int count = 1;

public static void main(String args[])throws IOException

{

Scanner rawText = new Scanner(new File(

"\\Java\\Zinni\\ZinniSpeechCDI5_04.txt"));

rawText.useDelimiter("\\W+");

StringBuilder rawTextString = new StringBuilder();

while(rawText.hasNext())

{

rawTextString.append(" " + rawText.next());

}

rawText.close();

StringTokenizer rawTextToken = new StringTokenizer(

rawTextString.toString());

wordTotal1 = rawTextToken.countTokens();

originalZinni = new String[wordTotal1];

for (int x = 0; x < wordTotal1; x++)

{

originalZinni[x] = rawTextToken.nextToken();

String lowerCase = new String(originalZinni[x]);

originalZinni[x] = lowerCase.toLowerCase();

}

Arrays.sort(originalZinni);

wordCount = new int [wordTotal1+1];

for(int x = 0; x < wordTotal1-1; x++)

{

if(originalZinni[x].contentEquals(originalZinni[x+1]))

{

wordCount[x] = count++;

}

else if(!originalZinni[x].contentEquals(originalZinni[x+1]))

{

wordCount[x] = count++;

count = 1;

}

}

for(int x = 0; x < wordTotal1; x++)

{

System.out.println(originalZinni[x] + " = " + wordCount[x]);

}

}

private static void displayList(String[] originalZinni)

{

for(String word: originalZinni)

{

System.out.println(word);

}

}

}

Problems are it prints out:

word = 1

word = 2

word1= 1

word1=2

word1=3

And I only want the highest count.

minkus46a at 2007-7-21 22:50:14 > top of Java-index,Java Essentials,New To Java...
# 19
.
minkus46a at 2007-7-21 22:50:14 > top of Java-index,Java Essentials,New To Java...
# 20

> You might want to reconsider your use of the ++

> postfix operator.

> Keep in mind that it permanently adds to the value of

> the variable. and only after the statement has

> executed.

No, it's after the variable's value has been evaluated, not after the statement completes.

> Local variables are faster than class variables, by

> the way.

That may be so, but it's irrelevant. We don't decide whether a variable is local or a member for performance reasons.

jverda at 2007-7-21 22:50:14 > top of Java-index,Java Essentials,New To Java...
# 21

> for(int x = 0; x < wordTotal1; x++) {

> if(originalZinni[x].contentEquals(originalZinni[x++

> ])) {

> wordCount[x] = count++;

> }

> else

> lse

> if(!originalZinni[x].contentEquals(originalZinni[x++])

> ) {

> wordCount[x] = count++;

> count = 0;

> }

> }

We're incrementing x at least twice in each loop iteration here, and sometimes three times. Is that what you want?

jverda at 2007-7-21 22:50:14 > top of Java-index,Java Essentials,New To Java...
# 22

I think so, it prints out basically what I want. Here, this is what it looks like:

...

zinni = 1

zinni = 2

zinni = 3

zinni = 4

zinni = 5

zinni = 6

zinni = 7

zinni = 8

zinni = 9

zinni = 10

zinni = 11

zinni = 12

zinni = 13

zone = 1

zone = 2

zone = 3

zones = 0

My problems are I only want the highest counts, and zones (the last member of the array) is set = to 0, when it should be 1.

I want it to look like

zinni = 13

zone = 3

zones = 1

minkus46a at 2007-7-21 22:50:14 > top of Java-index,Java Essentials,New To Java...
# 23
.
minkus46a at 2007-7-21 22:50:14 > top of Java-index,Java Essentials,New To Java...
# 24
Could I have some help please?
minkus46a at 2007-7-21 22:50:14 > top of Java-index,Java Essentials,New To Java...
# 25
What exact problem are you having?
jverda at 2007-7-21 22:50:14 > top of Java-index,Java Essentials,New To Java...
# 26

The code and the description of my problem is above, if you want me to copy and move it down lower I can, but I think it's pretty clear. I'm not sure though, what part doesn't make sense? I want to print only the highest count. Also, the last number in the parallel array is returning a zero for some reason. Thanks for your help

minkus46a at 2007-7-21 22:50:14 > top of Java-index,Java Essentials,New To Java...
# 27
> The code and the description of my problem is above,Reply 18?What do you mean by "only want the highest count"?
jverda at 2007-7-21 22:50:14 > top of Java-index,Java Essentials,New To Java...
# 28
Yeah, reply 18, but also reply 22. 22 shows what I'm getting, and what I want it to be.
minkus46a at 2007-7-21 22:50:14 > top of Java-index,Java Essentials,New To Java...
# 29

One thing that's harmless but pointless:String lowerCase = new String(originalZinni[x]);

Get rid of new String.

String lowerCase = originalZinni[x];

Strings are immutable, so you don't need a new one. str = someString does NOT fill in the contents of a String object. Just like all reference variable assignments, it simply points the str reference variable at some object.

jverda at 2007-7-21 22:50:14 > top of Java-index,Java Essentials,New To Java...
# 30

I guess my question is, how do I design a for loop so it only takes the highest number of a single word. I want 1 array of words, and also an array of ints that represent the number of occurrences in the array. I've been fiddling with my code, and here is an updated version of what I have. I still can't get it into the parallel arrays though.

import java.io.*;

import java.util.*;

import java.text.*;

public class ZinniSpeech2

{

private static String[] originalZinni;

private static String adaptedString;

private static int[] wordCount;

private static int count = 1;

private static int wordTotal;

public static void main(String args[])throws IOException

{

Scanner rawText = new Scanner(new File(

"\\Java\\Zinni\\ZinniSpeechCDI5_04.txt"));

rawText.useDelimiter("\\W+\\s");

String adaptedString = (stringAdapter(rawText));

rawText.close();

StringTokenizer intoArray = new StringTokenizer(adaptedString);

wordTotal = intoArray.countTokens();

}

private static String stringAdapter(Scanner rawText)

{

StringBuilder rawTextString = new StringBuilder();

//*************************** NEEDS WORK *****************

while(rawText.hasNext())

{

rawTextString.insert(0, rawText.next() + " ");

char tempChar = rawTextString.charAt(0);

if(!Character.isLetterOrDigit(tempChar))

{

rawTextString.deleteCharAt(0);

}

}

//*****************************************************************

String tempString = rawTextString.toString();

StringTokenizer adapterToken = new StringTokenizer(tempString);

int tokenTotal = adapterToken.countTokens();

String[] tempArray = new String[tokenTotal];

for (int x = 0; x < tokenTotal; x++)

{

tempArray[x] = adapterToken.nextToken();

String lowerCase = new String(tempArray[x]);

tempArray[x] = lowerCase.toLowerCase();

}

Arrays.sort(tempArray);

StringBuilder adaptedSB = new StringBuilder();

for(String word: tempArray)

{

adaptedSB.append(word + " ");

}

adaptedString = adaptedSB.toString();

return adaptedString;

}

private static void displayList(String[] anArray)

{

for(String word: anArray)

{

System.out.println(word);

}

}

}

minkus46a at 2007-7-21 22:50:19 > top of Java-index,Java Essentials,New To Java...
# 31

Okay, so you only want one element from each array printed out, right? But it's printing out all of them, right?

Well, look at the code that you have that does that. How do you think "print all this array's elements" code would look? How do you think "print one particular element from this array" code would look?

jverda at 2007-7-21 22:50:19 > top of Java-index,Java Essentials,New To Java...
# 32

> I guess my question is, how do I design a for loop so

> it only takes the highest number of a single word.

Why do you want a loop?

Okay, look. You're sorting the arrays, right? So if the array is sorted, what's the loop for?

Or, if you want to use a loop, then you just loop over the array, comparing each value to the highest value so far. If that current value is larger, it becomes the new highest so far. Just like you'd do by hand.

jverda at 2007-7-21 22:50:19 > top of Java-index,Java Essentials,New To Java...
# 33
I know how to print one particular element, but the file I'm importing is thousands of words long. I don't know how to design the for-loop to create the parallel arrays. That's the main problem, actually creating the arrays.
minkus46a at 2007-7-21 22:50:19 > top of Java-index,Java Essentials,New To Java...
# 34
Right now, I have a long list of words alphabetically arranged in a string. I need to turn that into 2 parallel arrays. I just can't get my head around this one. Thanks for your help.
minkus46a at 2007-7-21 22:50:19 > top of Java-index,Java Essentials,New To Java...
# 35

Okay, I don't really understand what you're trying to do.

What are the parallel arrays for?

Didn't you sort the arrays? If an array is sorted, why would you need a for loop to get its max element?

Before you said the problem was that it was printing out too much stuff. Now you say the problem is creating the arrays. I really don't understand what you're trying to do or what difficulty you're having at this point.

jverda at 2007-7-21 22:50:19 > top of Java-index,Java Essentials,New To Java...
# 36
> Right now, I have a long list of words alphabetically> arranged in a string. I need to turn that into 2> parallel arrays. Why?What do those arrays represent? What's the relationship between them?
jverda at 2007-7-21 22:50:19 > top of Java-index,Java Essentials,New To Java...
# 37

I've been altering the code of the past couple hours, so now I don't have anything split up into arrays. I just have the one long string of alphabetized words: adaptedString. I want to form one array of the words, that includes only one copy of each word, and another arrays that keeps track of the occurrences in the string.

Example:

String "a, a, a, b, b, c, d, e, e"

word Array[a, b, c, d, e]

countArray[3, 2, 1, 1, 2]

minkus46a at 2007-7-21 22:50:19 > top of Java-index,Java Essentials,New To Java...
# 38

Don't use two arrays.

Use a Map (e.g. HashMap) where the key is the word and the value is the count.

http://java.sun.com/docs/books/tutorial/collections/

In any case, I'm still not sure exactly what problem you're having.

Creating the arrays? (Which will now become creating the Map.) In what way are you not able to get it?

Printing out just the largest element of an array? What problem are you having with that?

jverda at 2007-7-21 22:50:19 > top of Java-index,Java Essentials,New To Java...
# 39
I'm not familiar at all with maps, so I'd rather use arrays. I think I should use a for-loop to create both arrays, b/c I could just do it with one for-loop, but I'm not sure exactly what the loop should look like.
minkus46a at 2007-7-21 22:50:19 > top of Java-index,Java Essentials,New To Java...
# 40

> Don't use two arrays.

>

> Use a Map (e.g. HashMap) where the key is the word

> and the value is the count.

Unfortunately Jeff, I assume this is a half baked hoemwork assignment and they are forced to use arrays.

What you need to do is read a word. Search the array and if it is not present add it to the end. Then increment the value in the other array at the corresponding index. If it is present then just increment the other array.

floundera at 2007-7-21 22:50:19 > top of Java-index,Java Essentials,New To Java...
# 41

> I'm not familiar at all with maps, so I'd rather use

> arrays.

If you are allowed to use a Map, then do so. It is a lot easier. The whole point of learning is to do just that learn. So don't whine about not knowing something. Read about it and learn how to use a Map. The time it takes to learn about Maps will probably be less than trying to do this with parallel arrays.

floundera at 2007-7-21 22:50:19 > top of Java-index,Java Essentials,New To Java...
# 42
> I'm not familiar at all with maps, so I'd rather use> arrays.Unfamiliarity is no excuse for bug-prone code. Unless this is a homework assignment and you've been commanded to use two arrays, use the map.It's disgustingly simple.
paulcwa at 2007-7-21 22:50:19 > top of Java-index,Java Essentials,New To Java...
# 43
Yeah, that's what I keep trying to do, but nothing I do works. What should the loop look like?
minkus46a at 2007-7-21 22:50:19 > top of Java-index,Java Essentials,New To Java...
# 44

> I'm not familiar at all with maps, so I'd rather use

> arrays.

You'd rather? Or you're required to by the terms of the assignment?

> I think I should use a for-loop to create

> both arrays, b/c I could just do it with one

> for-loop, but I'm not sure exactly what the loop

> should look like.

Step through how you'd do it manually. Use pencil and paper.

jverda at 2007-7-21 22:50:20 > top of Java-index,Java Essentials,New To Java...
# 45

> Yeah, that's what I keep trying to do, but nothing I

> do works. What should the loop look like?

What is what you're trying to do? In what exact way did your best attempt not work and what was that attempt?

Honestly, you're not making it easy to help you. You're not communicating your problem very well.

jverda at 2007-7-21 22:50:25 > top of Java-index,Java Essentials,New To Java...
# 46
And it is an extra-credit assignment.
minkus46a at 2007-7-21 22:50:25 > top of Java-index,Java Essentials,New To Java...
# 47
> And it is an extra-credit assignment.And therefore...?
jverda at 2007-7-21 22:50:25 > top of Java-index,Java Essentials,New To Java...
# 48
> And it is an extra-credit assignment.No offense but extra credit assignments are usually desinged for that small percentage of students that fully understand what they are doing and can whip this up with little or no help.
floundera at 2007-7-21 22:50:25 > top of Java-index,Java Essentials,New To Java...
# 49

I get points for using parallel arrays, I could do it with the map, but there would be no point as we haven't covered it yet and it wasn't mentioned in the assignment.

> What you need to do is read a word. Search the array

> and if it is not present add it to the end. Then

> increment the value in the other array at the

> corresponding index. If it is present then just

> increment the other array.

This is what I'm trying to do, but I can't even get the code to compile. I'm doing a linear search, but I don't know how to get it to add a word to the array only when the word hasn't already been entered.

minkus46a at 2007-7-21 22:50:25 > top of Java-index,Java Essentials,New To Java...
# 50
You can write another method say getIndex. It searches through your array for the word and returns the index of where it is located or -1 if not found. Then depending on what value you get returned you can perform different behaviour in the calling method.
floundera at 2007-7-21 22:50:25 > top of Java-index,Java Essentials,New To Java...
# 51

> I get points for using parallel arrays,

Then your teacher is incompetent.

> I could do it

> with the map, but there would be no point

There would be a point. You'd learn something useful. Doing it with parllel arrays is just teaching you bad practice.

> > What you need to do is read a word. Search the

> array

> > and if it is not present add it to the end. Then

> > increment the value in the other array at the

> > corresponding index. If it is present then just

> > increment the other array.

>

> This is what I'm trying to do, but I can't even get

> the code to compile.

Okay, and...? The only answer I can give to that is: Fix your syntax errors.

> I'm doing a linear search, but

> I don't know how to get it to add a word to the array

> only when the word hasn't already been entered.

Look at each element in the array. If the current element equals the one you're looking for, update the count for that word, and break out of the loop. If you reach the end of the array without having found what you're looking for, add that one to the array with a count of 1.

Like I said: Just like you'd do it with pencil and paper, which was apparenty not worth your effort for this extra credit assignment.

I'm outta here. Good luck.

jverda at 2007-7-21 22:50:25 > top of Java-index,Java Essentials,New To Java...
# 52

I just can't get that loop figured out, this is what I've got. I'm really sorry I'm so incompetent. It's probably just a dumb mistake, but I've spent about 6 hours trying to get this little bit of code figured out and I'm just beat to death. Could somebody please tell me what I'm doing wrong?

private static void computeParallelArrays(StringTokenizer intoArray)

{

String[] tempArray = new String[wordTotal];

for(int x = 0; x < wordTotal; x++)

{

tempArray[x] = intoArray.nextToken();

}

getIndex(tempArray, wordArray);

System.out.println(count);

displayList(wordArray);

}

private static int getIndex(String[] tempArray, String[] wordArray)

{

for(int x = 0; x < tempArray.length; x++)

{

wordArray = new String[tempArray.length];

wordArray[x] = tempArray[x];

if(tempArray[x].equals(wordArray[x]))

{

count++;

}

else

{

count = 1;

}

}

displayList(wordArray);

return count;

}

private static void displayList(String[] anArray)

{

for(String word: anArray)

{

System.out.println(word);

}

}

I eventually want to turn the counts into another array, but I can't even get the words to work right. I'm getting a long print out of nulls, then the last word in the tempArray appears. Thank you so much for your help.

minkus46a at 2007-7-21 22:50:25 > top of Java-index,Java Essentials,New To Java...
# 53
First thing to do, Java is an Object Oriented language. So why not make use of objects? If you do that then you can make your two arrays instance variables and you won't have to continually pass them around as parameters.
floundera at 2007-7-21 22:50:25 > top of Java-index,Java Essentials,New To Java...
# 54

My rule of thumb is that you should only create instance variables if their data truly qualifies as state. Of course "state" is a slippery concept to tie down.

But on the other hand...it looks like he already has created those two arrays as fields, and he's passing them as arguments sometimes but using them as fields other times.

paulcwa at 2007-7-21 22:50:25 > top of Java-index,Java Essentials,New To Java...
# 55

> I just can't get that loop figured out, this is what

> I've got. I'm really sorry I'm so incompetent.

Being incompetent is nothing to be sorry for in this situation. Everybody is new at some point, and not everybody catches on naturally. There's nothing wrong with that.

Nobody's angry at you. We just don't understand what your problem is (well, I don't anyway) and therefore don't know how to help you.

> It's

> probably just a dumb mistake,

Probably. I've been doing this professionally for almost 15 years, and I still make lots of dumb mistakes. Just found a major "D'OH!" today that made total sense a few months ago.

Don't worry about it. It happens. Because you're a student, the point isn't to produce perfect code. The point is to learn the principles and techniques.

The syntax of for (blah blah) and someArray[whatever] is minor. That's NOT what this stuff is about.

So....

Take a step back, take a deep breath, and think about the problem in the abstract. Without regard to Java or any programming language, BUT keeping in mind a rough idea of the main constructs (looping for doing the same thing multiple times, methods for separating out a small group of steps into one unit, variables for holding values, etc.), figure out how you would do it "manually." That is, write a bunch of very simple, brain-dead steps, then translate them to Java. When any step doesn't translate easily to Java, that's a sign that that step is too complicated, or trying to do too much, so break it down further.

Solving the problem. Divide and conquer. Clearly expressing the requirements and how you meet them. Expressing your intent in code in the most direct, easily understandable way possible. THIS stuff is the meat of what software development is about.

> but I've spent about 6

> hours

Oooh. Six whole hours. I spend more time than that on a dozen or two lines of code, and get paid good money for it. But they're a dozen or two lines of good code.

> please tell me what I'm doing wrong?

* Poor communication.

* Thinking about "how do I make a wobbadobba system in Java" rather than breaking the problem into pieces and thinking about them irrespective of Java.

* Ignoring the advice about using a map instead of parallel arrays. You're just making it harder for yourself. Good programmers are lazy.

jverda at 2007-7-21 22:50:25 > top of Java-index,Java Essentials,New To Java...