Search code examples
javawhile-loopsyntaxcharinfinite-loop

Interactions Pane stops and keeps on displaying a loading icon after inputing something


I have a coding assignment for my class that involves checking for characters for a "Tweet tester". There is something wrong with the following code, and I believe that it's an infinite loop, but I don't know what's causing it. Basically, every time I input a value, the interactions pane stops and just gives me a loading icon.

Scanner scan = new Scanner(System.in);

System.out.println("Tweet something!");

String tweet = scan.nextLine();
int mention1 = 0;

while (mention1 < tweet.length());
        {
          mention = tweet.charAt (mention1);
          if (mention == '@')
          {
            if (tweet.charAt(mention1 + 1) != ' ' || tweet.charAt (mention1 + 1) != '\t')
            {
              mentionCount++;
            }
          }
          mention1++;
  }
System.out.println ("Number of Mentions: " + mentionCount);

Also, the part of the assignment displayed is supposed to tell the user how many mentions there are in the tweet (entered by the user), and is supposed to exclude any @ followed by a space or a tab.


Solution

  • The problem is the semicolon after your while statement. It will indeed create an infinite loop because the subsequent code block will never be executed.

    while (mention1 < tweet.length());
                                     ^
                                   Remove