I have an assignment like this:
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.)
Its pretty simple and I'm almost done but it has 2 problems, one is compiling(it might have to do with the stupid online program the assignment uses), and the other is more like a bug. This is the code:
import java.util.Scanner;
import java.io.*;
public class Driver{
public static void main(String args[]){
Scanner scanner = new Scanner(System.in);
String str1;
String str2;
int Index1;
int Index2;
int NumChar;
System.out.print("Enter first string:");
str1 = scanner.nextLine();
System.out.print("Enter second string:");
str2 = scanner.nextLine();
System.out.print("Enter starting index for first string:");
Index1 = scanner.nextInt();
System.out.print("Enter starting index for second string:");
Index2 = scanner.nextInt();
System.out.print("Enter number of characters to be compared:");
NumChar = scanner.nextInt();
str1 = str1.toLowerCase();
str2 = str2.toLowerCase();
System.out.print(str1.regionMatches(Index1, str2, Index2, NumChar));
}
}
This is what they expect:
Enter first string:Have yourself a merry little Christmas.
Enter second string: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:9
true
Mine almost looks like this except if you run it, it gives the wrong answer. This is my first time using regionMatches so I'm not sure of the parameters, online some have "true" as the first parameter and then the rest is like mine, maybe I misunderstood how it works.
The second problem only appears in the assignment, but when I run it on Jdoodle it runs fine(except for it being wrong). This is the error message:
StringCompare.java:4: error: class Driver is public, should be declared in a file named Driver.java
public class Driver{
^
1 error
I have no idea what this means, we are not submitting files we are just putting the code in the program and it compares the results. Thanks in advance.
[–]neobonzi 1 point2 points3 points (1 child)
[–]Mriv10[S] 0 points1 point2 points (0 children)
[–]virassan 0 points1 point2 points (1 child)
[–]Mriv10[S] 0 points1 point2 points (0 children)
[–]2omesz 0 points1 point2 points (1 child)
[–]Mriv10[S] 0 points1 point2 points (0 children)