Write a static method named matchIndex that accepts as its parameter a Scanner f
ID: 3643872 • Letter: W
Question
Write a static 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.) Also all cap locks are bolds btw.
0123456789012345678901234567890123456789
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.
import java.util.*;
import java.io.*;
public class MatchIndex {
public static void main(String[] args) throws FileNotFoundException {
Scanner fileIn = new Scanner(new File("test.txt"));
matchIndex(fileIn);
}
// *** Your method code goes here ***
} // End of MatchIndex class
Explanation / Answer
Read two lines at a time. Iterate till the length of the smaller length string, and compare letter wise. Report the matching ones, and keep a count to output "none" if required. Should be real easy!
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.