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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T16:26:49+00:00 2026-06-02T16:26:49+00:00

For testing, I’ve got three names in a text file. Joe ,Smith Jim ,Jones

  • 0

For testing, I’ve got three names in a text file.

Joe       ,Smith
Jim       ,Jones
Bob       ,Johnson

I fixed the eternal looping by adding a second s=reader.readLine(); at the end of my while loop, but when I run the code below, I get the following output:

JoeSmith
JoeSmith
JimJones
JimJones
BobJohnson
BobJohnson

How can I prevent the duplicate names? Is my second s=reader.readLine(); placed incorrectly? * Crap. Nevermind. I’m printing the source data and the array fields created from it. Oy.

import java.nio.file.*;
import java.io.*;
import java.nio.channels.FileChannel;
import java.nio.ByteBuffer;
import static java.nio.file.StandardOpenOption.*;
import java.util.Scanner;
import java.text.*;
import javax.swing.JOptionPane;
//
public class VPass
{
    public static void main(String[] args)
    {
        final String FIRST_FORMAT = "          ";
        final String LAST_FORMAT = "          ";
        String delimiter = ",";
        String s = FIRST_FORMAT + delimiter + LAST_FORMAT ;
        String[] array = new String[2];
        Scanner kb = new Scanner(System.in);
        Path file = Paths.get("NameLIst.txt");
        try
        {    
            InputStream iStream=new BufferedInputStream(Files.newInputStream(file));
            BufferedReader reader=new BufferedReader(new InputStreamReader(iStream));
            s=reader.readLine();
            while(s != null)
            {
                array = s.split(delimiter);
                String firstName = array[0];
                String lastName = array[1];
                System.out.println(array[0]+array[1]+"\n"+firstName+lastName);
                s=reader.readLine();
            }
    }
    catch(Exception e)
    {
        System.out.println("Message: " + e);
    }
   }
  }
  • 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-02T16:26:49+00:00Added an answer on June 2, 2026 at 4:26 pm

    You never update s after the first loop iteration.

    You code needs to be more along the lines of:

    while ((s = reader.readLine()) != null)
    {
      array = s.split(delimiter);
      String firstName = array[0].trim();
      String lastName = array[1].trim();
      System.out.println(array[0]+array[1]+"\n"+userName+password);
    }
    

    Edit: added trim() suggestion as per Sanchit’s comment.


    Subsequent edit after the question changed:

    I fixed the eternal looping by adding a second s=reader.readLine(); at the end of my while loop, but when I run the code below, I get the following output:

    JoeSmith

    JoeSmith

    JimJones

    JimJones

    BobJohnson

    BobJohnson

    If we look at your code:

    while(s != null)
    {
      array = s.split(delimiter);
      String firstName = array[0];
      String lastName = array[1];
      System.out.println(array[0]+array[1]+"\n"+firstName+lastName);   // <-- this prints 2 lines of output
      s=reader.readLine();
    }
    

    … you see you output 2 lines of output for every loop iteration.

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

Sidebar

Related Questions

Testing environment: netbeans7.11 + glassfish 3.11 here is the jsp, class code: <%@page contentType=text/html
For testing purpose, I need to create logs in a CSV file in my
Testing a text-to-speech engine is a rather daunting task. The engine itself parses input
For testing purposes I have to generate a file of a certain size (to
While testing the below SQL code for another stackoverflow answer , I got the
While testing this responsive design I'm currently working on, I've noticed a weird text
Testing out someone elses code, I noticed a few JSP pages printing funky non-ASCII
Testing UI is difficult. What do you think is the best unit testing framework
Testing in various versions of Firefox new and old it seems that column-span in
Duplicate: Testing a website for cross-browser/multiple-version support How do you test visual components I

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.