logo Practice-It logo


Language/Type: Java file processing Scanner
Author: Marty Stepp (on 2012/08/16)

Write a method named matchIndex that accepts as its parameter a Scanner for an input file. Your method should compare each neighboring pair of lines (the first and second lines, then the third and fourth lines, and so on) looking for places where the character at a given 0-based index from the two lines is the same. For example, in the strings "hello" and "belt", the characters at indexes 1 ('e') and 2 ('l') match. Your code should be case-sensitive; for example, "J" does not match "j".

For each pair of lines, your method should print output showing the character indexes that match, separated by spaces in the format shown below. If no characters match, print "none" instead as shown below.

For example, suppose the input file contains the following text. (Line numbers and character indexes are shown around the input and matching characters are shown in bold, but these markings do not appear in the actual file.)

 1    The quick brown fox
 2    Those achy down socks
 3    Wheels on the school bus go round
 4    The wipers go swish swish swish
 5    His name is Robert Paulson
 6    So long 'n thanks for all the fish
 7    Humpty Dumpty sat on a wall
 8    And then he also had a great fall
 9    booyakasha
10    Bruno Ali G Borat

When passed the above file, your method would produce the following output:

lines 1 and 2: 0 1 7 12 13 14 15 17
lines 3 and 4: 1 2 13 14 23
lines 5 and 6: none
lines 7 and 8: 4 14 20 21 22
lines 9 and 10: none

Notice that lines are not generally the same length. You may assume that the file contains an even number of lines.

Type your solution here:

This is a method problem. Write a Java method as described. Do not write a complete program or class; just the method(s) above.

You must log in before you can solve this problem.

Log In

If you do not understand how to solve a problem or why your solution doesn't work, please contact your TA or instructor.
If something seems wrong with the site (errors, slow performance, incorrect problems/tests, etc.), please

Is there a problem? Contact a site administrator.