Results 1 to 4 of 4

Thread: help

  1. #1
    blueduiker is offline Member
    Join Date
    Nov 2009
    Location
    California
    Posts
    55
    Rep Power
    0

    Default help

    Assn:
    Write a program that gives the user a menu with two options—

    a) letter count

    b) word length

    letter count reads several lines of text from the keyboard and prints a table indicating the number of occurrences of each letter of the alphabet in the text. For example, the phrase



    To be, or not to be: that is the question:

    contains one “a”, two “b’s”, no “c’s,” etc.

    word_length reads several lines of text and prints a table indicating the number of one-letter words, two-letter-words, three-letter words, etc. appearing in the text. For example, the phrase

    Whether ‘tis nobler in the mind to suffer

    contains

    Word length Occurrences

    1 0

    Java Code:
    import java.util.Scanner;
    public class test_analysis{
      public static void main (String[] args) {
        
        //This is my declaration section
        String decision, input;
        int sumOFa = 0, sumOFb = 0, sumOFc = 0, sumOFd = 0, sumOFe = 0, sumOFf = 0, sumOFg = 0, sumOFh = 0, sumOFi = 0;
        int sumOFj = 0, sumOFk = 0, sumOFl = 0, sumOFm = 0, sumOFn = 0, sumOFo = 0, sumOFp = 0, sumOFq = 0, sumOFr = 0, sumOFs = 0, sumOFt = 0, sumOFu = 0, sumOFv = 0, sumOFw = 0, sumOFx = 0, sumOFy = 0, sumOFz = 0;
        int sumOFA = 0, sumOFB = 0, sumOFC = 0, sumOFD = 0, sumOFE = 0, sumOFF = 0, sumOFG = 0, sumOFH = 0, sumOFI = 0, sumOFJ = 0, sumOFK = 0, sumOFL = 0, sumOFM = 0, sumOFN = 0, sumOFO = 0, sumOFP = 0;
        int sumOFQ = 0, sumOFR = 0, sumOFS = 0, sumOFT = 0, sumOFU = 0, sumOFV = 0, sumOFW = 0, sumOFX = 0, sumOFY = 0, sumOFZ = 0;
        
        Scanner scan = new Scanner(System.in);
        
        //This section gets the user's inputed string
        System.out.println ("Welcome to Test Analysis");
        System.out.print ("Enter the string that you would like to be evaluated:");
        input = scan.next();
        System.out.println ("Your options for this application are: a) letter count OR b) word count");
        System.out.print ("Please choose which operation you would like to perform:");
        decision = scan.next();
        
        //Screens out bad answers
       /* while (decision != "a" || decision != "b") {
          System.out.println ("Error: the only acceptable choices are either a or b.");
          System.out.print ("Please choose which operation you would like to perform:");
          decision = scan.next();
        }*/
        while (decision == "a") {
          int length = input.length();
          for (int i = 0; i < length; i++) {
             switch (input.charAt(i)) {
               case 'a':
                 sumOFa++;
                 break;
               case 'b':
                 sumOFb++;
                 break;
               case 'c':
                 sumOFc++;
                 break;
               case 'd':
                 sumOFd++;
                 break;
               case 'e':
                 sumOFe++;
                 break;
               case 'f':
                 sumOFf++;
                 break;
               case 'g':
                 sumOFg++;
                 break;
               case 'h':
                 sumOFh++;
                 break;
               case 'i':
                 sumOFi++;
                 break;
               case 'j':
                 sumOFj++;
                 break;
               case 'k':
                 sumOFk++;
                 break;
               case 'l':
                 sumOFl++;
                 break;
               case 'm':
                 sumOFm++;
                 break;
               case 'n':
                 sumOFn++;
                 break;
               case 'o':
                 sumOFo++;
                 break;
               case 'p':
                 sumOFp++;
                 break;
               case 'q':
                 sumOFq++;
                 break;
               case 'r':
                 sumOFr++;
                 break;
               case 's':
                 sumOFs++;
                 break;
               case 't':
                 sumOFt++;
                 break;
               case 'u':
                 sumOFu++;
                 break;
               case 'v':
                 sumOFv++;
                 break;
               case 'w':
                 sumOFw++;
                 break;
               case 'x':
                 sumOFx++;
                 break;
               case 'y':
                 sumOFy++;
                 break;
               case 'z':
                 sumOFz++;
                 break;
                 case 'A':
                 sumOFA++;
                 break;
               case 'B':
                 sumOFB++;
                 break;
               case 'C':
                 sumOFC++;
                 break;
               case 'D':
                 sumOFD++;
                 break;
               case 'E':
                 sumOFE++;
                 break;
               case 'F':
                 sumOFF++;
                 break;
               case 'G':
                 sumOFG++;
                 break;
               case 'H':
                 sumOFH++;
                 break;
               case 'I':
                 sumOFI++;
                 break;
               case 'J':
                 sumOFJ++;
                 break;
               case 'K':
                 sumOFK++;
                 break;
               case 'L':
                 sumOFL++;
                 break;
               case 'M':
                 sumOFM++;
                 break;
               case 'N':
                 sumOFN++;
                 break;
               case 'O':
                 sumOFO++;
                 break;
               case 'P':
                 sumOFP++;
                 break;
               case 'Q':
                 sumOFQ++;
                 break;
               case 'R':
                 sumOFR++;
                 break;
               case 'S':
                 sumOFS++;
                 break;
               case 'T':
                 sumOFT++;
                 break;
               case 'U':
                 sumOFU++;
                 break;
               case 'V':
                 sumOFV++;
                 break;
               case 'W':
                 sumOFW++;
                 break;
               case 'X':
                 sumOFX++;
                 break;
               case 'Y':
                 sumOFY++;
                 break;
               case 'Z':
                 sumOFZ++;
                 break;
             }
          }
           System.out.println ("Your input: " + input);
           System.out.print ("Cotains: ");
           sumOFa += sumOFA;
           if (sumOFa > 0){
             if (sumOFa == 1)
               System.out.print ("1 \"a\", ");
             else
               System.out.print (sumOFa + "\"a\'s\", ");
           }
           else {
             System.out.print ("no \"a\'s\", ");
           }
           
           sumOFb += sumOFB;
           if (sumOFb > 0){
             if (sumOFb == 1)
               System.out.print ("1 \"b\", ");
             else
               System.out.print (sumOFb + "\"b\'s\", ");
           }
           else {
             System.out.print ("no \"b\'s\", ");
           }
           
           sumOFc += sumOFC;
           if (sumOFc > 0){
             if (sumOFc == 1)
               System.out.print ("1 \"c\", ");
             else
               System.out.print (sumOFc + "\"c\'s\", ");
           }
           else {
             System.out.print ("no \"c\'s\", ");
           }
               
           sumOFd += sumOFD;
           if (sumOFd > 0){
             if (sumOFd == 1)
               System.out.print ("1 \"d\", ");
             else
               System.out.print (sumOFd + "\"d\'s\", ");
           }
           else {
             System.out.print ("no \"d\'s\", ");
           }
           
           sumOFe += sumOFE;
           if (sumOFe > 0){
             if (sumOFe == 1)
               System.out.print ("1 \"e\", ");
             else
               System.out.print (sumOFe + "\"e\'s\", ");
           }
           else {
             System.out.print ("no \"e\'s\", ");
           }
           
           sumOFf += sumOFF;
           if (sumOFf > 0){
             if (sumOFf == 1)
               System.out.print ("1 \"f\", ");
             else
               System.out.print (sumOFf + "\"f\'s\", ");
           }
           else {
             System.out.print ("no \"f\'s\", ");
           }
           
           sumOFg += sumOFG;
           if (sumOFg > 0){
             if (sumOFg == 1)
               System.out.print ("1 \"g\", ");
             else
               System.out.print (sumOFg + "\"g\'s\", ");
           }
           else {
             System.out.print ("no \"g\'s\", ");
           }
           
           sumOFh += sumOFH;
           if (sumOFh > 0){
             if (sumOFh == 1)
               System.out.print ("1 \"h\", ");
             else
               System.out.print (sumOFh + "\"h\'s\", ");
           }
           else {
             System.out.print ("no \"h\'s\", ");
           }
           
           sumOFi += sumOFI;
           if (sumOFi > 0){
             if (sumOFi == 1)
               System.out.print ("1 \"i\", ");
             else
               System.out.print (sumOFi + "\"i\'s\", ");
           }
           else {
             System.out.print ("no \"i\'s\", ");
           }
           
           sumOFj += sumOFJ;
           if (sumOFj > 0){
             if (sumOFj == 1)
               System.out.print ("1 \"j\", ");
             else
               System.out.print (sumOFj + "\"j\'s\", ");
           }
           else {
             System.out.print ("no \"j\'s\", ");
           }
           
           sumOFk += sumOFK;
           if (sumOFk > 0){
             if (sumOFk == 1)
               System.out.print ("1 \"k\", ");
             else
               System.out.print (sumOFk + "\"k\'s\", ");
           }
           else {
             System.out.print ("no \"k\'s\", ");
           }
           
           sumOFl += sumOFL;
           if (sumOFl > 0){
             if (sumOFl == 1)
               System.out.print ("1 \"l\", ");
             else
               System.out.print (sumOFl + "\"l\'s\", ");
           }
           else {
             System.out.print ("no \"l\'s\", ");
           }
           
           sumOFm += sumOFM;
           if (sumOFm > 0){
             if (sumOFm == 1)
               System.out.print ("1 \"m\", ");
             else
               System.out.print (sumOFm + "\"m\'s\", ");
           }
           else {
             System.out.print ("no \"m\'s\", ");
           }
           
           sumOFn += sumOFN;
           if (sumOFn > 0){
             if (sumOFn == 1)
               System.out.print ("1 \"n\", ");
             else
               System.out.print (sumOFn + "\"n\'s\", ");
           }
           else {
             System.out.print ("no \"n\'s\", ");
           }
           
           sumOFo += sumOFO;
           if (sumOFo > 0){
             if (sumOFo == 1)
               System.out.print ("1 \"o\", ");
             else
               System.out.print (sumOFo + "\"o\'s\", ");
           }
           else {
             System.out.print ("no \"o\'s\", ");
           }
             
           sumOFp += sumOFP;
           if (sumOFp > 0){
             if (sumOFp == 1)
               System.out.print ("1 \"p\", ");
             else
               System.out.print (sumOFp + "\"p\'s\", ");
           }
           else {
             System.out.print ("no \"p\'s\", ");
           }
           
           sumOFq += sumOFQ;
           if (sumOFq > 0){
             if (sumOFq == 1)
               System.out.print ("1 \"q\", ");
             else
               System.out.print (sumOFq + "\"q\'s\", ");
           }
           else {
             System.out.print ("no \"q\'s\", ");
           }
           
           sumOFr += sumOFR;
           if (sumOFr > 0){
             if (sumOFr == 1)
               System.out.print ("1 \"r\", ");
             else
               System.out.print (sumOFr + "\"r\'s\", ");
           }
           else {
             System.out.print ("no \"r\'s\", ");
           }
           
           sumOFs += sumOFS;
           if (sumOFs > 0){
             if (sumOFs == 1)
               System.out.print ("1 \"s\", ");
             else
               System.out.print (sumOFs + "\"s\'s\", ");
           }
           else {
             System.out.print ("no \"s\'s\", ");
           }
           
           sumOFt += sumOFT;
           if (sumOFt > 0){
             if (sumOFt == 1)
               System.out.print ("1 \"t\", ");
             else
               System.out.print (sumOFt + "\"t\'s\", ");
           }
           else {
             System.out.print ("no \"t\'s\", ");
           }
           
           sumOFu += sumOFU;
           if (sumOFu > 0){
             if (sumOFu == 1)
               System.out.print ("1 \"u\", ");
             else
               System.out.print (sumOFu + "\"u\'s\", ");
           }
           else {
             System.out.print ("no \"u\'s\", ");
           }
           
           sumOFv += sumOFV;
           if (sumOFv > 0){
             if (sumOFv == 1)
               System.out.print ("1 \"v\", ");
             else
               System.out.print (sumOFa + "\"v\'s\", ");
           }
           else {
             System.out.print ("no \"v\'s\", ");
           }
           
           sumOFw += sumOFW;
           if (sumOFw > 0){
             if (sumOFw == 1)
               System.out.print ("1 \"w\", ");
             else
               System.out.print (sumOFw + "\"w\'s\", ");
           }
           else {
             System.out.print ("no \"w\'s\", ");
           }
           
           sumOFx += sumOFX;
           if (sumOFs > 0){
             if (sumOFs == 1)
               System.out.print ("1 \"s\", ");
             else
               System.out.print (sumOFs + "\"s\'s\", ");
           }
           else {
             System.out.print ("no \"s\'s\", ");
           }
           
           sumOFy += sumOFY;
           if (sumOFy > 0){
             if (sumOFy == 1)
               System.out.print ("1 \"y\", ");
             else
               System.out.print (sumOFy + "\"y\'s\", ");
           }
           else {
             System.out.print ("no \"y\'s\", ");
           }
           
           sumOFz += sumOFZ;
           if (sumOFz > 0){
             if (sumOFz == 1)
               System.out.print ("1 \"z\", ");
             else
               System.out.print (sumOFz + "\"z\'s\", ");
           }
           else {
             System.out.print ("no \"z\'s\", ");
           }
        }
        while (decision == "b") {
          
        }
      }
    }

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,429
    Rep Power
    7

    Default

    You forgot to post your question
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    gcampton Guest

    Default

    id probably convert everything to uppercase, or lowercase to make life easier, then use combination of split() and / or useDelimeter("1-0,a-z"... etc, count it that way...

    eg store in string object, if reading words count simply use string.split(); wordCount = string.length; create a new string to concatinate the previous array into the string without spaces, then string.split("A-Z,0-9"); and count letters.

    or something to that effect.
    Last edited by gcampton; 11-12-2009 at 12:36 AM.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    This code could likely be shrunk by a factor of 70% or more.

    I second the request for you to ask an actual question, since without this, we have no idea what your current specific problem is (other than severe code redundancy).

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •