为了测试,我在一个文本文件中有三个名字。
Joe ,Smith
Jim ,Jones
Bob ,Johnson
s=reader.readLine();
我通过在循环末尾添加一秒来修复永恒循环while
,但是当我运行下面的代码时,我得到以下输出:
JoeSmith
JoeSmith
JimJones
JimJones
BobJohnson
BobJohnson
如何防止重复名称?我的第二个s=reader.readLine();
位置不正确吗? *废话。没关系。我正在打印源数据和从中创建的数组字段。哦。
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);
}
}
}