java programming // please this question it is confusing me ...could someone hel
ID: 659687 • Letter: J
Question
java programming
// please this question it is confusing me ...could someone help me ?
//.........................................................................
public class WordCommandLine
{ public static void main (String [ ] args)
{ String word1 ;
String word2 ;
word1 = args [0] ;
word2 = args [1] ;
if ( word1.equalsIgnoreCase(word2) )
System.out.println ( "The words you entered are the same" ) ;
else
System.out.println ( "The words you entered are NOT the same" ) ;
} // end main
}// end class
// ............................................................................
Explanation / Answer
Solution:
1. java WordCommandLine fred zebra
first argument args[0]=fred
second argument args[1]=zebra
both arguments are not equal so it prints
The words you entered are NOT the same
2. java WordCommandLine Zebra ZebRa
first argument args[0]=Zebra
second argument args[1]=ZebRa
comparing args[0] and args[1] with equalsIgnoreCase so the output is
The words you entered are the same
3. java WordCommandLine Zebra ZebRa fred
first argument args[0]=Zebra
second argument args[1]=ZebRa
second argument args[2]=fred
comparing args[0] and args[1] equalsIgnoreCase so the output is
The words you entered are the same
4. java WordCommandLine Zebra
first argument args[0]=Zebra
there is no second arguement
so exception is arised java.lang.ArrayIndexOutOfBoundsException
5.java WordCommandLine 2 3
first argument args[0]=2
second argument args[1]=3
both arguments are not equal so it prints
The words you entered are NOT the same
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.