| Author |
Loop Problem
|
Iain Palmer
Ranch Hand
Joined: Jul 28, 2004
Posts: 56
|
|
I am writng a program that a user enters the amount of sentences he would like to enter ,then the computer ask for a sentence but i can't seem to get it to loop rString numberSentences = JOptionPane.showInputDialog("How Many Senteces To Be Entered"); int number = Integer.parseInt(numberSentences); System.out.println("Number of sentences to be entered :"+number); do { aSentence = JOptionPane.showInputDialog("Please Enter Sentence Number"+number); System.out.println("Sentence Number :"+sentences); // Set Up A Loop To Count the Number Charcters In The Sentences for(int charNo = 0;charNo < aSentence.length(); charNo++) switch(aSentence.charAt(charNo)) { case 'a': case 'A': case 'e': case 'E': case 'i': case 'I': case 'o': case 'O': case 'u': case 'U': vowels++; break; case ' ': words++; break; default: consonents++; break; } sentences++; } while (sentences < number); System.out.println("You Said you were going to enter :"+numberSentences+" Sentences"); System.out.println("You entered :"+sentences+" which were"); System.out.println(aSentence); System.out.println(words+" Words, "); System.out.println( consonents+" Consonents & "+vowels+" vowels"); System.exit(0); }ound. It's currentl in a do while loop
|
 |
Horatio Westock
Ranch Hand
Joined: Feb 23, 2005
Posts: 221
|
|
What you have posted won't compile as it is. You need post all of your code - for example you don't include where you declare the variables vowels, words etc. Also, please use the 'CODE' button at the bottom, and past your code in between the two tags - this means it will keep it's formatting and is much easier for us to read. It certainly looks as though you are on the right track, so hopefully we can help you spot the bugs when you post the rest. [ March 11, 2005: Message edited by: Horatio Westock ]
|
 |
fred rosenberger
lowercase baba
Bartender
Joined: Oct 02, 2003
Posts: 9950
|
|
well, part of your problem is the semicolon defines the end of your while loop. This code as written would go into an infinite loop if sentences is less than number. if it's not, you'll blow through this loop, and go straight to your print statements, and then end. you need to take out the semicolon, and probably put in braces around what SHOULD be in the body of your loop.
|
Never ascribe to malice that which can be adequately explained by stupidity.
|
 |
Iain Palmer
Ranch Hand
Joined: Jul 28, 2004
Posts: 56
|
|
Thanks for the help I had a play with the loop and got it to work here is the program import javax.swing.JOptionPane; public class ArrayDemo13 { // Program To Let A User Decide How Many Sentences He Wants To Enter // Output the Sentences, The Number Of Sentences He Choose // The Amount Of Words, consonents And Vowels public static void main(String args []) { //Intialise Variables to be used int vowels = 0; int words = 1; int consonents = 0; int sentences = 1; String aSentence; // Create JOPtionPanes for the number Sentences To Be Entered // And For The Sentences String numberSentences = JOptionPane.showInputDialog("How Many Senteces To Be Entered"); int number = Integer.parseInt(numberSentences); System.out.println("Number of sentences to be entered :"+number); if (number <0 | number >10) { System.exit(0); } else do { aSentence = JOptionPane.showInputDialog("Please Enter Sentence Number"+number); // Set Up A Loop To Count the Number Charcters In The Sentences for(int charNo = 0;charNo < aSentence.length(); charNo++) switch(aSentence.charAt(charNo)) { case 'a': case 'A': case 'e': case 'E': case 'i': case 'I': case 'o': case 'O': case 'u': case 'U': vowels++; break; case ' ': words++; break; default: consonents++; break; } System.out.println("Sentence number :"+ sentences); System.out.println(aSentence); System.out.println(words+" Words, "); System.out.println( consonents+" Consonents & "+vowels+" vowels"); sentences++; vowels = 0; words = 1; consonents = 0; } while (sentences <= number); System.exit(0); } } by the way is this easier to read now?
|
 |
Horatio Westock
Ranch Hand
Joined: Feb 23, 2005
Posts: 221
|
|
Originally posted by fred rosenberger: well, part of your problem is the semicolon defines the end of your while loop. This code as written would go into an infinite loop if sentences is less than number. if it's not, you'll blow through this loop, and go straight to your print statements, and then end. you need to take out the semicolon, and probably put in braces around what SHOULD be in the body of your loop.
This is all true unless you are using a "do { ... } while (condition);" loop
|
 |
fred rosenberger
lowercase baba
Bartender
Joined: Oct 02, 2003
Posts: 9950
|
|
ah crap!!! see, that's why those code tags help... cause then you can TELL what's going on much easier. i have GOT to stop trying to help people so early in the morning....
|
 |
Horatio Westock
Ranch Hand
Joined: Feb 23, 2005
Posts: 221
|
|
Iain, I have a suggestion. Why not think about how you could use another loop to keep asking the user for a number of sentences until they enter a valid number, rather than exiting? It would a good addition to your programme. A couple of other things: What happens when you just press 'OK' instead of entering a number of sentences? What happens when you just press 'OK' instead of entering a sentence? What happens when you enter the digits 0-9. How does it affect your totals for vowels, consonants etc? [ March 11, 2005: Message edited by: Horatio Westock ] [ March 11, 2005: Message edited by: Horatio Westock ]
|
 |
Iain Palmer
Ranch Hand
Joined: Jul 28, 2004
Posts: 56
|
|
|
I've manged to sort out the problem with the numbers but .How do you get a loop to go back above itself?
|
 |
fred rosenberger
lowercase baba
Bartender
Joined: Oct 02, 2003
Posts: 9950
|
|
you can't jump to a spot in your code before the start of the loop. but you can have a loop inside a loop.
|
 |
Iain Palmer
Ranch Hand
Joined: Jul 28, 2004
Posts: 56
|
|
|
Do you mean out the entire program in a loop? of just the question asking for how many sentences?
|
 |
Iain Palmer
Ranch Hand
Joined: Jul 28, 2004
Posts: 56
|
|
|
Sorry that shoulg have read do you mean put the entire program in a loop
|
 |
Iain Palmer
Ranch Hand
Joined: Jul 28, 2004
Posts: 56
|
|
|
Ok Thanks for the Help I've got it to loop round now
|
 |
 |
|
|
subject: Loop Problem
|
|
|