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 2 of 2

Thread: Cant get the correct print out just once.

  1. #1
    Junior Member
    Join Date
    Nov 2013
    Location
    Greenville NC
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Cant get the correct print out just once.

    import java.util.*;
     
    public class LetCount
    {
     
      public static final int NUMCHARS = 26 + 1; //You define
     
      public static int addr(char ch)
      {
        return (int) ch - (int) 'A' + 1;
      }
     
     
      public static void main(String[] args)
      {
        Scanner keyboard = new Scanner(System.in);  // for reading input
         int[] count = new int [NUMCHARS]; 
         int curIndex = 0;
         int max = 0;
         System.out.println("Enter text to be read");
           while (keyboard.hasNext())
             {
             String str1 = keyboard.nextLine().toUpperCase();
             char ch = str1.charAt(curIndex);
              for(curIndex = 0; curIndex < str1.length(); curIndex++)
              {
              ch = str1.charAt(curIndex);
              if (ch >= 'A' && ch <= 'Z') {
              addr(ch);
              count[ch - 'A']++;
                            }         
     for (int i = 0; i < 26; ++i) {
               int highest; 
                    highest = count[i];
     
     
              System.out.printf(" %c ", i + 'A');
              System.out.println("occurred " + count[i] + " times");          
                if (highest >= max) {
                if (highest > max) {
                    max = highest;
     
     }
     
     
                }
     
            }
                  System.out.println(max);
        }
     
     
              }
     
     
                                      }
     
     
      }

    Other than maybe too many } why does this print out a statment for every letter?

    Im trying to take it and print out the count for each letter and say the highest and lowest of the counts


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Cant get the correct print out just once.

    One of your loops (maybe more than one) doesn't have an exit. Add some print statements to figure out which loop is misbehaving and fix it. I'm also not sure of the purpose of the while() loop. You might rethink whether that is required at all.

    Also, there's no detectable purpose for your code formatting. Each enclosed block of code should be indented one unit - whatever that is in your world - offset inside braces. Your braces are all over the place.

Similar Threads

  1. Replies: 1
    Last Post: December 3rd, 2012, 02:35 PM
  2. Getting collision correct..
    By Randor in forum What's Wrong With My Code?
    Replies: 0
    Last Post: November 6th, 2012, 02:49 PM
  3. xfa.host.print: print a page + some page range.
    By gammaman in forum Totally Off Topic
    Replies: 2
    Last Post: May 10th, 2012, 08:07 AM
  4. Can anybody correct the code?
    By ur2cdanger in forum What's Wrong With My Code?
    Replies: 8
    Last Post: October 24th, 2011, 12:50 AM
  5. Can anybody correct the code?
    By ur2cdanger in forum JDBC & Databases
    Replies: 1
    Last Post: October 17th, 2011, 07:07 PM