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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T08:19:39+00:00 2026-05-31T08:19:39+00:00

While adding the lines of a text file to List , i donot want

  • 0

While adding the lines of a text file to List , i donot want to add empty lines to the List. I tried this but not working :

Scanner s=new Scanner(new File(text.txt));
List <String> names=new ArrayList<String>();
while(s.hasNext()){
if (s.nextLine()!=null){
names.add(s.nextLine());
}}

My text file looks like this:

name1   erer    | 26-08-1988

name2 rerer   | 13-07-1988       

name3  erer    | 14-07-1988   

name4  weww |   13-07-2001   
name5 ewew | 18-10-1987   
  • 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-31T08:19:40+00:00Added an answer on May 31, 2026 at 8:19 am

    You skip a line each time (double call to nextLine() in each iteration), should be:

    String line;
    if ((line=s.nextLine())!=null && !line.trim().equals("")){
        names.add(line);
    }
    

    or even:

    if (!"".equals(line=s.nextLine()))
    

    Code edited to reflect (correct) comments.

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

Sidebar

Related Questions

Is there any way to add bouncy effect while adding marker on map in
i have two list, i wanted to update the second list while adding a
I am implementing a jquery file upload page. While a user is adding files,
i have this piece of function..and i want to add a new line to
I've been working on this small piece of code that seems trivial but still,
I am inputting this text file into the command line: 800 5 10 800
While adding two language code files i.e. C# and VB.NET to App_Code, got an
I can't view header in JTable while adding it into a JFrame.. String[] col={Name,ID,Marks};
I am getting the following exception while adding data into database: org.hibernate.HibernateException: The database
Using VB6 In a folder, am having n number of text files, file names

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.