By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,997 Members | 1,470 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,997 IT Pros & Developers. It's quick & easy.

help on setting up letter counter with joptionpane dialog boxes

P: 5
What I'm bad at is the dialog box part. This is the code Ive come up with... if you see a way to improve what im thinkng of.... post it please.

Program should....
1. Ask user to type in a sentence, using JOptionPane.showInputDialog().
2. The program will scan each letter in the string and count how many times the upper case letter 'E' appears, and how many times the lower case letter 'e' appears.
3. Using a JOptionPane.showInputDialog(), tell the user how many upper and lower case e's were in the string.
4. Repeat this process until the user types the word 'Stop'

import javax.swing.*;
public class Project0 {
public static void main (String args[]) {
String line = JOptionPane.showInputDialog("Enter a line of text:");
// Checks if input has minimum of 1 character //if (line.length() == 0) // System.exit(0);
if (line.length() !=0 || line.length() ==0 ){ // If input is 'Stop' (case sensitivite) // program will terminate.
if(line.length() == 4 && line.charAt(0) == 83 && line.charAt(1) == 116 && line.charAt(2) == 111 && line.charAt(3) == 112 ) System.exit(0);
} // End check for "Stop" input
JOptionPane.showMessageDialog(null, "Your string contains " + uppercaseCount(line) + " uppercase letters 'E' and " + lowercaseCount(line) + " lowercase letters 'e'.");
} //end main // This method goes through the input "line" character by character // and keeps count of which are _UPPERCASE_ letters
}
}
}
// using charAt to cycle through each letter public static int uppercaseCount(String line)
{ int upperCount = 0;
for ( int i = 0; i < line.length(); i++ ) {
char x = line.charAt(i);
if ( x == 69 ) { upperCount++;
} //end if()
} //end loop
return upperCount; } //end uppercaseCount
public static int lowercaseCount(String line) {
int lowerCount = 0;
for ( int i = 0; i < line.length(); i++ ) {
char x = line.charAt(i);
if ( x == 101 ) { lowerCount++; } //end if()
} //end loop
return lowerCount; } //end uppercaseCount
} //end class
}


ehh it didnt come out looking clean here... but put it on eclipse or jdk... i think it works? oh and got to use that number system instead of just char 'e' string, etc.

thanks whomever understands this.
Sep 15 '07 #1
Share this Question
Share on Google+
3 Replies


10K+
P: 13,264
What I'm bad at is the dialog box part. This is the code Ive come up with... if you see a way to improve what im thinkng of.... post it please.

Program should....
1. Ask user to type in a sentence, using JOptionPane.showInputDialog().
2. The program will scan each letter in the string and count how many times the upper case letter 'E' appears, and how many times the lower case letter 'e' appears.
3. Using a JOptionPane.showInputDialog(), tell the user how many upper and lower case e's were in the string.
4. Repeat this process until the user types the word 'Stop'

import javax.swing.*;
public class Project0 {
public static void main (String args[]) {
String line = JOptionPane.showInputDialog("Enter a line of text:");
// Checks if input has minimum of 1 character //if (line.length() == 0) // System.exit(0);
if (line.length() !=0 || line.length() ==0 ){ // If input is 'Stop' (case sensitivite) // program will terminate.
if(line.length() == 4 && line.charAt(0) == 83 && line.charAt(1) == 116 && line.charAt(2) == 111 && line.charAt(3) == 112 ) System.exit(0);
} // End check for "Stop" input
JOptionPane.showMessageDialog(null, "Your string contains " + uppercaseCount(line) + " uppercase letters 'E' and " + lowercaseCount(line) + " lowercase letters 'e'.");
} //end main // This method goes through the input "line" character by character // and keeps count of which are _UPPERCASE_ letters
}
}
}
// using charAt to cycle through each letter public static int uppercaseCount(String line)
{ int upperCount = 0;
for ( int i = 0; i < line.length(); i++ ) {
char x = line.charAt(i);
if ( x == 69 ) { upperCount++;
} //end if()
} //end loop
return upperCount; } //end uppercaseCount
public static int lowercaseCount(String line) {
int lowerCount = 0;
for ( int i = 0; i < line.length(); i++ ) {
char x = line.charAt(i);
if ( x == 101 ) { lowerCount++; } //end if()
} //end loop
return lowerCount; } //end uppercaseCount
} //end class
}


ehh it didnt come out looking clean here... but put it on eclipse or jdk... i think it works? oh and got to use that number system instead of just char 'e' string, etc.

thanks whomever understands this.
Have you tried it?
Try it on the computer, pay special attention to any error messages you get. Post them here if you don't understand them or fail to remove them.
Sep 15 '07 #2

P: 5
ahahah it doesnt run for some odd reason, im guessing i fuked up some of my curlies... but im trying to find out if i made some obvious errors... or made it harder for myself by using a long code, thanks though
Sep 16 '07 #3

P: 5
import javax.swing.*;
public class Project0 {
public static void main (String args[]) {
String line = JOptionPane.showInputDialog("Please enter a sentence.");
// Checks if input has minimum of 1 character //if (line.length() == 0) // System.exit(0);
if (line.length() !=0 || line.length() ==0 ){ // If input is 'Stop' (case sensitivite) // program will terminate.
if(line.length() == 4 && line.charAt(0) == 83 && line.charAt(1) == 116 && line.charAt(2) == 111 && line.charAt(3) == 112 )
System.exit(0); } // End check for "Stop" input
JOptionPane.showMessageDialog(null, "Number of lower case e's: "+uppercaseCount(line)+" Number of upper case e's: "+lowercaseCount(line)+" lowercase letters 'e'."); } //end main
// This method goes through the input "line" character by character
// and keeps count of which are _UPPERCASE_ letters




{
// using charAt to cycle through each letter public static int uppercaseCount(String line)
{ int upperCount = 0;
for ( int i = 0; i < line.length(); i++ ) {
char x = line.charAt(i);
if ( x == 69 ) { upperCount++;
} //end if()
} //end loop
return upperCount; } //end uppercaseCount
public static int lowercaseCount(String line) {
int lowerCount = 0;
for ( int i = 0; i < line.length(); i++ ) {
char x = line.charAt(i);
if ( x == 101 ) { lowerCount++; //end if()
} //end loop
return lowerCount; } //end uppercaseCount
} //end class
}

My new code is looking like this ... but Im having 2 errors.... one being (The method uppercaseCount(string) is undefined for the type Project0) I dont know what that means on Eclipse.

And the other problem im having is having my output show on 2 lines..
"Number of lower case e's: "+uppercaseCount(line)+" Number of upper case e's: "+lowercaseCount(line)+" lowercase letters 'e'."

Please do help..
Sep 16 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.