Question: I have a code but there's only one problem which is that I couldn't figure out how to put the output in the same line,

 I have a code but there's only one problem which isthat I couldn't figure out how to put the output in the

I have a code but there's only one problem which is that I couldn't figure out how to put the output in the same line, instead of another line so if you could help i would appreciate it!

CODE:

import java.util.Scanner;

public class StringCompare {

public static void main(String[] args) {

Scanner sc = new Scanner(System. in );

System.out.println("Enter-first-string:");

String first = sc.nextLine();

System.out.println("Enter-second-string:");

String second = sc.nextLine();

System.out.println("Enter-starting-index-for-first:");

int firstind = sc.nextInt();

System.out.println("Enter-starting-index-for-second:");

int secondind = sc.nextInt();

System.out.println("Enter-number-of-charecters-to-be-compared:");

int numChars = sc.nextInt();

boolean isqual = isStringMatches(first, second, firstind, secondind, numChars);

System.out.println("is both string are equal :" + isqual);

}

private static boolean isStringMatches(String first, String second,

int firstind, int secondind, int numChars) {

int firstcomparelen = firstind + numChars;

int secondcomparelen = secondind + numChars;

if (firstind >= first.length() || secondind >= second.length()) {

return false;

} else if (numChars > first.length() || numChars > second.length()) {

return false;

}

else if (firstcomparelen > first.length() || secondcomparelen > second.length()) {

return false;

}

else {

String firstsubstring = first.substring(firstind, first.length());

String secondsubstring = second.substring(secondind, second.length());

int equal = 0;

for (int i = 0; i

if (firstsubstring.charAt(i) != secondsubstring.charAt(i)) {

equal++;

}

}

if (equal > 0) {

return false;

} else {

return true;

}

}

}

}

Write a program that uses String method regionMatches to compare two strings input by the user. The program should prompt the user to enter two strings, the starting index in the first string, the starting index in the second string, and the number of characters to be compared. The program should print whether or not the strings are equal. (Ignore the case of the characters during comparison.) SAMPLE RUN #1: java String Compare Interactive Session Hide Invisibles Highlight: None Show Highlighted Only Enter.first.string: Have yourself.a-merry.little Christmas. Enter.secondstring:It's beginning to look-a-lot-like christmas.- Enter.starting.index.for first.string: 29.- Enter.starting.index.for second string:34. Enter number of characters to be compared: 92 true Problems Detected: The contents of your standard output is incorrect. Given the following was entered from the keyboard: Deck-the-halls-with-boughs-of-holly! Tis-the-season-to-be-jolly!3 31.d 22-d 52 you displayed: Enter-first-string: Enter-second-string: Enter-starting-index-for-first: Enter-starting-index-for-second: Enter-number-of-charecters-to-be-compared:2 is-both-string-are-equal :trued instead of: Enter-first-string:Enter-second-string:Enter-starting-index-for-first-string:Enter-starting-index-for-second

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!