Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 25 of 25

Thread: Java Program - Letter Frequencies - HELP!

  1. #1
    Member
    Join Date
    Mar 2013
    Posts
    37
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Default Java Program - Letter Frequencies - HELP!

    I have a programming assignment that I can't figure out. Please help!

    For this assignment you are to print a table that gives approximate letter frequencies for English text. To keep things simple and concrete, your job will be to print out the letter frequencies for multiple lines of text you enter from the keyboard. More specifically, you program should continue entering lines of text until you type two Returns in a row. Then your code should print the letter frequencies, followed by a report that gives the most frequent letter and its count (in case of ties for most frequent letter, any most frequent letter will do). Also, your code should ignore letter case - so capital letters as well as lower case letters should be counted.

    Enter lines of text, type two returns in a row to end


    now is the time

    The quick brown fox jumped over the LAZY dog

    and why not?


    a 2

    b 1

    c 1

    d 3

    e 6

    f 1

    g 1

    h 4

    i 3

    j 1

    k 1

    l 1

    m 2

    n 4

    o 6

    p 1

    q 1

    r 2

    s 1

    t 5

    u 2

    v 1

    w 3

    x 1

    y 2

    z 1



    most frequent: e 6



    Tips/Further Qualifications


    Use the input style that's used in Program Backwards in Chapter 7. (It accepts multiple lines of text). However, there's no need to store the entered lines in an array (as happens in Program Backwards). For this assignment you can process each line after it's been entered in one pass, and then, in effect, you can throw it away.
    You MUST do this assignment with two classes: a driver, which handles the input, and is called LetterDriver, and a principal processing class, called LetterProfile. Objects in the LetterProfile class are responsible for handling the character processing. LetterProfile must have an array instance variable that functions like a scoreboard, tallying letter frequencies as lines are read.
    Convert all text as it comes in to lower case. Ignore characters that aren't letters. Remember that if someLetter is a char variable that holds a letter, then (someLetter-'a') gives the numerical position of someLetter among all lower case letters. Thus if someLetter holds an 'e', then (someLetter-'a') = 4. Use this fact to guide the updating of your letter frequency array scoreboard.
    As always, be sure to comment your code.

    Backwards Program:

    import java.util.*;

    public class Backwards{
    public static void main(String[] args){
    String[] lines = new String[50];
    Scanner scan = new Scanner(System.in);
    int pos = 0;
    String t = " ";
    while(t.length() > 0){
    t = scan.nextLine();
    lines[pos] = t;
    pos++;
    }
    for(int j = pos - 1; j >= 0; j--){
    lines[j] = lines[j].toUpperCase();
    System.out.println(lines[j]);
    }
    }
    }


    Here is what I have so far:

    Driver Class:

    import java.util.*;

    public class LetterDriver{
    public static void main(String[] args){
    Scanner scan = new Scanner(System.in);
    String tScan= " ";
    while(tScan.length() > 0){
    tScan = scan.nextLine();
    }
    }

    }

    Profile class:

    public class LetterProfile{
    int scoreboard[] = new int [26];

    public void countChars (String s)
    {
    s.toLowerCase();
    char a = 'a';
    for (int i =0;i < s.length();i++){
    int next = (int)s.charAt(i)-(int) a;
    if ( next<26 && next >= 0) //if the character is a-z
    scoreboard[next]++;
    }
    }
    public void scoreLine (String line){
    int index = line.length();
    for (int j = 0; j<index; j++)
    scoreboard[j]++;
    System.out.println(j + scoreboard[j]);
    }
    public int largestLength(){
    int largest = 0;
    int largestindex = 0;
    for(int a = 0; a<26; a++)
    if(scores[a]>largest)
    largest = scores[a];
    largestindex = a;
    return(char)largestindex;
    }
    public void printResults(){
    System.out.println(largestLength());
    }
    }



    I am getting so many errors and I am horrible at java programming and can't fix it );
    Any advice would be appreciated! Thanks!


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Java Program - Letter Frequencies - HELP!

    Please copy the full text of the error messages and paste it here.

    Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    kbrady481 (March 13th, 2013)

  4. #3
    Member
    Join Date
    Mar 2013
    Posts
    37
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Default Re: Java Program - Letter Frequencies - HELP!

    import java.util.*;
     
    public class LetterDriver{
      public static void main(String[] args){
        Scanner scan = new Scanner(System.in);
        String tScan= " ";
        while(tScan.length() > 0){
          tScan = scan.nextLine();
        }
      }
     
    }

    public class LetterProfile{
      int scoreboard[] = new int [26];
     
     public void countChars (String s)
      {
        s.toLowerCase();
        char a = 'a';
        for (int i =0;i < s.length();i++){
          int next = (int)s.charAt(i)-(int) a;
          if ( next<26 && next >= 0) //if the character is a-z
            scoreboard[next]++;
        }
     }
        public void scoreLine (String line){
          int index = line.length();
          for (int j = 0; j<index; j++)
          scoreboard[j]++;
          System.out.println(j + scoreboard[j]);
        }
        public int largestLength(){
          int largest = 0;
          int largestindex = 0;
          for(int a = 0; a<26; a++)
            if(scores[a]>largest)
            largest = scores[a];
          largestindex = a;
          return(char)largestindex;
        }
        public void printResults(){
          System.out.println(largestLength());
        }
     }

    errors:

    6 errors found:
    File: C:\Users\Edward\LetterProfile.java [line: 18]
    Error: C:\Users\Edward\LetterProfile.java:18: reference to println is ambiguous, both method println(char[]) in java.io.PrintStream and method println(java.lang.String) in java.io.PrintStream match
    File: C:\Users\Edward\LetterProfile.java [line: 18]
    Error: C:\Users\Edward\LetterProfile.java:18: cannot find symbol
    symbol : variable j
    location: class LetterProfile
    File: C:\Users\Edward\LetterProfile.java [line: 18]
    Error: C:\Users\Edward\LetterProfile.java:18: cannot find symbol
    symbol : variable j
    location: class LetterProfile
    File: C:\Users\Edward\LetterProfile.java [line: 24]
    Error: C:\Users\Edward\LetterProfile.java:24: cannot find symbol
    symbol : variable scores
    location: class LetterProfile
    File: C:\Users\Edward\LetterProfile.java [line: 25]
    Error: C:\Users\Edward\LetterProfile.java:25: cannot find symbol
    symbol : variable scores
    location: class LetterProfile
    File: C:\Users\Edward\LetterProfile.java [line: 26]
    Error: C:\Users\Edward\LetterProfile.java:26: cannot find symbol
    symbol : variable a
    location: class LetterProfile

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Java Program - Letter Frequencies - HELP!

    cannot find symbol
    The compiler gives that message when it can not find a definition that is in scope (within the same pair of {}s) for the symbol named in the error message. The code needs to define/declare any variables before it tries to use it and make sure the definition is in scope.

    One problem I see is that the for and if statements do not use {}s to enclose the code that is in the statement.
    The {}s make the code easier to read, understand and modify.

    Also the nested statements within {}s should be indented 3-4 spaces to make the code easier to read and understand.
    If you don't understand my answer, don't ignore it, ask a question.

  6. The Following User Says Thank You to Norm For This Useful Post:

    kbrady481 (March 13th, 2013)

  7. #5
    Member
    Join Date
    Mar 2013
    Posts
    37
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Default Re: Java Program - Letter Frequencies - HELP!

    Thank you!

    public class LetterProfile{
      int scoreboard[] = new int [26];
     
     public void countChars (String s)
      {
        s.toLowerCase();
        char a = 'a';
        for (int i =0;i < s.length();i++)
        {
          int next = (int)s.charAt(i)-(int) a;
          if ( next<26 && next >= 0) //if the character is a-z
            scoreboard[next]++;
        }
     }
        public void scoreLine (String line)
        {
          int index = line.length();
          for (int j = 0; j<index; j++)
          {
            scoreboard[j]++;
          }
          System.out.println(j + scoreboard[j]);
        }
        public int largestLength(){
          int largest = 0;
          int largestindex = 0;
          for(int a = 0; a<26; a++)
          {
            if(scores[a]>largest)
            largest = scores[a];
            largestindex = a;
          }
          return(char)largestindex;
        }
        public void printResults(){
          System.out.println(largestLength());
        }
     }

    I am still getting the following errors:

    5 errors found:

    File: C:\Users\Edward\LetterProfile.java [line: 22]
    Error: C:\Users\Edward\LetterProfile.java:22: reference to println is ambiguous, both method println(char[]) in java.io.PrintStream and method println(java.lang.String) in java.io.PrintStream match
    File: C:\Users\Edward\LetterProfile.java [line: 22]
    Error: C:\Users\Edward\LetterProfile.java:22: cannot find symbol
    symbol : variable j
    location: class LetterProfile
    File: C:\Users\Edward\LetterProfile.java [line: 22]
    Error: C:\Users\Edward\LetterProfile.java:22: cannot find symbol
    symbol : variable j
    location: class LetterProfile
    File: C:\Users\Edward\LetterProfile.java [line: 29]
    Error: C:\Users\Edward\LetterProfile.java:29: cannot find symbol
    symbol : variable scores
    location: class LetterProfile
    File: C:\Users\Edward\LetterProfile.java [line: 30]
    Error: C:\Users\Edward\LetterProfile.java:30: cannot find symbol
    symbol : variable scores
    location: class LetterProfile

  8. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Java Program - Letter Frequencies - HELP!

    Error: C:\Users\Edward\LetterProfile.java:22: cannot find symbol
    symbol : variable j
    The scope of a variable used in a for loop is limited to the end of the for loop. It is not known outside of the for loop.

    What version of the JDK are you working with? I don't get the first error.

    Error: C:\Users\Edward\LetterProfile.java:29: cannot find symbol
    symbol : variable scores
    Where the symbol: scores defined?
    If you don't understand my answer, don't ignore it, ask a question.

  9. The Following User Says Thank You to Norm For This Useful Post:

    kbrady481 (March 13th, 2013)

  10. #7
    Member
    Join Date
    Mar 2013
    Posts
    37
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Default Re: Java Program - Letter Frequencies - HELP!

    i think i have the most recent version of java and im using dr.java

    --- Update ---

    ok I managed to get rid of that weird first error. now I am down to 2 errors with the last for loop.

    public class LetterProfile{
      int scoreboard[] = new int [26];
     
     public void countChars (String s)
      {
        s.toLowerCase();
        char a = 'a';
        for (int i =0;i < s.length();i++)
        {
          int next = (int)s.charAt(i)-(int) a;
          if ( next<26 && next >= 0) //if the character is a-z
            scoreboard[next]++;
        }
     }
        public void scoreLine (String line)
        {
          int index = line.length();
          for (int j = 0; j<index; j++)
          {
            scoreboard[j]++;
          System.out.println(j + scoreboard[j]);
        }
        }
        public int largestLength(){
          int largest = 0;
          int largestindex = 0;
          for(int a = 0; a<26; a++){
            if(scores[a]>largest)
            largest = scores[a];
            largestindex = a;
          return(char)largestindex;
        }
    }
        public void printResults(){
          System.out.println(largestLength());
        }
     }

    errors:

    2 errors found:
    File: C:\Users\Edward\LetterProfile.java [line: 28]
    Error: C:\Users\Edward\LetterProfile.java:28: cannot find symbol
    symbol : variable scores
    location: class LetterProfile
    File: C:\Users\Edward\LetterProfile.java [line: 29]
    Error: C:\Users\Edward\LetterProfile.java:29: cannot find symbol
    symbol : variable scores
    location: class LetterProfile

  11. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Java Program - Letter Frequencies - HELP!

    Use your editor's Find or Search function to find where the variable named in the error message is defined.
    The compiler can not find a definition that is in scope where that variable is being used.
    If you don't understand my answer, don't ignore it, ask a question.

  12. The Following User Says Thank You to Norm For This Useful Post:

    kbrady481 (March 13th, 2013)

  13. #9
    Member
    Join Date
    Mar 2013
    Posts
    37
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Default Re: Java Program - Letter Frequencies - HELP!

    Thank you so much! I have gotten rid of all of the errors, but my code still does not return anything. I have print statements, so I am confused about that. But you have been a huge help! Thanks!

    --- Update ---

    updated profile class:

    public class LetterProfile{
      int scoreboard[] = new int [26];
     
     public void countChars (String s)
      {
        s.toLowerCase();
        char a = 'a';
        for (int i =0;i < s.length();i++)
        {
          int next = (int)s.charAt(i)-(int) a;
          if ( next<26 && next >= 0) //if the character is a-z
            scoreboard[next]++;
        }
     }
        public void scoreLine (String line)
        {
          int index = line.length();
          for (int j = 0; j<index; j++)
          {
            scoreboard[j]++;
          System.out.println(j + scoreboard[j]);
        }
        }
        public int largestLength(){
          int largest = 0;
          int largestindex = 0;
          for(int a = 0; a<26; a++){
            if(scoreboard[a]>largest)
            largest = scoreboard[a];
            largestindex = a;}
          return(char)largestindex;
    }
        public void printResults(){
          System.out.println(largestLength());
        }
     }

  14. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Java Program - Letter Frequencies - HELP!

    What happens when you compile and execute the code? What does it print out?

    You shouldn't hide }s at the end of a statement. A } should be on a line by itself so it is easy to see.

    Statements within if statements should be enclosed with {} and be indented.
    If you don't understand my answer, don't ignore it, ask a question.

  15. #11
    Member
    Join Date
    Mar 2013
    Posts
    37
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Default Re: Java Program - Letter Frequencies - HELP!

    When I run the program, it asks me for an input. However, when I enter some text and press enter twice, the code is supposed to print out each letter in the entered text, and how many times that letter has occurred in the text. When I run this code, it does ask for an input, but it does not return anything after i press enter twice.

  16. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Java Program - Letter Frequencies - HELP!

    Add some more println() statements that print out messages as different methods are called and different loops executed so you can see where the execution flow goes. If nothing prints, add some more statements until you see where the code is executing. If no println in a method prints, that means that code is not being called and executed. Then look at the code to see why the method is not called. Perhaps you have left out calls to methods so that they are never executed.
    If you don't understand my answer, don't ignore it, ask a question.

  17. The Following User Says Thank You to Norm For This Useful Post:

    kbrady481 (March 13th, 2013)

  18. #13
    Junior Member
    Join Date
    Mar 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java Program - Letter Frequencies - HELP!

    kbrady I'm having the same problem with nothing returning

  19. #14
    Member
    Join Date
    Mar 2013
    Posts
    37
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Default Re: Java Program - Letter Frequencies - HELP!

    I'm pretty sure it has something to do with the driver, but I have no idea how to fix it. I am going to office hours tomorrow to figure it out...

  20. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Java Program - Letter Frequencies - HELP!

    Where does the code call a method that has a print or println() method in it? If you don't execute a print method nothing will be printed. The code has to call the methods that have calls to the print methods.
    If you don't understand my answer, don't ignore it, ask a question.

  21. The Following User Says Thank You to Norm For This Useful Post:

    kbrady481 (March 13th, 2013)

  22. #16
    Junior Member
    Join Date
    Mar 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java Program - Letter Frequencies - HELP!

    which methods should we call and how?

  23. #17
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Java Program - Letter Frequencies - HELP!

    If there are methods that print out the messages you want to see printed, the code needs to call those methods.

    Which methods print what you want to see printed?

    Have these problems been fixed?
    You shouldn't hide }s at the end of a statement. A } should be on a line by itself so it is easy to see.

    Statements within if statements should be enclosed with {} and be indented.
    If you don't understand my answer, don't ignore it, ask a question.

  24. #18
    Member
    Join Date
    Mar 2013
    Posts
    37
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Default Re: Java Program - Letter Frequencies - HELP!

    I am a true beginner with computer programming, so I am not sure exactly what you mean.

    public class LetterProfile
    {
      int scoreboard[] = new int [26];
     
     public void countChars (String s)
      {
        s.toLowerCase();
        char a = 'a';
        for (int i =0;i < s.length();i++)
        {
          int next = (int)s.charAt(i)-(int) a;
          if ( next<26 && next >= 0) //if the character is a-z
            scoreboard[next]++;
        }
     }
        public void scoreLine (String line)
        {
          int index = line.length();
          for (int j = 0; j<index; j++)
          {
            scoreboard[j]++;
            System.out.println(j + scoreboard[j]);
          }
        }
        public int largestLength()
        {
          int largest = 0;
          int largestindex = 0;
          for(int a = 0; a<26; a++)
          {
            if(scoreboard[a]>largest)
            largest = scoreboard[a];
            largestindex = a;
          }
          return(char)largestindex;
        }
        public void printResults()
        {
          largestLength();
          System.out.println(largestLength());
        }
     }

  25. #19
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Java Program - Letter Frequencies - HELP!

    Ok, I'll make it easier by asking one question at a time.

    Where in the code (what is the name of the method) is the println() method used?
    If you don't understand my answer, don't ignore it, ask a question.

  26. #20
    Member
    Join Date
    Mar 2013
    Posts
    37
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Default Re: Java Program - Letter Frequencies - HELP!

    The println() methods are used in the methods scoreLine and printResults

  27. #21
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Java Program - Letter Frequencies - HELP!

    Where is the scoreLine() method called?
    Where is the printResults() method called?
    If you don't understand my answer, don't ignore it, ask a question.

  28. #22
    Member
    Join Date
    Mar 2013
    Posts
    37
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Default Re: Java Program - Letter Frequencies - HELP!

    I'm not sure what you mean by that. Should these methods be called in the driver class?

  29. #23
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Java Program - Letter Frequencies - HELP!

    If you want the println() statements to execute and print something, the code must call the methods that have println() statements in them.

    Calling them in the driver class could be the way to do it.
    If you don't understand my answer, don't ignore it, ask a question.

  30. The Following User Says Thank You to Norm For This Useful Post:

    kbrady481 (March 13th, 2013)

  31. #24
    Member
    Join Date
    Mar 2013
    Posts
    37
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Default Re: Java Program - Letter Frequencies - HELP!

    I have no idea where to go from here, I am just going to get help from my teacher tomorrow but thank you for all your help!

  32. #25
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Java Program - Letter Frequencies - HELP!

    Time with your teacher should be helpful.

    New thread at: http://www.javaprogrammingforums.com...-anything.html
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. can i ask how can a letter with value can be recognize in a program??
    By w0ooop in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 20th, 2013, 09:26 AM
  2. letter pattern
    By severus1 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 4th, 2011, 01:24 PM
  3. Sentence and Letter Count Program
    By velop in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 10th, 2010, 12:10 AM
  4. help with the logic on this letter grade program.
    By etidd in forum Loops & Control Statements
    Replies: 2
    Last Post: January 28th, 2010, 09:14 PM
  5. letter to number
    By silverspoon34 in forum Java Theory & Questions
    Replies: 1
    Last Post: November 27th, 2009, 07:01 AM