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