dwwilson66
dwwilson66

Reputation: 7066

Why am I looping while reading a text file in Java?

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);
    }
   }
  }

Upvotes: 1

Views: 2203

Answers (2)

Greg Kopff
Greg Kopff

Reputation: 16545

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.

Upvotes: 2

Synesso
Synesso

Reputation: 38958

Put s=reader.readLine(); again at the end of your while loop. Eventually it will become null and your loop will exit.

Upvotes: 2

Related Questions