| | |
Length.Java
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Oct 2009
Posts: 31
Reputation:
Solved Threads: 1
Right now my code runs just the way I want it to. I would like the 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)
Enter a sentence or phrase
Java Syntax (Toggle Plain Text)
// ********************************************************** // length.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 Length { 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 countE = 0; // the number of e's in the phrase int countS = 0; // the number of s'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: "); //System.out.print ("Enter a sentence, phrase, or 'quit' to exit program: "); phrase = scan.nextLine(); length = phrase.length(); // Initialize counts countBlank = 0; // 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 'e': case 'E': countE++; break; case 's': case 'S': countS++; 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 E's: " + countE); System.out.println ("Number of S's: " + countS); System.out.println ("Number of T's: " + countT); System.out.println (); } }
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
----------------------------------------------
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
•
•
Join Date: Oct 2009
Posts: 31
Reputation:
Solved Threads: 1
0
#3 Oct 19th, 2009
•
•
•
•
Wrap the thing from that point on in a while (!phrase.equals("quit")) { loop. while (!phrase.equals("quit")) {If I do it before
Java Syntax (Toggle Plain Text)
length = phrase.length();
Java Syntax (Toggle Plain Text)
length = phrase.length();
0
#4 Oct 19th, 2009
•
•
•
•
Where would I place this?
while (!phrase.equals("quit")) {
If I do it beforeit won't run because I haven't initialized "phrase".Java Syntax (Toggle Plain Text)
length = phrase.length();
Java Syntax (Toggle Plain Text)
... String phrase; // a string of characters ... // Read in a string and find its length System.out.print ("Enter a sentence or phrase: "); ... System.out.println (); }
Java Syntax (Toggle Plain Text)
... String phrase = ""; // a string of characters ... while (!phrase.equals("quit")) { // Read in a string and find its length System.out.print ("Enter a sentence or phrase: "); ... System.out.println (); } }
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
----------------------------------------------
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
•
•
Join Date: Oct 2009
Posts: 31
Reputation:
Solved Threads: 1
0
#5 Oct 19th, 2009
I really don't understand what you are saying. How would I initialize "phrase" if i put so soon? I'm really new with java so please be kind to me.
Java Syntax (Toggle Plain Text)
while (!phrase.equals("quit")) {
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.
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
----------------------------------------------
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
•
•
Join Date: Sep 2008
Posts: 1,658
Reputation:
Solved Threads: 206
0
#7 Oct 19th, 2009
•
•
•
•
I really don't understand what you are saying. How would I initialize "phrase" if i putso soon? I'm really new with java so please be kind to me.Java Syntax (Toggle Plain Text)
while (!phrase.equals("quit")) {
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();
Out.
•
•
Join Date: Sep 2008
Posts: 1,658
Reputation:
Solved Threads: 206
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
Method overriding
Inheritance
And also, mark solved threads as solved
Last edited by BestJewSinceJC; Oct 19th, 2009 at 1:13 pm.
Out.
![]() |
Similar Threads
- Array list or character ? in java (Java)
- Understanding some SQLite Java Wrapper code (Java)
- Find the length of of the longest group of duplicates in a Java array (Java)
- finding the length in java (Computer Science)
- java uses or overrides a deprecated API?? (Java)
- I can't implement a word count into my text editor (JAVA) (Java)
- I need an exampls program in java to store image datatype in ms-sql (MS SQL)
Other Threads in the Java Forum
- Previous Thread: Store ArrayList in Hashmap???
- Next Thread: While Loops
Views: 545 | Replies: 9
| Thread Tools | Search this Thread |
Tag cloud for Java
actionlistener android api apple applet application arguments array arrays automation binary bluetooth character chat class classes client code component consumer database desktop detection draw eclipse encode error event exception file fractal ftp game givemetehcodez graphics gui helpwithhomework html ide image input integer j2me java javac javaee javaprojects jmf jni jpanel julia linked linux list loop mac map method methods mobile netbeans newbie number object online oracle os print printf problem program programming project properties recursion researchinmotion rotatetext rsa scanner score screen server set size sms socket sort sql string swing template test threads time transfer tree update windows working xstream






. 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. 