Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Need help with While and For Statements

  1. #1
    Junior Member
    Join Date
    Sep 2009
    Posts
    5
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Need help with While and For Statements

    Okay, the code below works fine, it prompts the user to enter a sentence, makes sure it is a proper sentence, then checks how many words there are. What I want it to do is prompt the user over and over again (keeping track of the sentences entered) until one of the sentences contains a 'q' or a 'Q'. I've been trying to put another while around the whole thing but that doesn't work. I really am stuck.

    import javax.swing.*;
     
    public class ChurchAustinA2Q2 {
     
      public static void main (String [] args) {
     
     
        String prompt = "Enter one sentence. " ;
        String input ;
     
        boolean inputOk = false ;
     
        int loc ;
        int loc2 ;
        int letterCount = 0 ;
        int wordCount = 0 ;
        int charCount = 0 ;
        int sentenceCount = 0 ;
     
        char ch = 'x' ;
     
     
        input = JOptionPane.showInputDialog(null, prompt) ;
        input = input.trim() ;
        loc = input.length() ;
     
        while ( ! inputOk ){
    		if ( input.charAt(loc-1) == ( '!' ) ){
    			inputOk = true ;
    		}
    		else if ( input.charAt(loc-1) == ( '.' ) ){
    			inputOk = true ;
    		}
    		else if ( input.charAt(loc-1) == ( '?' ) ){
    			inputOk = true ;
    		}
    		else{
    			inputOk = false ;
    			input = JOptionPane.showInputDialog(null, "That is not a proper sentence. " + prompt) ;
    			input = input.trim() ;
        		loc = input.length() ;
    		}
    	}
     
     
    	for ( loc2 = 0 ; loc2 < input.length() ; loc2++ ){
    		ch = input.charAt(loc2);
    		charCount++ ;
     
    		if (( 'a' <= ch && ch < 'z' ) || ( 'A' <= ch && ch <= 'Z' ) || ( ch == 39 ) ) {
    			letterCount++ ;
    		}
    		else{
     
    			if ( letterCount > 0 ){
    				wordCount++ ;
    				letterCount = 0 ;
    			}
    		}
    	}
     
     
     
    	System.out.println("\n") ;
    	System.out.println("Sentence Statistics ") ;
    	System.out.println("=================== ") ;
    	System.out.println("Sentences: " + sentenceCount) ;
    	System.out.println("Words: " + wordCount) ;
    	System.out.println("Word Characters: " + (charCount - wordCount) ) ;
    	System.out.println("\n") ;
    	System.out.println("A / a = ") ;
    	System.out.println("E / e = ") ;
    	System.out.println("I / i = ") ;
    	System.out.println("O / o = ") ;
    	System.out.println("U / u = ") ;
    	System.out.println("\n") ;
    	System.out.println("Average Word Length: ") ;
    	System.out.println("Average Vowels Per Word: ") ;
     
      }
    }


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Need help with While and For Statements

    Contains a 'q' or 'Q', or is a 'q' or 'Q'? These are two very different situations.

    Wrap your code inside a while statement, and make it's condition true (in other-words, an infinite loop). Then after the user has inputted a sentence, use the contains() method to see if the sentence contains any q's or Q's. Or, if you meant it has only one q or Q, then use the equals() method

    while(true)
    {
        // code before
        // get sentence code
        if (sentence.toLowerCase().contains("q"))
         {
              break;
         }
         // rest of code
    }

  3. The Following User Says Thank You to helloworld922 For This Useful Post:

    duckman (October 20th, 2009)

  4. #3
    Junior Member
    Join Date
    Sep 2009
    Posts
    5
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Need help with While and For Statements

    Yeah, that's what I was doing before but it was not working properly for some reason. I figured out what I was doing wrong now though. Thanks, anyways.

Similar Threads

  1. [SOLVED] Fixing of bug in number guessing game
    By big_c in forum Loops & Control Statements
    Replies: 6
    Last Post: April 16th, 2009, 03:15 AM