help on setting up letter counter with joptionpane dialog boxes

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • majorecono
    New Member
    • Sep 2007
    • 5

    help on setting up letter counter with joptionpane dialog boxes

    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.sho wInputDialog().
    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.sho wInputDialog(), 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.sho wInputDialog("E nter 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.sho wMessageDialog( 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.
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by majorecono
    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.sho wInputDialog().
    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.sho wInputDialog(), 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.sho wInputDialog("E nter 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.sho wMessageDialog( 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.

    Comment

    • majorecono
      New Member
      • Sep 2007
      • 5

      #3
      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

      Comment

      • majorecono
        New Member
        • Sep 2007
        • 5

        #4
        import javax.swing.*;
        public class Project0 {
        public static void main (String args[]) {
        String line = JOptionPane.sho wInputDialog("P lease 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.sho wMessageDialog( null, "Number of lower case e's: "+uppercaseCoun t(line)+" Number of upper case e's: "+lowercaseCoun t(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: "+uppercaseCoun t(line)+" Number of upper case e's: "+lowercaseCoun t(line)+" lowercase letters 'e'."

        Please do help..

        Comment

        Working...