Java字符串循环数组Null

Java字符串循环数组Null,java,arrays,string,null,duplicates,Java,Arrays,String,Null,Duplicates,所以我收到了一组电子邮件,我应该读入它们,将它们存储在一个数组中,删除重复的,然后打印“剩余”。我几乎可以做到这一点,但在删除重复项后,当我打印剩余项时,它会额外打印一个null 这是我的密码。有人能告诉我修理它的方向吗 public class Duplicate { public static void main(String [] args){ Scanner keyboard = new Scanner(System.in); System.out

所以我收到了一组电子邮件,我应该读入它们,将它们存储在一个数组中,删除重复的,然后打印“剩余”。我几乎可以做到这一点,但在删除重复项后,当我打印剩余项时,它会额外打印一个
null

这是我的密码。有人能告诉我修理它的方向吗

public class Duplicate {
    public static void main(String [] args){
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Enter file name: ");
        String fileName = keyboard.nextLine();
        if(fileName.equals("")){
            System.out.println("Error: User did not specify a file name.");
        }
        else{Scanner inputStream = null;

        try{inputStream = new Scanner(new File(fileName));
        }
        catch(FileNotFoundException e){
            System.out.println("Error: "+ fileName + " does not exist.");
            System.exit(0);
        }


        String [] address = new String[100];

        for(int i=0;inputStream.hasNextLine();i++){
            String email = inputStream.nextLine();
            address[i]=email.toLowerCase();
            //System.out.println(address[i]);
        }


        Set<String> mail = new HashSet<String>(Arrays.asList(address));

        for(String email:mail){
            System.out.println(email);
        }
公共类重复{
公共静态void main(字符串[]args){
扫描仪键盘=新扫描仪(System.in);
System.out.println(“输入文件名:”);
字符串文件名=keyboard.nextLine();
if(fileName.equals(“”){
System.out.println(“错误:用户未指定文件名”);
}
else{Scanner inputStream=null;
尝试{inputStream=newscanner(新文件名));
}
catch(filenotfounde异常){
System.out.println(“错误:“+fileName+”不存在。”);
系统出口(0);
}
字符串[]地址=新字符串[100];
for(int i=0;inputStream.hasNextLine();i++){
字符串email=inputStream.nextLine();
地址[i]=email.toLowerCase();
//System.out.println(地址[i]);
}
Set mail=newhashset(Arrays.asList(address));
用于(字符串电子邮件:mail){
System.out.println(电子邮件);
}

我假设您读取的地址少于100个。数组地址中的其余元素为空。这就是空值的原因

将固定大小的数组替换为
ArrayList

List address=new ArrayList();
//...
add(email.toLowerCase());
// ...
还必须替换集合的构造:

Set<String> mail = new HashSet<String>(address);
Set mail=newhashset(地址);

您正在尝试从文件中读取固定数量的[100]元素

如果有更多的电子邮件,您将错过一些,如果少一些,您将剩下一些
null
s[这些
Set
将折叠成单个
null
]

尝试使用而不是数组,或者从一开始就使用

String [] address = new String[100];  

SortedSet<String> address = new TreeSet<String>();   

address.add(email.toLowerCase());
只要这样做:

Set<String> addresses = new HashSet<String>(); // use a Set

while (inputStream.hasNextLine()) {
    addresses.add(inputStream.nextLine().toLowerCase()); // in-line unused variable
}

for (String email : mail) {
    System.out.println(email);
}
Set addresses=new HashSet();//使用一个集合
while(inputStream.hasNextLine()){
address.add(inputStream.nextLine().toLowerCase());//行内未使用的变量
}
用于(字符串电子邮件:mail){
System.out.println(电子邮件);
}

您的代码有几个问题

  • 您可以使用扫描仪读取文件,这是我很少看到的,我自己也从来没有这样做过。FileInputStream类为此目的进行了更优化

  • 使用固定大小的数组存储未知数量的字符串

  • 对于未知数量的循环迭代,使用for循环。这没有错,但是while循环更合适

  • 您从数组创建了一个新的哈希集。同样没有错,但是为什么您没有在循环中使用哈希集呢?您可以避免同时使用索引

我假设你是一个刚开始编程的人,刚刚编写了一些代码来自动执行一些你不想手动执行的操作。当然,这没什么错,在这种情况下,只需使用ArrayList,因为在你的情况下,所有字符串对象都保证是唯一的:

public class Duplicate {
 public static void main(String [] args){

Scanner keyboard = new Scanner(System.in);
System.out.println("Enter file name: ");
String fileName = keyboard.nextLine();
if(fileName.equals("")){
    System.out.println("Error: User did not specify a file name.");
}
  else{Scanner inputStream = null;

try{inputStream = new Scanner(new File(fileName));
}
catch(FileNotFoundException e){
    System.out.println("Error: "+ fileName + " does not exist.");
    System.exit(0);
}


ArrayList<String> addresses = new ArrayList<String>();

for(int i=0;inputStream.hasNextLine();i++){
    String email = inputStream.nextLine();
    address.add(email.toLowerCase());
    //System.out.println(email);
}

for(String email:addresses){
    System.out.println(email);
}
公共类重复{
公共静态void main(字符串[]args){
扫描仪键盘=新扫描仪(System.in);
System.out.println(“输入文件名:”);
字符串文件名=keyboard.nextLine();
if(fileName.equals(“”){
System.out.println(“错误:用户未指定文件名”);
}
else{Scanner inputStream=null;
尝试{inputStream=newscanner(新文件名));
}
catch(filenotfounde异常){
System.out.println(“错误:“+fileName+”不存在。”);
系统出口(0);
}
ArrayList地址=新的ArrayList();
for(int i=0;inputStream.hasNextLine();i++){
字符串email=inputStream.nextLine();
add(email.toLowerCase());
//System.out.println(电子邮件);
}
用于(字符串电子邮件:地址){
System.out.println(电子邮件);
}

通过适当的导入。注意:这是最小的更改,仍有一些地方可以改进,请参见上文。祝你好运!

这是否仍然允许我读取多达100个地址?因为它将以随机数量进行测试,非常不雅观:从开始使用
设置
,并删除副本以设置code是它will.Nosid很合适,但与其使用数组/数组列表,为什么不立即使用你的集合?在读取流之前创建集合,并将地址添加到集合中。老实说,我真的不知道如何使用集合。我才刚刚开始,从这个网站上获得了整个集合。+1:没有理由不只是我们一个
Set
f从一开始。像前面的问题一样,这听起来也像是家庭作业。请确保使用
家庭作业
标签。
Set<String> addresses = new HashSet<String>(); // use a Set

while (inputStream.hasNextLine()) {
    addresses.add(inputStream.nextLine().toLowerCase()); // in-line unused variable
}

for (String email : mail) {
    System.out.println(email);
}
public class Duplicate {
 public static void main(String [] args){

Scanner keyboard = new Scanner(System.in);
System.out.println("Enter file name: ");
String fileName = keyboard.nextLine();
if(fileName.equals("")){
    System.out.println("Error: User did not specify a file name.");
}
  else{Scanner inputStream = null;

try{inputStream = new Scanner(new File(fileName));
}
catch(FileNotFoundException e){
    System.out.println("Error: "+ fileName + " does not exist.");
    System.exit(0);
}


ArrayList<String> addresses = new ArrayList<String>();

for(int i=0;inputStream.hasNextLine();i++){
    String email = inputStream.nextLine();
    address.add(email.toLowerCase());
    //System.out.println(email);
}

for(String email:addresses){
    System.out.println(email);
}