944,214 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 1454
  • Java RSS
Oct 19th, 2009
0

Length.Java

Expand Post »
Right now my code runs just the way I want it to. I would like the
Java Syntax (Toggle Plain Text)
  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.
Java Syntax (Toggle Plain Text)
  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. }
Similar Threads
Reputation Points: 17
Solved Threads: 1
Junior Poster in Training
COKEDUDE is offline Offline
82 posts
since Oct 2009
Oct 19th, 2009
0
Re: Length.Java
Wrap the thing from that point on in a while (!phrase.equals("quit")) { loop.
Moderator
Reputation Points: 1471
Solved Threads: 490
Industrious Poster
masijade is offline Offline
4,043 posts
since Feb 2006
Oct 19th, 2009
0
Re: Length.Java
Click to Expand / Collapse  Quote originally posted by masijade ...
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
Java Syntax (Toggle Plain Text)
  1. length = phrase.length();
it won't run because I haven't initialized "phrase". If I place it after the
Java Syntax (Toggle Plain Text)
  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.
Reputation Points: 17
Solved Threads: 1
Junior Poster in Training
COKEDUDE is offline Offline
82 posts
since Oct 2009
Oct 19th, 2009
0
Re: Length.Java
Click to Expand / Collapse  Quote originally posted by COKEDUDE ...
Where would I place this?
while (!phrase.equals("quit")) {
If I do it before
Java Syntax (Toggle Plain Text)
  1. length = phrase.length();
it won't run because I haven't initialized "phrase".
Well, think about that a bit.
Java Syntax (Toggle Plain Text)
  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. }

Java Syntax (Toggle Plain Text)
  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.
Moderator
Reputation Points: 1471
Solved Threads: 490
Industrious Poster
masijade is offline Offline
4,043 posts
since Feb 2006
Oct 19th, 2009
0
Re: Length.Java
I really don't understand what you are saying. How would I initialize "phrase" if i put
Java Syntax (Toggle Plain Text)
  1. while (!phrase.equals("quit")) {
so soon? I'm really new with java so please be kind to me.
Reputation Points: 17
Solved Threads: 1
Junior Poster in Training
COKEDUDE is offline Offline
82 posts
since Oct 2009
Oct 19th, 2009
1
Re: Length.Java
Take a close look at my previous post.

The first code block is your code, the second code block is modifications to that code.
Moderator
Reputation Points: 1471
Solved Threads: 490
Industrious Poster
masijade is offline Offline
4,043 posts
since Feb 2006
Oct 19th, 2009
0
Re: Length.Java
Click to Expand / Collapse  Quote originally posted by COKEDUDE ...
I really don't understand what you are saying. How would I initialize "phrase" if i put
Java Syntax (Toggle Plain Text)
  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,
Quote ...
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.
Reputation Points: 874
Solved Threads: 352
Posting Maven
BestJewSinceJC is offline Offline
2,758 posts
since Sep 2008
Oct 19th, 2009
0
Re: Length.Java
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.
Reputation Points: 17
Solved Threads: 1
Junior Poster in Training
COKEDUDE is offline Offline
82 posts
since Oct 2009
Oct 19th, 2009
0
Re: Length.Java
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.
Reputation Points: 874
Solved Threads: 352
Posting Maven
BestJewSinceJC is offline Offline
2,758 posts
since Sep 2008
Nov 2nd, 2009
0
Re: Length.Java
Done. Sorry for the late reply.
Reputation Points: 17
Solved Threads: 1
Junior Poster in Training
COKEDUDE is offline Offline
82 posts
since Oct 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Store ArrayList in Hashmap???
Next Thread in Java Forum Timeline: While Loops





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC