0

I'm currently learning Java in school, and have an assignment to use a while loop to prompt the user for 2 Strings and it will keep prompting until the first and last chars of both Strings are the same. I currently have the piece of code at the bottom. When i do satisfy the conditions, it prints out "Bingo" fine. However when i go into the while loop, no matter what output i enter, it stays in whichever if block i enter, and is an infinite loop. Is there an issue with my while condition or is it the if conditions within the while loop?

     import java.util.*;

     public class StringTest2{
     public static void main(String[]args){
     Scanner sc = new Scanner(System.in);

     System.out.print("Enter word1: ");
     String word1 = sc.nextLine().toUpperCase();

     System.out.print("Enter word2: ");
     String word2 = sc.nextLine().toUpperCase();


     int word1Length = word1.length();
     int word2Length = word2.length();

     char word1First = word1.charAt(0);
     char word1Last = word1.charAt(word1Length - 1);

     char word2First = word2.charAt(0);
     char word2Last = word2.charAt(word2Length - 1);   


     while(word1First != word2First || word1Last != word2Last){

       if(word1First == word2First && word1Last == word2Last){
            System.out.println("Bingo!");
        }

        if(word1First == word2First || word1Last == word2Last){
            System.out.println("Gotcha!");
            System.out.print("Enter word1: ");
            word1 = sc.nextLine().toUpperCase();

            System.out.print("Enter word2: ");
            word2 = sc.nextLine().toUpperCase();;
        }

        if(word1First != word2First && word1Last != word2Last){
            System.out.println("Mismatch!");
            System.out.print("Enter word1: ");
            word1 = sc.nextLine().toUpperCase();

            System.out.print("Enter word2: ");
            word2 = sc.nextLine().toUpperCase();
        }

    }
    System.out.println("Bingo!");
}

}

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Gabberz
  • 13
  • 4
  • You are comparing char with string....why? – Hackerman Oct 03 '16 at 13:47
  • 2
    You never update your `word1First`, `word1Last`, `word2First`, and `word2Last` variables. those need to go into the while() loop as well if you want them to be relevant to the words you just typed in. – Shark Oct 03 '16 at 13:47
  • @Hackerman, i'm looking to compare the first and last letters of the 2 Strings so i plucked them out using the charAt method to compare them. – Gabberz Oct 03 '16 at 13:51
  • The question which this is supposed to be a duplicate of isn't really a duplicate at all. The problem here is that if the conditions are mest on the first iteration, it works fine, but after "fixing" them, it no longer works because the variables aren't updated. IF this is a duplicate, it is **NOT** a duplicate of the marked question. – Shark Oct 03 '16 at 13:52
  • 2
    @Shark oh, i see. so what i essentially was doing was still comparing the two chars plucked out from the 1st 2 Strings i entered. Alright, got it. Thanks! – Gabberz Oct 03 '16 at 13:52
  • 1
    essentially, put these 4 lines at the end of the `while()` loop: ` word1Length = word1.length(); word2Length = word2.length(); word1First = word1.charAt(0); word1Last = word1.charAt(word1Length - 1); word2First = word2.charAt(0); word2Last = word2.charAt(word2Length - 1); ` – Shark Oct 03 '16 at 13:53
  • Ok i got it. Thank you for the super fast response @Shark – Gabberz Oct 03 '16 at 13:55
  • thtanks, no problemo :) – Shark Oct 03 '16 at 13:56

0 Answers0