0

This code is in Java. It allows me to enter the first input fine, but after the second is inputted it keeps looking for more strings, none of the rest of my code follows. Idealy the code will find if 2 strings are anagrams, but I have not been able to test this due to this annoying problem.

import java.util.*;
public class Anagram
{
    public static void main(String[] args)
    {
        Scanner scan  = new Scanner(System.in);
        System.out.println("Please enter a word");
        String first = scan.nextLine();
        System.out.println("Please enter a second word");
        String second = scan.next();
        first = first.toLowerCase();
        second = second.toLowerCase();
        int lengthF = first.length();
        int lengthS = first.length();
        int x = 0;
        int y = 0;
        int placeF=0;
        int placeS=0;
        char g = 97;
        int count =0;
        if(lengthF != lengthS)
        {
            System.out.println("The words are not anagrams");
            x=1;
        }
        while(x == y||g<123)
        {
            x=0;
            y=0;
            for(int i = 0;i<lengthF;i++)
            {
                if(first.charAt(i)==g)
                {
                    x++;
                }
            }
            for(int i = 0;i<lengthS;i++)
            {
                if(second.charAt(i)==g)
                {
                    y++;
                }
            }
            count++;
            g++;
        }
        if(count==23)
        System.out.println("Anagram");
        else
        System.out.println("Not Anagram");
    }
}
  • 1
    I don't think that your program expects more input, it's more like `while(x == y||g<123)` is an infinite loop. You just need to find out why. – Tom Nov 07 '15 at 18:54
  • 2
    Avoid using variable names such as x, y, g please, give them more meaningful names. – Felipe Alarcon Nov 07 '15 at 18:57
  • but even if their lengths are different if(lengthF != lengthS) { System.out.println("The words are not anagrams"); x=1; } still never happens – matthewjon Nov 07 '15 at 18:57
  • If I'm reading it right, it should be `while(x == y && g<123)` – Cinnam Nov 07 '15 at 19:02
  • 1
    @matthewjon Maybe you should take a look at the "way" to get the second length? Hint: `int lengthS = first.length();` is not correct. Btw I've tested your code `while(x == y||g<123)` is an "infinite" loop. `x` and `y` might not be equal on each iteration, but since you said "***or*** `g < 123` they become equal again. But anyway the way how you check for anagrams is flawed and won't work, even after fixing the length and what Cinnam said. You find better algorithms here: [How to check if two words are anagrams](http://stackoverflow.com/q/15045640) – Tom Nov 07 '15 at 19:02
  • @Tom thank you very much! – matthewjon Nov 07 '15 at 19:04
  • @matthewjon: you assign define lengthS = first.length(); thus lengthS == lengthF is always true – wastl Nov 07 '15 at 19:05
  • I guess you're new to Java, so no worries. Do you already know what a Debugger is and how they work? If not, try to read some stuff about them, because they help you to step through your program to see what really happens. An important tool for each developer. – Tom Nov 07 '15 at 19:06
  • @Tom I'm not quite sure what a Debugger is, but I will definitely check it out, thanks. – matthewjon Nov 07 '15 at 19:15
  • not `debugger` but specifically `step debugger` every IDE has one –  Nov 07 '15 at 20:09
  • Double check your loop; it seems to be infinite. Especially considering that `x` will *always* equal `y` at the beginning. – Makoto Nov 07 '15 at 20:14

1 Answers1

1

I've spotted three bugs in your code, which you can easily find by yourself if you always follow these general rules for programming:

  1. Never use numbers obscurely. Write them in a way that explains where that value comes from: Instead of:

char g=97;

... let it be:

char g='a';

And the same goes for every single "special" number in your program: 97, 123 and 23 (In this way you'll see 23 is wrong).

  1. Indexed loops should be always for, with its initial value, its ongoing condition and its increment operation. Ongoing condition must be the index interval condition, plus optional secondary conditions combined by AND operators.

And the third bug... Well, right now I cannot think of any general rule to avoid it. It must have been a copy-and-paste issue: The variable lengthS is wrongly initialized.

Also, I recommend you not to code the same algorithm more than once: Take it out to an individual function and reuse it. So you can do with the loop that counts the number of occurrences of a certain char within a certain string. You could define it like this:

private static int countOccurrencesOfChar(String s, char c) {...}
Little Santi
  • 8,563
  • 2
  • 18
  • 46
  • 1
    I'd add: 4) learn to use a debugger, think about what values you would expect in each step and what you actually get - test especially edge cases. 5) don't give up at the first problem - if thinking about a problem feels like hitting a wall, take a short walk on fresh air. – Thomas S. Nov 07 '15 at 20:19