java - I couldn't make this programm run as I wanted -


public class phone {     public static void main(string args[]) {          string numbers[][] = { { "tom", "555-3322" }, { "mary", " 555-8976" },                 { "jon", "555-1037" }, { "rachel", "555-1400" } };          int i;          if (args.length != 1)             system.out.println("usage: java phone <name>");         else {             (i = 0; < numbers.length; i++) {                 if (numbers[i][0].equals(args[0])) {                     system.out.println(numbers[i][0] + ": " + numbers[i][1]);                     break;                 }                 if (i == numbers.length)                     ;                 system.out.println("name not found.");             }         }     } } 

so after trie run theses line of code,i didn't other result, apart first sysout statement.

your code has few errors.

usage: java phone

it looks expected content of args array should "phone" <name> 2 elements

if(args.length != 1)  

is not valid condition. should replace

if (args.length < 2) 

other problem <name> second element in args array stored in args[1]

if(numbers[i][0].equals(args[0]))  

should

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

last problems involve

if (i == numbers.length);     system.out.println("name not found."); 
  • inside loop i never equal number.length because for (i = 0; < numbers.length; i++) loop iterates if i<number.length. condition should replaced

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

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

    is same

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

    which means execution of system.out.println("name not found."); doesn't depend on result of if condition.

    to solve problem remove additional ;, , avoid problem surround code should depend of if else for while inside blocks {...}.


Comments

Popular posts from this blog

css - SVG using textPath a symbol not rendering in Firefox -

Java 8 + Maven Javadoc plugin: Error fetching URL -

order - Notification for user in user account opencart -