0
import java.util.Scanner;

public class CompareStrings {
    public static void main(String[] args) {

        // prompted user input
          Scanner input = new Scanner(System.in);
            int firstIndex;
            int secondIndex;

            System.out.print("Enter First String:"); // prompt user
                String stringNumberOne = input.next(); // assigns stringNumberOne to user input
            System.out.print("Enter Second String:"); // prompt
                String stringNumberTwo = input.next(); // assigns stringNumberTwo to user input


            System.out.print("Enter Starting Index for First String:"); // prompt
                 firstIndex = input.nextInt(); // assigns firstIndex to user input
            System.out.print("Enter Starting Index for Second String:"); // prompt
                 secondIndex = input.nextInt(); // assigns secondIndex to user input
            System.out.print("Enter Number of Characters to be Compared:"); // prompt
                int numberCompared = input.nextInt(); // assigns numberCompared to user input

            boolean results = stringNumberOne.regionMatches(firstIndex,
                    stringNumberTwo, secondIndex, numberCompared);   

            if (results)
                System.out.println(true);
            else
                System.out.println(false);
    }
}

This is my code. I am trying to use 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 then should print whether or not the strings are equal (true/false). Ignoring the case of the characters during comparison. I have written the above code and if a single word like "Hello" is entered, I am able to run my program correctly. However, if I write a sentence such as "Hello this is my Java program" I receive an error stating

String:Exeception in thread "main" java.util.InputMismatchException

and then the code will not run. It highlights the portion of my firstIndex = input.nextInt(); code. Any help would be greatly appreciated! Thank you for taking the time to read this post! :)

GBlodgett
  • 12,704
  • 4
  • 31
  • 45
  • Why is your indentation all over the place like that? Looks bad, and is difficult to follow! – Andreas Aug 03 '18 at 18:30
  • 1
    FYI, `if (results) System.out.println(true); else System.out.println(false);` is equivalent to `System.out.println(results);` – shmosel Aug 03 '18 at 18:31
  • 3
    Possible duplicate of [Scanner.nextLine() throws java.util.InputMismatchException](https://stackoverflow.com/questions/51563406/scanner-nextline-throws-java-util-inputmismatchexception) – Ryuzaki L Aug 03 '18 at 18:57

2 Answers2

2

String stringNumberOne = input.next(); From the docs for next():

Finds and returns the next complete token from this scanner.

next() only grabs the next complete token. (In this case only the first word) So currently when you input a whole sentence the two next() calls will be resolved and you will be trying to resolve the third word to an int, which will throw an InputMismatchException exception. Change this line to

String stringNumberOne = input.nextLine();

To grab an entire line.

GBlodgett
  • 12,704
  • 4
  • 31
  • 45
  • I can't believe it was that easy! My code will now add the additional lines, however, the true/false results are no longer correct. :/ – Noblest Argon Aug 03 '18 at 18:32
  • What input are you giving it, and what is it outputting? – GBlodgett Aug 03 '18 at 18:34
  • This was the example I was given, so I am using this to confirm: 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 false – Noblest Argon Aug 03 '18 at 18:36
  • it should be true, but states false – Noblest Argon Aug 03 '18 at 18:37
  • @NoblestArgon `Christmas` doesn't equal `christmas` (Capitalize Christmas in the second sentence) – GBlodgett Aug 03 '18 at 18:41
  • Oh my example must have been incorrect. Thank you for all of your help! – Noblest Argon Aug 03 '18 at 18:43
0

Writing everything in the main method is not good practice in Java. Here's my answer:

class Test {
    
    
    final static Scanner sc = new Scanner(System.in);

    private String str1;
    private String str2;
    private int toffset;
    private int ooffset;
    private int len;
    
    public Test() {

             this("", "");
        
    }
    public Test(String str1, String str2) {
             super();
             this.str1 = str1;
             this.str2 = str2;
    }

    @Override
    public String toString() {
             return this.str1+" and "+this.str2;
                
    }
    
    public Test[] accept() {
            
             Test[] arr = new Test[1];
             System.out.print("Enter First String   : ");
             this.str1 = sc.nextLine();
             
             System.out.print("Enter Second String  : ");
             this.str2 = sc.nextLine();
             
             System.out.print("Enter toffset    : "); 
             this.toffset  = sc.nextInt();
             
             System.out.print("Enter ooffset    : "); 
             this.ooffset = sc.nextInt(); 
            
             System.out.print("Enter Length     : "); 
             this.len = sc.nextInt(); 
             
             arr[0] = new Test(str1, str2);
             return arr;
            
    }
    
    public void print(Test[] arr)
    {
             for(Test t : arr)
                 System.out.println("Strings you want to compare are : "+t);
    
    }
    
    public void compareString() {
        
             System.out.println("Answer : "+str1.regionMatches(toffset ,str2, ooffset, len));
    }
    

}


public class Program {
    
    
    public static void main(String args[]){
                   
              Test t = new Test();
              Test[] arr = t.accept();
              t.print(arr);
              t.compareString();
          
    }
}