hey guys.
i'm having trouble with looping a program.
my assignment is to get the user to input a string and then display it backwards.
i've successfully done this, but it's supposed to ask if you want to run the program again, and that's where my program fails.

public class backwardstring
{
    public static void main (String[]args) 
    {
        int choice1 = 1;
        String string;
        final int arraylimit = 30;
        Scanner keyboard = new Scanner (System.in);
        char[] character = new char[arraylimit];
        int i = 0;
        
        
        do
        {
            System.out.println("Enter a word or sentence:");
            string = keyboard.nextLine();
            character = string.toCharArray();
            
            
             
            for (i = character.length - 1; i >= 0; i--)
            {
                System.out.print(character[i]);
            
                                    
            }
        
            System.out.println("");
            System.out.println("Would you like to enter another word or sentence?(Y(0)/N(1)");
            choice1 = keyboard.nextInt();
            i = 0;
            
        }
        while(choice1 == 0);
        
        
        
    }
}

it completely skips over the for loop after the first time it's run.
:(

help, anyone?
pleaseee?
:'(

move Scanner keyboard = new Scanner (System.in); inside the loop

oh.
that was simple.
i feel dumb now.
lolll.
thanks!

Hi,

I suppose the problem is solved though you can make it a bit more user friendly by using a char insted of an int like,

char choice;
and then in your while condition use
while(choice=='y'||choice=='Y');

oh yeah, that's true.
lol plus my teacher is all about making it user friendly all the time.

thanks!

:D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.