trouble with looping

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Feb 2009
Posts: 7
Reputation: regina99 is an unknown quantity at this point 
Solved Threads: 0
regina99 regina99 is offline Offline
Newbie Poster

trouble with looping

 
0
  #1
Feb 14th, 2009
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.
  1. public class backwardstring
  2. {
  3. public static void main (String[]args)
  4. {
  5. int choice1 = 1;
  6. String string;
  7. final int arraylimit = 30;
  8. Scanner keyboard = new Scanner (System.in);
  9. char[] character = new char[arraylimit];
  10. int i = 0;
  11.  
  12.  
  13. do
  14. {
  15. System.out.println("Enter a word or sentence:");
  16. string = keyboard.nextLine();
  17. character = string.toCharArray();
  18.  
  19.  
  20.  
  21. for (i = character.length - 1; i >= 0; i--)
  22. {
  23. System.out.print(character[i]);
  24.  
  25.  
  26. }
  27.  
  28. System.out.println("");
  29. System.out.println("Would you like to enter another word or sentence?(Y(0)/N(1)");
  30. choice1 = keyboard.nextInt();
  31. i = 0;
  32.  
  33. }
  34. while(choice1 == 0);
  35.  
  36.  
  37.  
  38. }
  39. }

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


help, anyone?
pleaseee?
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 69
Reputation: h3xc0de is an unknown quantity at this point 
Solved Threads: 12
h3xc0de h3xc0de is offline Offline
Junior Poster in Training

Re: trouble with looping

 
0
  #2
Feb 14th, 2009
move Scanner keyboard = new Scanner (System.in); inside the loop
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 7
Reputation: regina99 is an unknown quantity at this point 
Solved Threads: 0
regina99 regina99 is offline Offline
Newbie Poster

Re: trouble with looping

 
0
  #3
Feb 14th, 2009
oh.
that was simple.
i feel dumb now.
lolll.
thanks!
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 27
Reputation: rudasi is an unknown quantity at this point 
Solved Threads: 2
rudasi's Avatar
rudasi rudasi is offline Offline
Light Poster

Re: trouble with looping

 
0
  #4
Feb 14th, 2009
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');
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 7
Reputation: regina99 is an unknown quantity at this point 
Solved Threads: 0
regina99 regina99 is offline Offline
Newbie Poster

Re: trouble with looping

 
0
  #5
Feb 14th, 2009
oh yeah, that's true.
lol plus my teacher is all about making it user friendly all the time.

thanks!

Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 298 | Replies: 4
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC