Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3219344
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T15:38:05+00:00 2026-05-17T15:38:05+00:00

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

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T15:38:05+00:00Added an answer on May 17, 2026 at 3:38 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey guys, I don't know RegExp yet. I know a lil about it but
Hey guys, I want to parse some xml but I don't know how I
Hey guys got an issue with Cakephp validation.. I want to know why is
Hey guys can someone explain the big o notation for each of the following
hey guys, how can I just achieve this simple layout? I'm hoping the answer
Hey guys, I am very new to AJAX and and working on a rating
Hey guys so I'm a bit new to Android programming and I have an
Hey guys I am new to android development, I am currently making an application.The
hey guys i wanna ask a question about sending data to database to server
Hey guys I am new to the iOS world so please bear with me.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.