1

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);
    }
   }
  }
dwwilson66
  • 6,806
  • 27
  • 72
  • 117
  • FYI: if you're trying to implement the CSV (comma-separated values) or similar format, there are several Java implementations already out there which will handle escaping and other "features" that commonly cause more naive implementations to break. See http://stackoverflow.com/questions/101100/csv-api-for-java – rob Apr 27 '12 at 02:32

2 Answers2

2

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

Synesso
  • 37,610
  • 35
  • 136
  • 207
2

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.

Greg Kopff
  • 15,945
  • 12
  • 55
  • 78
  • I'd also use a trim in there somewhere as the OP has a lot of spaces between the first word and the comma. String firstName =array[0].trim(); – Sanchit Apr 27 '12 at 01:50
  • The thing about `while ((s = reader.readLine()) != null)` is that you're mixing an assignment with an expression. Many would consider this bad practice (myself included). – Synesso Apr 27 '12 at 02:28
  • I would normally avoid it in basically every other case, but I find this a compact solution and allow myself this one transgression. :-) Don't forget that a for loop does an assignment and expression in one line too! – Greg Kopff Apr 27 '12 at 02:39