Problem
Write a method:
/** * Returns true if every string in strings has the same length and false * otherwise. * * @param strings The array of Strings to check. * @return True if every String in strings has the same length and false otherwise. */ public static boolean hasEqualLengthStrings(String[] strings) { //... }
Examples
Consider the following examples in a main method:
class Main { public static void main(String[] args) { String[] arr = { "Moe", "Larry", "Curly" }; boolean result = hasEqualLengthStrings(arr); // should be false. String arr2 = { "ten", "ace" }; result = hasEqualLengthStrings(arr2); // should be true. } }
Hints
This code will print the all the String
s in the argument (note though that the final version of the method should not print anything):
public static boolean hasEqualLengthStrings(String[] strings) { for (int i = 0; i < strings.length; i++) System.out.println(strings[i]); return false; //just to make it compile. }
Submission
Upload the .java file containing your hasEqualLengthStrings()
method. The file may also have a main()
method that tests this method, but the main()
method will not be graded.
This material was created by Raffi Khatchadourian and published freely under a Creative Commons Attribution 4.0 license.