Ask Question
13 April, 18:04

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

+1
Answers (1)
  1. 13 April, 19:59
    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?
Get 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 there is no answer or all answers are wrong, use a search bar and try to find the answer among similar questions.
Search for Other Answers