Length.Java

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Oct 2009
Posts: 31
Reputation: COKEDUDE is an unknown quantity at this point 
Solved Threads: 1
COKEDUDE COKEDUDE is offline Offline
Light Poster

Length.Java

 
0
  #1
Oct 19th, 2009
Right now my code runs just the way I want it to. I would like the
  1. Enter a sentence or phrase
part to continue until the user enters "quit". I would like to use a while statement. Every way I have tried to enter phrase not equal to "quit" has not worked. Do I need to declare "quit" somewhere or what? Any help would be greatly appreciated.
  1. // **********************************************************
  2. // length.java
  3. //
  4. // This program reads in strings (phrases) and counts the
  5. // number of blank characters and certain other letters
  6. // in the phrase.
  7. // **********************************************************
  8.  
  9. import java.util.Scanner;
  10.  
  11. public class Length
  12. {
  13. public static void main (String[] args)
  14. {
  15. String phrase; // a string of characters
  16. int countBlank; // the number of blanks (spaces) in the phrase
  17. int length; // the length of the phrase
  18. int countA = 0; // the number of a's in the phrase
  19. int countE = 0; // the number of e's in the phrase
  20. int countS = 0; // the number of s's in the phrase
  21. int countT = 0; // the number of t's in the phrase
  22. char ch; // an individual character in the string
  23.  
  24. Scanner scan = new Scanner(System.in);
  25.  
  26. // Print a program header
  27. System.out.println ();
  28. System.out.println ("Character Counter");
  29. System.out.println ();
  30.  
  31. // Read in a string and find its length
  32. System.out.print ("Enter a sentence or phrase: ");
  33. //System.out.print ("Enter a sentence, phrase, or 'quit' to exit program: ");
  34. phrase = scan.nextLine();
  35. length = phrase.length();
  36.  
  37. // Initialize counts
  38. countBlank = 0;
  39.  
  40. // a for loop to go through the string character by character
  41. // and count the blank spaces
  42.  
  43. for(int i = 0; i < phrase.length(); i++)
  44. {
  45. ch = phrase.charAt(i);
  46.  
  47. switch (ch)
  48. {
  49. case ' ': countBlank++;
  50. break;
  51. case 'a':
  52. case 'A': countA++;
  53. break;
  54. case 'e':
  55. case 'E': countE++;
  56. break;
  57. case 's':
  58. case 'S': countS++;
  59. break;
  60. case 't':
  61. case 'T': countT++;
  62. break;
  63. }
  64. }
  65. // Print the results
  66. System.out.println ();
  67. System.out.println ("Number of blank spaces: " + countBlank);
  68. System.out.println ("Number of A's: " + countA);
  69. System.out.println ("Number of E's: " + countE);
  70. System.out.println ("Number of S's: " + countS);
  71. System.out.println ("Number of T's: " + countT);
  72. System.out.println ();
  73. }
  74. }
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,467
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 267
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven
 
0
  #2
Oct 19th, 2009
Wrap the thing from that point on in a while (!phrase.equals("quit")) { loop.
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 31
Reputation: COKEDUDE is an unknown quantity at this point 
Solved Threads: 1
COKEDUDE COKEDUDE is offline Offline
Light Poster
 
0
  #3
Oct 19th, 2009
Originally Posted by masijade View Post
Wrap the thing from that point on in a while (!phrase.equals("quit")) { loop.
Where would I place this?
while (!phrase.equals("quit")) {
If I do it before
  1. length = phrase.length();
it won't run because I haven't initialized "phrase". If I place it after the
  1. length = phrase.length();
, one of two things happens depending on where I place my "}". Option one at the end is it causes an infinite loop or option two if I place the "}" sooner is it does nothing.
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,467
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 267
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven
 
0
  #4
Oct 19th, 2009
Originally Posted by COKEDUDE View Post
Where would I place this?
while (!phrase.equals("quit")) {
If I do it before
  1. length = phrase.length();
it won't run because I haven't initialized "phrase".
Well, think about that a bit.
  1. ...
  2. String phrase; // a string of characters
  3. ...
  4. // Read in a string and find its length
  5. System.out.print ("Enter a sentence or phrase: ");
  6. ...
  7. System.out.println ();
  8. }

  1. ...
  2. String phrase = ""; // a string of characters
  3. ...
  4. while (!phrase.equals("quit")) {
  5. // Read in a string and find its length
  6. System.out.print ("Enter a sentence or phrase: ");
  7. ...
  8. System.out.println ();
  9. }
  10. }
Last edited by masijade; Oct 19th, 2009 at 3:12 am.
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 31
Reputation: COKEDUDE is an unknown quantity at this point 
Solved Threads: 1
COKEDUDE COKEDUDE is offline Offline
Light Poster
 
0
  #5
Oct 19th, 2009
I really don't understand what you are saying. How would I initialize "phrase" if i put
  1. while (!phrase.equals("quit")) {
so soon? I'm really new with java so please be kind to me.
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,467
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 267
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven
 
1
  #6
Oct 19th, 2009
Take a close look at my previous post.

The first code block is your code, the second code block is modifications to that code.
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,658
Reputation: BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold 
Solved Threads: 206
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso
 
0
  #7
Oct 19th, 2009
Originally Posted by COKEDUDE View Post
I really don't understand what you are saying. How would I initialize "phrase" if i put
  1. while (!phrase.equals("quit")) {
so soon? I'm really new with java so please be kind to me.
Phrase is initialized in the code that you posted. It is initialized to "", which is the empty String. In order to quit using the code Masijade showed you, after you say "please enter a sentence or phrase" inside of the while loop, you would need to read in the user's input, then store it into the phrase variable. If that doesn't make sense, then read about the Scanner class. http://java.sun.com/j2se/1.5.0/docs/...l/Scanner.html

Relevant topics: Where it says,
For example, this code allows a user to read a number from System.in:

Scanner sc = new Scanner(System.in);
int i = sc.nextInt();
But you cannot read in Strings with the nextInt method, so read about the nextLine() method.
Out.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 31
Reputation: COKEDUDE is an unknown quantity at this point 
Solved Threads: 1
COKEDUDE COKEDUDE is offline Offline
Light Poster
 
0
  #8
Oct 19th, 2009
Thx a lot masijade. I finally got it . Can you please explain why that works? I would like to learn. I would have never of figured that out on my own. My knowledge of java is fairly limited.
Last edited by COKEDUDE; Oct 19th, 2009 at 12:44 pm.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,658
Reputation: BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold 
Solved Threads: 206
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso
 
0
  #9
Oct 19th, 2009
Because phrase is a String object, and the "equals" method for the String class is implemented so that it compares two Strings to see if they are the same. If you are interested, you should read about the following topics:

Method overriding
Inheritance

And also, mark solved threads as solved
Last edited by BestJewSinceJC; Oct 19th, 2009 at 1:13 pm.
Out.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 31
Reputation: COKEDUDE is an unknown quantity at this point 
Solved Threads: 1
COKEDUDE COKEDUDE is offline Offline
Light Poster
 
0
  #10
Nov 2nd, 2009
Done. Sorry for the late reply.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 545 | Replies: 9
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC