Results 1 to 7 of 7
- 10-23-2008, 03:51 AM #1
Member
- Join Date
- Oct 2008
- Posts
- 13
- Rep Power
- 0
I can ot get my output to reflect accurate info, does anyone have a possible solution
///////////////////////////////////////////////////////////
import java.util.Scanner;
public class Count
{
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
int countA = 0; // the number of a's in the phrase
int countT = 0; // the number of t's in 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;
// a while loop to allow user to keep entering phrases
while (!phrase.equals("quit"))
phrase = scan.nextLine();
length = phrase.length();
{
// a for loop to go through the string character by character
// and count the blank spaces
for(int i = 0; i < phrase.length(); i++)
{
ch = phrase.charAt(i);
switch (ch)
{
case ' ': countBlank++;
break;
case 'a':
case 'A': countA++;
break;
case 't':
case 'T': countT++;
break;
}
}
// 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 T's: " + countT);
System.out.println ();
}
}
}
-
A couple of suggestions:
Please see comments.Java Code://while (!phrase.equals("quit")) while (!phrase.equalsIgnoreCase("quit")) // *1* { // *2* phrase = scan.nextLine(); // *3* }
*1* equalsIgnoreCase is probably better here than equals as you don't care if it's "quit" or "Quit" or "QUIT"
*2* ALWAYS enclose all blocks in curly braces, always.
*3* this overwrites the previous String held by the phrase variable (obtained in the first call to scan.nextLine()). Are you very sure that you want to do this? Wouldn't you rather concatenate strings here? (I know that I would!) By concatenate, I mean string1 += string2; BTW, this line is the cause of your bug.Last edited by Fubarable; 10-23-2008 at 04:15 AM.
-
correction to my correction:
If you do concatenate, do it with a separate String. In other words, use one String to get your input (and to check to see if "quit" entered) and another String that gets concatenated with the inputted String to do your tests on. Otherwise the concatenated String will never equal "quit" but rather will equal "a;sdlfkja;oeiua;sejquit"
- 10-23-2008, 04:30 AM #4
Member
- Join Date
- Oct 2008
- Posts
- 13
- Rep Power
- 0
Java Code://loop while (true) { //read a line String line=scan.nextLine(); //if its quit stop right now if(line.equals("quit")) break; else //ADD the line to the phrase phrase += line;}
okay i think thats the right correction
so my full program would be
Java Code:import java.util.Scanner; public class Count { 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 int countA = 0; // the number of a's in the phrase int countT = 0; // the number of t's in 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; // a while loop to allow user to keep entering phrases while (true) { //read a line String line=scan.nextLine(); //if its quit stop right now if(line.equals("quit")) break; else //ADD the line to the phrase phrase += line;} { // a for loop to go through the string character by character // and count the blank spaces for(int i = 0; i < phrase.length(); i++) { ch = phrase.charAt(i); switch (ch) { case ' ': countBlank++; break; case 'a': case 'A': countA++; break; case 't': case 'T': countT++; break; } } // 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 T's: " + countT); System.out.println (); } } }Last edited by gallimaufry; 10-23-2008 at 04:36 AM.
- 10-23-2008, 04:31 AM #5
Member
- Join Date
- Oct 2008
- Posts
- 13
- Rep Power
- 0
just wanna make sure it met these
Counting Characters
The file Count.java 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 other types of characters, such as vowels, consonants,
and punctuation ( , ; ! ? and .), not just blank spaces. To keep things relatively simple we'll count the
vowels and consonants in the string disregarding their cases. You need to declare and initialize three
additional counting variables (e.g. countVowels and so on). Your current if could be modified to
cascade (if-ladder).
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 (in any case). 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.
this is my only chance for extra credit and avoid failing..... ive been doing this for hoursLast edited by gallimaufry; 10-23-2008 at 04:37 AM.
-
1) Do you have a question, or are you just dumping your code?
2) When posting code, please use code tags so your code is readable. To do this, you will need to paste already formatted code into the forum, highlight this code, and then press the "code" button at the top of the forum Message editor prior to posting the message. Another way is to place the tag [code] at the top of your block of code and the tag [/code] at the bottom, like so:
Java Code:[code] // your code block goes here. // note the differences between the tag at the top vs the bottom. [/code]
- 10-23-2008, 04:39 AM #7
Member
- Join Date
- Oct 2008
- Posts
- 13
- Rep Power
- 0
Similar Threads
-
solution for my project
By shkelqa in forum AWT / SwingReplies: 4Last Post: 05-28-2008, 10:31 PM -
solution for my project
By themburu in forum Java AppletsReplies: 4Last Post: 05-21-2008, 01:03 PM -
Please need solution
By prithvi in forum New To JavaReplies: 4Last Post: 04-22-2008, 01:27 PM -
FEST-Reflect 0.4
By JavaBean in forum Java SoftwareReplies: 0Last Post: 03-01-2008, 10:21 PM -
How to reflect the changes
By priyanka_t in forum JavaServer Pages (JSP) and JSTLReplies: 0Last Post: 12-05-2007, 01:34 PM


LinkBack URL
About LinkBacks

Bookmarks