2

It's supposed to count the number of matching pairs in the strings.

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

        System.out.print("Please enter a word: ");
        Scanner inpFirst = new Scanner(System.in);
        String inputF = inpFirst.nextLine();

        System.out.print("Please enter another word: ");
        Scanner inpSecond = new Scanner(System.in);
        String inputS = inpSecond.nextLine();

        int lenghtF = inputF.length() - 1;
        int lengthS = inputS.length() - 1;
        int f = 0;
        int s = 0;
        int matchingPairs = 0;

        while ((f < lenghtF) & (s < lengthS)) {
            char testF = inputF.charAt(f);
            char testS = inputS.charAt(s);
            if (testF == testS) {
                char testTwoF = inputF.charAt(f+1);
                char testTwoS = inputS.charAt(f+1);
                if (testTwoF == testTwoS)
                    matchingPairs = matchingPairs++;
            }
            System.out.println("jrfjtf");
            f = f++; 
            s = s++;
        }
        System.out.println("The number of matching pairs is: " + matchingPairs);


    }
}
upog
  • 4,965
  • 8
  • 42
  • 81
Count Zander
  • 21
  • 2
  • 3
  • 1
    Since the loop exit condition depends on `f` and `s`, it would be reasonable to try debugging this by printing the values `f` and `s` on each iteration, rather than `"jrfjtf"`, which will only show that you're in an infinite loop. (Maybe you did try this, but you didn't mention it in the question. If you did try it, then you should have mentioned that the problem was that the values aren't being incremented. This would have helped you find the duplicate that @RohitJain linked to.) – Joshua Taylor Oct 22 '13 at 04:30

3 Answers3

4

Change the last two lines of the loops to f++ and s++.

Basically, setting

f = f++

doesn't increment the value, it sets f=f, you want just f++ instead.

As Masud has mentioned, change your operator from && and &. Most of the time (especially with if statements), you should be using the && operator.

Dhaivat Pandya
  • 6,499
  • 4
  • 29
  • 43
1

You used & that is bitwise operator. Use conditional and (&&) operator instead of & in while loop.

Masudul
  • 21,823
  • 5
  • 43
  • 58
  • In Java, the single ampersand is still a boolean check, but it's not short-circuiting. `(f < lenghtF) & (s < lengthS)` is logically correct; it just does a little bit more work than it needs to. See, for instance [Why do we usually use `||` not `|`, what is the difference?](http://stackoverflow.com/q/7101992/1281433) – Joshua Taylor Oct 22 '13 at 04:32
  • I don't think that would actually make a difference. `(f < lenghtF)` and `(s < lengthS)` both evaluate to booleans, and the bitwise operator acts as non-short-circuiting logical operators when both operators are booleans. See [this question](http://stackoverflow.com/questions/1724205/effect-of-a-bitwise-operator-on-a-boolean-in-java) for more info. – MrAzzaman Oct 22 '13 at 04:34
0

Seems like you have an incremention problem. 1. Using the bitwise and operator - '&' seems to be ok. It just computes both sides of the condition and going the long way instead of making shortcuts like in "if(foo != null && foo.doOpperation) if foo is null , the right side won't be checked , that way you can avoid 'null refferance error'. 2. You are incrimenting in a wrong way, " f=f++" will keep f as it is .

Suggetions: Use && in the conditioning and "f++;" as incremention operator. Another suggestion in this case is using a for loop, your condition is simple and the opperation on f and s is only inceementation.

Itzik Gili
  • 324
  • 3
  • 16