while loop with a decrementing counter

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Nepomuk
    Recognized Expert Specialist
    • Aug 2007
    • 3111

    #16
    Originally posted by tolkienarda
    hmm i am still having a problem
    i still cannot input my string that is to be reversed.
    and my loop appears to be infinite, i am not sure if that semi colon behind my while statement is correct but i got build errors without it
    here is my entire main() function

    Code:
        public static void main(String[] args) {
        	
        	String line;
        	Boolean cont = false;
        	Boolean rev;
        	int length;
        	int i;
        	do
        	{
    	    	System.out.println("Enter a line of text");
    	    	line = console.nextLine();
    	    	length = line.length();
    	    	i=length - 1;
    	    	rev = recRev(line, i);
    	    	System.out.println("\n would you like to go again(y/n)");
    	    	if(console.next().equals("y"))
    	    		cont = true;
        	} while(cont);
        }
    Output
    Code:
    Enter a line of text
    asdf
    fdsa
     would you like to go again(y/n)
    y
    Enter a line of text
    
     would you like to go again(y/n)
    n
    Enter a line of text
    
     would you like to go again(y/n)
    and there is no pause for me to input text after the "Enter a line of text" line it just prints the " would you like to go again(y/n)"

    eric
    Now, that will go infinitely. Do this:
    [CODE=java]
    do
    {
    System.out.prin tln("Enter a line of text");
    line = console.nextLin e();
    length = line.length();
    i=length - 1;
    rev = recRev(line, i);
    System.out.prin tln("\n would you like to go again(y/n)");
    cont = false; // You need this!!!
    if(console.next ().equals("y"))
    cont = true;
    } while(cont);
    [/CODE]otherwise, it will be set to true once and forever.

    You said something about not being able to reverse your String? It seems to do that fine in your output, doesn't it?

    Greetings,
    Nepomuk

    Comment

    Working...