删除数组的空值

时间:2018-10-15 23:08:38

标签: java

该代码为用户输入名称,为生日输入第二个输入。它最多可以存储10个条目namebirthday,并且可以通过输入“ ZZZ”提前终止。我弄清楚了大部分代码,但是我不知道的部分是,如果条目在10之前终止,那么会有一段文本,上面写着[adam, john, dave, null, null, null,....]

import java.util.*;
public class BirthdayReminderRedo {

    public static void main(String[] args) {
        String[] name = new String[10];
        String[] birthday = new String[10];
        String[] selectName = new String[100];
        String inputName;
        Scanner userInput = new Scanner(System.in);
        int count;

        for(count=0; count < 10; count++){
            System.out.println("Please enter a name or type ZZZ to end name inputs>>");
            inputName = userInput.nextLine();
            if(inputName.equals("ZZZ")){
                while(name.remove(null)){}
                System.out.println(count);
                System.out.println(name);
                //System.out.println(Arrays.toString(name));
                break;
            }
            else{
                name[count] = inputName;
            }

            if(count == 10){
                for(int secondCount = 0; secondCount > 0; secondCount++);
                break;
            }
            else{
                System.out.println("Please enter birthday in the format DD/MM/YYYY>>");
                birthday[count] = userInput.nextLine();
            }

        }
        String dataCheck = null;
        do{
            for(int secondCount = 0; secondCount < 10; secondCount++){
                System.out.println("Please enter a name to get the birthday or enter ZZZ to end program>>");
                userInput = new Scanner(System.in);
                dataCheck = userInput.nextLine();
                selectName[secondCount] = dataCheck;
                boolean valid = false;
                if(selectName[secondCount].equals("ZZZ")){
                    System.out.println("Thank you for using this program");
                    break;
                }
                for(int thirdCount = 0; thirdCount < 10; thirdCount++){
                    if(selectName[secondCount].equals(name[thirdCount])){
                        System.out.println(birthday[thirdCount]);
                        valid = true;
                    }
                    else if (thirdCount == 9 && !valid){
                        System.out.println("Not a valid name");
                    }
                }
            }
        } while(!"ZZZ".equals(dataCheck));
    }
}

关于如何从此null中删除println的任何提示?

2 个答案:

答案 0 :(得分:0)

代替使用数组。为什么不利用Lists并实例化ArrayList,以使您不必担心集合中多余/未定义的元素?

List<String> name = new ArrayList<String>();
List<String> birthday = new ArrayList<String>();
String[] selectName = new String[100];
String inputName;
Scanner userInput = new Scanner(System.in);
int count;

for(count=0; count < 10; count++){
    System.out.println("Please enter a name or type ZZZ to end name inputs>>");
    inputName = userInput.nextLine();
    if(inputName.equals("ZZZ")){
        break;
    }
    else{
        name.add(inputName);
    }

    if(count == 10){ //Sunny - Count will never be 10
        for(int secondCount = 0; secondCount > 0; secondCount++);
        break;
    }
    else{
        System.out.println("Please enter birthday in the format DD/MM/YYYY>>");
        birthday.add(userInput.nextLine());
    }
}

答案 1 :(得分:0)

如果您坚持使用数组而不是:

  1. 如果要使用Arrays.toString,请将值复制到初始化为输入数量的新数组中。

  2. 如果您不喜欢使用该方法,也可以按以下方式打印它们:

     for(String n: name) 
     { 
        if(n!=null)  
            System.out.print(n + " "); 
     }
    

如果使用数组并不重要,请按照另一个答案的建议进行操作;使用ArrayList。