Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Help with my StringCompare code

  1. #1
    Junior Member
    Join Date
    Jun 2019
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help with my StringCompare code

    I am trying to get this to work on this website called myprogramminglab for college.
    Instructions: 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.)

    What MPL is going to test with and it has to be the exact format which I think I got:
    Enter·first·string:Have·yourself·a·merry·little·Ch ristmas.↵
    Enter·second·string:It's·beginning·to·look·a·lot·l ike·christmas.↵
    Enter·starting·index·for·first·string:29·↵
    Enter·starting·index·for·second·string:34·↵
    Enter·number·of·characters·to·be·compared:9↵
    true↵

    My problem is that there is some errors in the prompts because it is not printing anything. Thanks!

    Errors MPL gave me:
    Failed Test Run #1

    ⇒ The contents of your standard output is incorrect.
    ⇒ There is an error in your prompts.



    import java.util.Scanner;
     
     
    class compare {
    public static void main(String[] args)
    {
    int stind1,stind2,noc;
    String str1, str2, strtemp;
    Scanner scan = new Scanner(System.in);
     
    System.out.print("Enter First String:");
    str1 = scan.nextLine(); 
    System.out.print("Enter Second String:");
    str2 = scan.nextLine();
     
    System.out.print("Enter Starting Index of First string:");
    stind1 = scan.nextInt();
     
    System.out.print("Enter Starting Index of Second string:");
    stind2 = scan.nextInt(); 
     
    System.out.print("Enter number of characters:");
    noc = scan.nextInt(); 
     
    System.out.print("String 1 = " +str1+ "\n"); 
    System.out.print("String 2 = " +str2+ "\n"); 
     
    System.out.print("Starting index of first string " +stind1+ "\n");
    System.out.print("Starting index of second string " +stind2+ "\n");
    System.out.print("Number of characters to compare" +noc+ "\n");
     
    System.out.println(str1.regionMatches(true,stind1,str2,stind2,noc)); 
     
    }
    }
    Last edited by ericeclifford; June 30th, 2019 at 08:04 PM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help with my StringCompare code

    What happens when you execute the program using the java command in a command prompt window?
    Run the program, copy the contents of the window and paste it here if you have any questions about what happened.

    I don't know anything about using the website called myprogramminglab.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 3
    Last Post: April 27th, 2013, 07:19 AM
  2. Replies: 4
    Last Post: January 24th, 2013, 11:20 AM
  3. Replies: 7
    Last Post: January 24th, 2013, 10:41 AM
  4. Replies: 5
    Last Post: November 14th, 2012, 10:47 AM
  5. Replies: 3
    Last Post: September 3rd, 2012, 11:36 AM