1

I need to create a program that reads in a random word using a prompt.

The program needs to produce an output that is the average letter in the string.

If the average of the letters was 97.2 display a small a, but if the average of the letters was 97.5, display a small b.

I need to use type-casting and the charAt method that is part of the string class

This is all the information that I was given on what I have to do, and I am very confused. I don't have any code, because I don't even know where to start on this question. All help would be greatly appreciated.

Thank you! I really appreciate the feedback!

Here is my code post feedback:

public class Average_Of_String {



public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    String word;


    System.out.println("say something");
    word = scan.nextLine();

    float sum = 0;
    for(int i = 0; i < word.length(); i += 1) {
        sum += word.charAt(i);
    }

    System.out.println("Sum is " + sum + " and average is " + Math.round(sum/word.length()) + "(" + sum/word.length() + ")");
    int average = (int) (sum/word.length());
    System.out.println((char) average);
}

}
roschach
  • 8,390
  • 14
  • 74
  • 124
kali
  • 21
  • 4
  • For creating prompts you need the Scanner class. Using scanner once you get the input string, loop thorugh its characters while casting them to ints and take the average. After that, round the average and cast it to a char – mettleap Oct 20 '18 at 20:03
  • You should try to start doing something yourself. nobody will code for you. If you do not know where to start studying java and write a program to read words. – roschach Oct 20 '18 at 20:50
  • I never asked anybody to code for me, I was just asking them to help me to better understand the question, so that I could go out and get a start on it. @FrancescoBoi – kali Oct 20 '18 at 21:32
  • ok sorry! Also good for you! – roschach Oct 20 '18 at 21:40

2 Answers2

1

The charAt function returns a character. Ascii Table states:

an ASCII code is the numerical representation of a character such as 'a' or '@' or an action of some sort

On that site you can see that a equals decimal 97 etc.

Mark
  • 5,089
  • 2
  • 20
  • 31
  • Yes, I understand that, but I want to know how I can find the average that is equal to that 97, and what I need to use to find it. – kali Oct 20 '18 at 20:06
  • Once you have the numeric represenation of a character, you can do that for each character in a word, and take the average of all those numeric values – Mark Oct 20 '18 at 20:08
  • Another hint: `System.out.println((char) 97);` will print out `a`. – Mark Oct 20 '18 at 20:09
  • Thanks a lot! @Mark. This really helped! – kali Oct 20 '18 at 21:34
1

Try this, code is simple and self-explanatory:

class Average {
  public static void main(String[] args) {
    String word = args[0]; // let us suppose you get the word this way
    float sum = 0;
    for(int i = 0; i < word.length(); i += 1) {
      sum += word.charAt(i);
    }

    System.out.println("Sum is " + sum + " and average is " + Math.round(sum/word.length()) + "(" + sum/word.length() + ")");
  }
}
Mark
  • 5,994
  • 5
  • 42
  • 55