I couldn’t make this programm run as I wanted

Your code has few errors. From

Usage: java Phone

it looks like expected content of args array should be "Phone" <name> which are two elements so

if(args.length != 1) 

is not valid condition. You probably should replace it with

if (args.length < 2)

Other problem is that <name> is second element in args array stored in args[1] so


should be

if(numbers[i][0].equals(args[1])) //we want to compare name, not "Phone" string

Last problems involve

if (i == numbers.length);
    System.out.println("Name not found.");
  • inside for loop i will never be equal number.length because for (i = 0; i < numbers.length; i++) loop iterates only if i<number.length. So this condition should be replaced with

    if (i == numbers.length -1)
  • there is semicolon right after this condition which represents empty instruction, which means that

    if (i == numbers.length - 1);
        System.out.println("Name not found.");

    is essentially same as

    if (i == numbers.length - 1)
    System.out.println("Name not found.");

    which means that execution of System.out.println("Name not found."); doesn’t depend on result of if condition.

    To solve this problem simply remove this additional ;, and to avoid this problem always surround code which should depend of if else for while inside blocks {...}.

Browse More Popular Posts

Leave a Comment