hey i need some help with my assignment am a seriose beginner

i need to create a program using arrays that prompts the user to write
a sentence and then prints outs the word and the number of times its appearing..i don't know where to begin

please help :(

Recommended Answers

All 4 Replies

Break the problem up into separate steps and do one step at a time.
For example: prompt for a sentence and read in the sentence and print it out.
See the Scanner class for a way to read in a sentence. Many code samples on this forum if you use Search.

  1. import java.util.Scanner;
  2. public class WordCount
  3. {
  4. public static void main (String [] args)
  5. {
  6. Scaner input = new Scanner (System.in);
  7. int numWords=0;
  8. System.out.print("Enter a sentence: ");
  9. String userString = input.nextLine();
  10. userString = userString.substring(0, userString.length()-1);
  11. for (int i = 0; i < userString.length(); i++)

  12. }
  13. }
  14. }

Can you explain what that code is supposed to do?
That's normally done by adding comments to the code.

What is the next step in your assignment?

public class LetterCount
 {
  public static void main (String [] args)
   {
  int freq = 0; // frequency of charactersdeclared and initialised to zero

  String lineTwo ; //string called lineTwo initialised.

  System.out.println("Enter a string of text;\n"); // prompt user for the a string of text.
  lineTwo = input.nextLine();	                   // read text from user.

  System.out.println ("Enter letter");             // prompt user to enter a letter
                                                   // in which they would like to find                    
                                                   // out  how many times its appearing.
  String letter = input.nextLine();                // get line of text from user

  for (int i = 0; i<lineTwo.length(); i++)    //iterate through the line of text up                                                                       the length of the text entered.
   {
 
    if (lineTwo.charAt(i) == letter.charAt(0))     // compare the character at i 
                                                   // from the first line and character that the user inputs
     {
      freq++; // increment the frquency by 1 if the comparison in the of statement is equal.
     }
   

    if ((lineTwo.length()-1) == i) // compare
      {
       System.out.printf("%s \t %d", letter, freq ); // print the letter and how many times its appearing.
       System.out.printf("\n\n"); // print two lines.
      }
   }
  }
 }

i managed to write the code buh i used string.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.