// **********************************************************
// Count.java
//
// This program reads in strings (phrases) and counts the
// number of blank characters and certain other letters
// in the phrase.
// **********************************************************
import java.util.Scanner;
public class Count
{
public static void main (String[] args)
{
String phrase; // a string of characters
int countBlank, countA, countE, countS, countT; // the number of blanks (spaces) in the phrase
int length, value; // the length of the phrase
char ch; // an individual character in the string
Scanner scan = new Scanner(System.in);
// 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 (quit to quit): ");
phrase = scan.nextLine();
length = phrase.length();
// Initialize counts
countBlank = 0;
countA = 0;
countE = 0;
countS = 0;
countT = 0;
// a for loop to go through the string character by character
// and count the blank spaces
while (value != quit)
{
for (int i = 0; i < length; i++)
{
ch = phrase.charAt(i);
switch (ch)
{
case 'a':
case 'A': countA++;
break;
case 'e':
case 'E': countE++;
break;
case 's':
case 'S': countS++;
break;
case 't':
case 'T': countT++;
break;
case ' ': countBlank++;
break;
}
}
length += value;
System.out.print ("Enter a sentence or phrase (quit to quit): " );
value = scan.nextInt();
}
// Print the results
System.out.println ();
System.out.println ("Number of blank spaces: " + countBlank);
System.out.println ("Number of A's in sentence: " + countA);
System.out.println ("Number of E's in sentence: " + countE);
System.out.println ("Number of S's in sentence: " + countS);
System.out.println ("Number of T's in sentence: " + countT);
}
}
While loop outside of a for loophow do you integrate a while loop to make the 'for' continue
Page 1 of 1
1 Replies - 1080 Views - Last Post: 21 March 2010 - 09:59 PM
#1
While loop outside of a for loop
Posted 21 March 2010 - 08:40 PM
Ok so I've figured out the whole coding thing so the program itself works, but what I need to do is put a while loop outside of the 'for' loop to make the program continue after it's been initialized, and for a prompt on the print statement to say 'type quit to quit' for the program to quit when the user types in the word 'quit'. I didn't understand what my teacher was saying, and she said to make the 'quit' a variable? I don't know I'm confused, but all I'm trying to do is make the while loop work so the program will continue.
Replies To: While loop outside of a for loop
#2
Re: While loop outside of a for loop
Posted 21 March 2010 - 09:59 PM
It's pretty simple.

Just change your loop variable to a string:
produces this output:
Just change your loop variable to a string:
package assign2;
import java.util.*;
public class Main {
public static void main(String[] args) {
String phrase; // a string of characters
int countBlank, countA, countE, countS, countT; // the number of blanks (spaces) in the phrase
int length, value = 0; // the length of the phrase
char ch; // an individual character in the string
// initialize for while() loop
String answer = "";
Scanner scan = new Scanner(System.in);
// 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 (quit to quit): ");
phrase = scan.nextLine();
length = phrase.length();
// Initialize counts
countBlank = 0;
countA = 0;
countE = 0;
countS = 0;
countT = 0;
// a for loop to go through the string character by character
// and count the blank spaces
while (!answer.equalsIgnoreCase("quit")) {
for (int i = 0; i < length; i++) {
ch = phrase.charAt(i);
switch (Character.toUpperCase(ch)) {
case 'A':
countA++;
break;
case 'E':
countE++;
break;
case 'S':
countS++;
break;
case 'T':
countT++;
break;
case ' ':
countBlank++;
break;
}
}
length += value;
System.out.print("Enter a sentence or phrase (quit to quit): ");
answer = scan.nextLine();
}
// Print the results
System.out.println();
System.out.println("Number of blank spaces: " + countBlank);
System.out.println("Number of A's in sentence: " + countA);
System.out.println("Number of E's in sentence: " + countE);
System.out.println("Number of S's in sentence: " + countS);
System.out.println("Number of T's in sentence: " + countT);
}
}
produces this output:
Character Counter Enter a sentence or phrase (quit to quit): This is a test! Enter a sentence or phrase (quit to quit): quit Number of blank spaces: 3 Number of A's in sentence: 1 Number of E's in sentence: 1 Number of S's in sentence: 3 Number of T's in sentence: 3 BUILD SUCCESSFUL (total time: 10 seconds)
This post has been edited by n8wxs: 21 March 2010 - 10:02 PM
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote



|