Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Program works, but I need it to print horizontally.

807599Dec 6 2006 — edited Dec 7 2006
The program works, except I have some minor conflicts.

I need all the letters of the Alphabet to be printed, even if the letter is not used in the sentence. A blank space should be provided.


Ex: A and D are seen twice in the sentence but B and C aren't so there is an empty space. THIS is what I need.

* *
* *
_ _ _ _
A B C D



Also the other problem is that the letters are printing Vertically, and I need it to print horizontally.

Any help would be greatly appreciated, and I am working with things as I post this. Thanks again.

Heres the Code:
import java.util.Scanner;

public class program5
{
   public static void main(String[] args)
   {
      int     i, j;
      String  Sentence, Response = "Y";
      int[]   Letters = new int[26];
      boolean Continue = true;
      Scanner keyboard = new Scanner(System.in);

      while (Continue)
         {
         for (j = 0; j < 26; j++)
            Letters[j] = 0;
         // end for

         System.out.println("Please enter a sentence not longer than 60 characters.");
         System.out.println("Do not go beyond this point ------------------------------>|");
         Sentence = keyboard.nextLine();
         if (Sentence.length() > 60)
            {
            System.out.println("Sentence is too long: Program Terminating!!!");
            System.exit(0);
            }
         // end if
         Sentence = Sentence.toUpperCase();
   
         for (j = 0; j < Sentence.length(); j++)
            {
            i = (int) Sentence.charAt(j) - 65;
            if (i >= 0 && i <= 25)
               Letters[i]++;
            // end if
            }
         // end for

         for (j = 0; j < 26; j++)
            if (Letters[j] != 0)
               {
               System.out.print((char)(j + 65) + ": ");
               for (i = 1; i <= Letters[j]; i++)
                  System.out.print('*');
               // end for
               System.out.println();
               }
            // end if
         // end for

         System.out.println("\n\nWould you like to go again? ");
         System.out.print("If so, enter 'Y'. If not, enter any other character: ");
         Response = keyboard.nextLine();
         Continue = (Response.equals("y") || Response.equals("Y"));
         }
      // end while
   }
   // end function main
}
// end class program5
 
Here is the output, and like I said I need it to be horizontal and all the letters of the alphabet printed with blanks when the letter is not used, and Asteriks when the letter is present in the sentence.

Output:

Please enter a sentence not longer than 60 characters.
Do not go beyond this point ------------------------------>|

I am not good at programming.

A: ***
D: *
G: ***
I: **
M: ***
N: **
O: ****
P: *
R: **
T: **


Would you like to go again?
If so, enter 'Y'. If not, enter any other character:
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 4 2007
Added on Dec 6 2006
21 comments
2,719 views