connor marble
connor marble

Reputation: 9

Infinite buffer before the loop?

Simple code that repeats an integer just reversing it. have to break the loop by pressing enter, however when I try to run this code it just buffers infinitely, and doesn't show anything?

import java.util.*;
public class ReverseDisplay
{
    public static void main(String Args[])
    {
        Scanner kb = new Scanner(System.in);
        boolean repeat = true;
        while (repeat == true);
        {
            System.out.print("Enter an integer to be reversed or hit enter to end program:");
            int line = kb.nextInt();
            int length = (int)(Math.log10(line) + 1);
            if (length == 1)
            {
                repeat = false; 
            }
            else
            {
                System.out.print("The reverse of " + line + " is ");
                reverseDisplay(line);
            }
        }

    }
    public static void reverseDisplay(int value)
    {   
        if (value < 10)
        {
            System.out.println(value);
            return;
        }
        else
        {
            System.out.print(value % 10);
            reverseDisplay(value / 10);
        }
    }   
}

EDIT : Removing the semicolon caused it to run properly, Thanks for the quick response.

Upvotes: 0

Views: 54

Answers (1)

Kamil Jarosz
Kamil Jarosz

Reputation: 361

There should be no ";" after "while". Your code is effectively running in an infinite loop doing absolutely nothing.

Upvotes: 2

Related Questions