How do I remove/not display the null values from two arrays?

My code works fine. However the output seems incorrect when I enter -1 from name or age input. How do I remove null values and "-1" and display the existed array?

import java.util.Scanner;

public class quizLoop {
    private static Scanner key = new Scanner(System.in);
    private static Scanner keyNum = new Scanner(System.in);

    public final static int arrayLoop = 5;
    public static String[] nameList = new String[arrayLoop];
    public static int[] age = new int[arrayLoop];

    public static void main(String[] args) {
        System.out.println("NAME & AGE SYSTEM\n-----------------\n");
        for(int i=0; i<arrayLoop; i++) {
            System.out.print("Name: ");
            nameList[i] = key.nextLine();
            if(nameList[i].equals("-1"))
                break;

            System.out.print("Age: ");
            age[i] = keyNum.nextInt();
            if(age[i] < 0)
                break;
        }

        System.out.println("----------");
        for(int i=0; i<nameList.length; i++) {
            System.out.println(nameList[i] + " " + age[i]);
        }
    }
}

Answers


Currently, when you input -1, the loop exits. That is, as soon as you input -1, the loop is not run again. This is because you use the break statement.

If you'd like to let -1 allow the user to start the current entry again, you'll need to do two things:

if (nameList[i].equals("-1")) {
    // Take the loop variable down one.
    i--;
    // Instead of break, continue to the next iteration.
    continue;
}

If you want to keep the loop how it is, but only print non-null values, modify your printing code:

for(int i=0; i<nameList.length; i++) {
    if (nameList[i] == null || nameList[i].equals("-1") || age[i] < 0) {
        // Invalid; go to the next one.
        continue;
    } else { // (not strictly necessary)
        System.out.println(nameList[i] + " " + age[i]);
    }
}

Try to use a List instead a Array, something like this:

import java.util.Scanner;

public class quizLoop {
private static Scanner key = new Scanner(System.in);
private static Scanner keyNum = new Scanner(System.in);

public final static int arrayLoop = 5;
public static List<String> nameList = new ArrayList<String>();
public static List<Integer> ages = new ArrayList<Integer>();

public static void main(String[] args) {
    System.out.println("NAME & AGE SYSTEM\n-----------------\n");

    while (true){
        System.out.print("Name: ");
        String name = key.nextLine();
        if(name.equals("-1"))
            break;

        System.out.print("Age: ");
        Integer age = keyNum.nextInt();
        if(age < 0)
            break;
        nameList.add(name);
        ages.add(age);
    }

    System.out.println("----------");
    for(int i=0; i<nameList.length; i++) {
        System.out.println(nameList[i] + " " + age[i]);
    }
}
}

Need Your Help

How do I nest resource identifiers?

.net msdn resx guidelines

The page Names of Resources on MSDN says the following.

Impossible to center exactly li's inside a div

html css

Please, give me some help in the following:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.