Ask Question
18 March, 09:43

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".

+3
Answers (1)
  1. 18 March, 12:58
    0
    Java code given below

    Explanation:

    Java code:

    import java. util.*;

    import java. io.*;

    import java. math.*;

    public class abc {

    public static void main (String[] args) throws FileNotFoundException{

    File file = new File ("input. txt");

    Scanner sc = new Scanner (file);

    matchIndex (sc);

    }

    public static void matchIndex (Scanner sc) {

    int line=1;

    while (sc. hasNextLine ()) {

    System. out. print ("lines "+Integer. toString (line) + " and "+Integer. toString (line+1) + ": ");

    String s=sc. nextLine ();

    String t=sc. nextLine ();

    s=s. trim ();

    t=t. trim ();

    int l=Math. min (s. length (), t. length ());

    Boolean match=false;

    for (int i=0; i
    if (s. charAt (i) = =t. charAt (i)) {

    System. out. print (Integer. toString (i) + " ");

    match=true;

    }

    }

    if (! match) {

    System. out. println ("none ");

    }

    else{

    System. out. println ();

    }

    line=line+2;

    }

    }

    }
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “Write a method named matchIndex that accepts as its parameter a Scanner for an input file. Your method should compare each neighboring pair ...” in 📘 Social Studies if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers