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 9052639
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T13:13:32+00:00 2026-06-16T13:13:32+00:00

I have a program that takes input from a file, saves each word in

  • 0

I have a program that takes input from a file, saves each word in the file as a token and then adds each token to an array list.

The problem is that the arrayList comes up as for example [“cat”,”dog”,” “,” “,”bird”], and I don’t want the spaces in the arrayList.

And the file that is read is set up as shown below:

cat dog


bird

It is obvious that it is the blank lines cause the spaces, but the blank lines are necessary.

Anyway, my code is below:

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

public class NewMain{

public static void main(String[] args){

    try{
        FileInputStream fstream = new FileInputStream("Filename");

        //Get the object of datainputstream
        DataInputStream in = new DataInputStream(fstream);
        BufferedReader br = new BufferedReader(new InputStreamReader(in));
        String strLine;

        List<String> listOfWords = new ArrayList<String>();
       while((strLine = br.readLine()) != null){
        String [] tokens = strLine.split("\\s+");
        String [] words = tokens;
        for(String word : words){
            listOfWords.add(word);
            System.out.print(word);
            System.out.print(" ");      
        } 
        System.out.print("\n");
    }
       System.out.println(listOfWords);

        List<String> space = new ArrayList<String>();
        String[] spaces = {" "};
        space.addAll(Arrays.asList(spaces));

        editList(listOfWords,space);

        System.out.println(listOfWords);
in.close();
    }
    catch(Exception e){
        System.err.println("Error: " + e.getMessage());    
    }  
}

public static void editList(Collection<String> list1, Collection<String> list2){
    Iterator<String> it = list1.iterator();
        while(it.hasNext()){         
       if(list2.contains(it.next())) {
                it.remove();
            }  
       }
}
} 

The String[] spaces = {" "}; should remove the blank spaces, as I have tested it by removing spaces from an non-file arrayList. And the strange thing is that if I change it to String[] spaces = {"cat"}; it will remove cat from the arrayList.

  • 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-06-16T13:13:34+00:00Added an answer on June 16, 2026 at 1:13 pm

    in your for loop add an if condition:

    for(String word : words){
                if(!word.equals(""))  /* OR if( (word.length > 0) )*/  {
                listOfWords.add(word);
                System.out.print(word);
                System.out.print(" ");   
               }   
            } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a program that takes a list of names from a file and
I have a perl program that takes input and output file arguments, and I'd
I have a program that either takes data from a file or from the
I have to create a program that takes an input file of unsorted numbers
I am trying to make a program that takes input from a file and
I am making a program that takes input from files that I have called
I am writing a program that takes in an input file from the user.
  I have a file copy program that takes from one file and pastes in
I have a program that saves an image in a local directory and then
Currently I have a program that takes as input a folder that contains .MPP

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.