简体   繁体   中英

Why does my program keep looking for inputs?

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");
    }
}

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) {...}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM