tags:

views:

114

answers:

1

Hey guys, I'm new to Java (well, 3/4 of a year spent on it).

So I don't know much about it, I can do basic things, but the advanced concepts have not been explained to me, and there is so much to learn! So please go a little but easy on me...

Ok, so I have this project where I need to read lines of text from a file into an array but only those which meet specific conditions. Now, I read the lines into the array, and then skip out on all of those which don't meet the criteria. I use a for loop for this. This is fine, but then when I print out my array (required) null values crop up all over the place where I skipped out on the words.

How would I remove the null elements specifically? I have tried looking everywhere, but the explanations have gone way over my head!

Here is the code that I have to deal with the arrays specifically: (scanf is the scanner, created a few lines ago):

//create string array and re-open file
    scanf = new Scanner(new File ("3letterWords.txt"));//re-open file
    String words [] = new String [countLines];//word array
    String read = "";//to read file

    int consonant=0;//count consonants
    int vowel=0;//count vowels

    //scan words into array
    for (int i=0; i<countLines; i++)
    {
        read=scanf.nextLine();

        if (read.length()!=0)//skip blank lines
        {    
            //add vowels
            if (read.charAt(0)=='a'||read.charAt(0)=='e'||read.charAt(0)=='i'||read.charAt(0)=='o'||read.charAt(0)=='u')
            {
                if (read.charAt(2)=='a'||read.charAt(2)=='e'||read.charAt(2)=='i'||read.charAt(2)=='o'||read.charAt(2)=='u')
                {
                    words[i]=read;
                    vowel++;
                }
            }
            //add consonants
            if (read.charAt(0)!='a'&&read.charAt(0)!='e'&&read.charAt(0)!='i'&&read.charAt(0)!='o'&&read.charAt(0)!='u')
            {
                if (read.charAt(2)!='a'&&read.charAt(2)!='e'&&read.charAt(2)!='i'&&read.charAt(2)!='o'&&read.charAt(2)!='u')
                {
                    words[i]=read;
                    consonant++;
                }
            }

        }//end if

        //break out of loop when reached EOF
        if (scanf.hasNext()==false)
            break;

    }//end for

    //print data
    System.out.println("There are "+vowel+" vowel words\nThere are "+consonant+" consonant words\nList of words: ");

   for (int i=0; i<words.length; i++)
       System.out.println(words[i]);

Thanks so much for any help received!

+2  A: 

Just have a different counter for the words array and increment it only when you add a word:

int count = 0;

for (int i=0; i<countLines; i++) {
  ...
  // in place of: words[i] = read;
  words[count++] = read;
  ...
}

When printing the words, just loop from 0 to count.


Also, here's a simpler way of checking for a vowel/consonant. Instead of:

if (read.charAt(0)=='a'||read.charAt(0)=='e'||read.charAt(0)=='i'||read.charAt(0)=='o'||read.charAt(0)=='u')

you can do:

if ("aeiou".indexOf(read.charAt(0)) > -1)

Update: Say read.charAt(0) is some character x. The above line says look for that character in the string "aeiou". indexOf returns the position of the character if found or -1 otherwise. So anything > -1 means that x was one of the characters in "aeiou", in other words, x is a vowel.

casablanca
Oh, thank you so much! It works perfectly now! Never would have thought of that! My question is solved :D
Strategist01
OK, can you explain that last line to me please? I don't get the "indexOf" or the " > -1" part!
Strategist01
Glad that your question was answered! Could you please vote for casablanca's answer and accept it (to vote, just click on the up arrow and to accept the answer, click on the empty checkmark).
Barthelemy
Uh, I can't vote up, it says I don't have enough reputation...
Strategist01
I've added an explanation of `indexOf` to the answer.
casablanca
Ah, thank you. So if I were to search for a consonant would I use a <= -1, or a == -1?
Strategist01
Yes, `== -1` will do. In your case though, since you already have an `if` condition for vowels, an `else` would mean the same thing.
casablanca