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 equalnumber.length
becausefor (i = 0; < numbers.length; i++)
loop iterates ifi<number.length
. condition should replacedif (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 ofif
condition.to solve problem remove additional
;
, , avoid problem surround code should depend ofif
else
for
while
inside blocks{...}
.
Comments
Post a Comment