Results 1 to 2 of 2
  1. #1
    loftus727 is offline Member
    Join Date
    Feb 2013
    Rep Power

    Default problems with program

    I can not figure out what the problem is but after it runs through once the output comes out wrong.

    here are my instructions:
    The file contains the skeleton of a program to read in a string (a sentence or phrase) and count the number of blank spaces in the string. The program currently has the declarations and initializations and prints the results. All it needs is a loop to go through the string character by character and count (update the countBlank variable) the characters that are the blank space. Since we know how many characters there are (the length of the string) we use a count controlled loop—for loops are especially well-suited for this.

    1. Add the for loop to the program. Inside the for loop you need to access each individual character—the charAt method of the String class lets you do that. The assignment statement

    ch = phrase.charAt(i);

    assigns the variable ch (type char) the character that is in index i of the String phrase. In your for loop you can use an assignment similar to this (replace i with your loop control variable if you use something other than i). NOTE: You could also directly use phrase.charAt(i) in your if (without assigning it to a variable).

    2. Test your program on several phrases to make sure it is correct.

    3. Now modify the program so that it will count several different characters, not just blank spaces. To keep things relatively simple we'll count the a's, e's, s's, and t's (both upper and lower case) in the string. You need to declare and initialize four additional counting variables (e.g. countA and so on). Your current if could be modified to cascade but another solution is to use a switch statement. Replace the current if with a switch that accounts for the 9 cases we want to count (upper and lower case a, e, s, t, and blank spaces). The cases will be based on the value of the ch variable. The switch starts as follows—complete it.

    switch (ch)
    case 'a':
    case 'A': countA++;

    case ....


    Note that this switch uses the "fall through" feature of switch statements. If ch is an 'a' the first case matches and the switch continues execution until it encounters the break hence the countA variable would be incremented.

    4. Add statements to print out all of the counts.

    5. It would be nice to have the program let the user keep entering phrases rather than having to restart it every time. To do this we need another loop surrounding the current code. That is, the current loop will be nested inside the new loop. Add an outer while loop that will continue to execute as long as the user does NOT enter the phrase quit. Modify the prompt to tell the user to enter a phrase or quit to quit. Note that all of the initializations for the counts should be inside the while loop (that is we want the counts to start over for each new phrase entered by the user). All you need to do is add the while statement (and think about placement of your reads so the loop works correctly). Be sure to go through the program and properly indent after adding code—with nested loops the inner loop should be indented.

    here is my code
    Java Code:
    import java.util.Scanner;
    public class Countnew1
      public static void main (String[] args)
          String phrase;    // a string of characters
          int countBlank;   // the number of blanks (spaces) in the phrase
          int length;       // the length of the phrase
          char ch;          // an individual character in the string
          int i;
    		int countA,countE, countS, countT;
        Scanner scan = new Scanner(;
          // Print a program header
          System.out.println ();
          System.out.println ("Character Counter");
          System.out.println ();
          // Read in a string and find its length
          System.out.print ("Enter a sentence or phrase or quit to stop: ");
          phrase = scan.nextLine();
          length = phrase.length();
          // Initialize counts
          // a for loop to go through the string character by character
          // and count the blank spaces
    		countBlank = 0;		
    		countA = 0;
    		countE = 0; 
    		countS = 0; 
    		countT = 0;
    		if (phrase.equalsIgnoreCase("quit"))
            				for (i = 0; i < length ; i++ )
           					 ch = phrase.charAt(i);
    		  						switch (ch)
    		 							case ' ': countBlank++;
    		 							case 'a':
    		  							case 'A': countA++;
    									case 'e':
    		 							case 'E': countE++;
    		 	   					case 's':
    		 							case 'S': countS++;
    		 							case 't':
    		 							case 'T': countT++;
         			   // Print the results
          			System.out.println ();
          			System.out.println ("Number of blank spaces: " + countBlank);
          			System.out.println ("Number of A's: " + countA);
    					System.out.println ("Number of E's: " + countE);
    					System.out.println ("Number of S's: " + countS);
    					System.out.println ("Number of T's: " + countT);
    					System.out.println ();
    					System.out.println ("Enter another phrase or quit to stop");
    					phrase = scan.nextLine();
    any help would be greatly appreciated

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Voorschoten, the Netherlands
    Blog Entries
    Rep Power

    Default Re: problems with program

    You forgot to (re)initialize your variable 'length'.

    kind regards,

    Build a wall around Donald Trump; I'll pay for it.

Similar Threads

  1. problems with the program
    By stefandanielsen in forum AWT / Swing
    Replies: 5
    Last Post: 03-03-2011, 08:04 AM
  2. problems when i run the program
    By stefandanielsen in forum New To Java
    Replies: 3
    Last Post: 03-03-2011, 08:02 AM
  3. problems with the program
    By stefandanielsen in forum New To Java
    Replies: 1
    Last Post: 03-01-2011, 10:08 AM
  4. Problems with program..
    By spatel14 in forum New To Java
    Replies: 8
    Last Post: 07-01-2010, 02:51 PM
  5. Problems with this Java Program...
    By Bangtajra123 in forum Java Applets
    Replies: 6
    Last Post: 09-30-2008, 10:04 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts