-1

I need to create a program using the getAverage method and the main method to have the user input five chars and then calculate/print the average ASCII value and the highest letter, but Eclipse is giving me a lot of errors and I am not really sure what I am doing

   public static int getAverage(char [] ascii, int [] decimal, int [] letters) {
{       

    System.out.println("Enter 5 letters from the English Alphabet: ");
    Scanner input = new Scanner(System.in);

IM TRYING TO GET THE LETTER TO STORE AS AN ASCII NUMBER HERE

   System.out.println("Letter 1 (a-z or A-Z): ");
    char a = (input.next()).charAt(0);
        int letterOne = -1;
        for(int i=0; i<ascii.length; i++){
            if (ascii[i] == a)
                letterOne=i;
        }
    System.out.println("Letter 2 (a-z or A-Z): ");
    char b = (input.next()).charAt(0);
        int letterTwo = -1;
        for(int i=0; i<ascii.length; i++){
            if (ascii[i] == b)
                letterTwo=i;
        }
    System.out.println("Letter 3 (a-z or A-Z): ");
    char c = (input.next()).charAt(0);
        int letterThree = -1;
        for(int i=0; i<ascii.length; i++){
            if (ascii[i] == c)
                letterThree=i;
    }
    System.out.println("Letter 4 (a-z or A-Z): ");
    char d = (input.next()).charAt(0);
        int letterFour = -1;
        for(int i=0; i<ascii.length; i++){
            if (ascii[i] == d)
                letterFour=i;
    }
    System.out.println("Letter 5 (a-z or A-Z): ");
    char e = (input.next()).charAt(0);
        int letterFive = -1;
            for(int i=0; i<ascii.length; i++){
                if (ascii[i] == e)
                    letterFive=i;
    }

I DONT KNOW HOW TO MAKE THESE INTO AN ARRAY THAT I CAN USE IN THE MAIN

    int[] letter = new int{letterOne, letterTwo, letterThree, letterFour, letterFive}; 

    int [] lettersArray = {a, b, c, d, e};
    int average = ((a+b+c+d+e)/5);


    System.out.println("Your average value is: " + average);

NOT SURE WHAT TO RETURN HERE IF ANYTHING?

     return ;

}


public static void main(String[] args)
{
    int [] decimalArray = new int[52];
    char[] asciiArray = new char[52];

    int base = 65;

    for (int i=0; i<26;i++){
        decimalArray[i] = base;
        asciiArray[i] = (char) base;
        base++;
    }
    base = 97;
    for(int i = 26; i<52; i++){
        decimalArray[i] = base;
        asciiArray[i] = (char) base;
        base++;
    }

    int [] lettersArray = new int[5];

NOT SURE HOW TO CHANGE FROM A CHAR TO AN INT HERE

    int[] letters = new int[5];
    char max = letters[0]
            for(int i = 0; i<5; i++){
                if(max < letters[i])
                    max = letters[i];
            }

    getAverage(asciiArray, decimalArray, lettersArray);

    System.out.println("The highest letter is: " + max);


}



}
  • Let's start with the syntax errors: (1) Do you have something at the top that says "public class SomeName {"? (2) You have an extra `{` after the start of `getAverage`. That will cause a lot of errors right away. (3) `int letter[] = new int[] { letterOne, ...`. You need square brackets after `int`. Use the same syntax for `lettersArray`. (4) `getAverage` was declared as returning an `int`, so you need to put some integer variable or expression after `return`. (5) `char max = letters[0]` needs a `;`. That won't fix all the problems but get you closer to compiling. – ajb Oct 12 '13 at 02:08

2 Answers2

0

1: Ascii value.

In Java, a char holds an ascii, aka: numeric, aka: integer, value, however, when you try to print it, the character represented by that value will be shown.

    char a = 97;
    System.out.println( a ); // output: a

    char avg = ('a' + 'c') / 2;
    System.out.println( avg ); // output: b

    int b = (int)avg;
    System.out.println( b ); // output: 98

2: I don't know how to make an array to use in main

The main method is static, this means it exists without an instance of the class being created first and may be invoked at any time. The array you've declared is not static and main may not refer to the array because the array doesn't exist until an instance of the class has been created. Make the array static (at your own peril), or use main to create a new instance of your class and then tell the instance what to do.

3: Not sure what to return here.

You said up in your method declaration that you would return an int, you damn well better, or Java will get angry.

4: Not sure how to cast a char to an int.

Review my code in the first part of this post.

Ray Stojonic
  • 1,250
  • 1
  • 7
  • 11
0

Unless you require accessing the values the user entered previously, then you don't necessarily need an array. You could do it just like this:

import java.util.Scanner;

class ASCII_Average_Calculator {
  public static void main(String []args){
    int n_letters = 5;//the amount of letters you want to capture

    int sum = 0;
    int ascii = 0;//this will hold the ascii value
    double average = 0;

    String letters = new String();

    for (int i = 0; i < n_letters; ++i) {
      System.out.println("Letter " + (i+1) + " (a-z or A-Z):");

      Scanner input = new Scanner(System.in);
      char c = input.next().charAt(0);//see this link for more: https://stackoverflow.com/questions/13942701/take-a-char-input-from-the-scanner

      ascii = (int)c;//cast the character to an int

      //perform comparisons to see if it is not within the limits for
      //lowercase ascii values: (97 to 122)
      //uppercase ascii values: (65 to 90)
      //and ensure that the value captured is within those limits (i.e. valid for a-z and A-Z)
      while ((!(ascii > 96) && (ascii <= 96 + 26)) ||
          (!(ascii > 64) && (ascii <= 64 + 26))) {
        System.out.println("Invalid entry: \"" + c + "\"! Please try again.");
        System.out.println("Letter " + (i+1) + " (a-z or A-Z):");
        input = new Scanner(System.in);
        c = input.next().charAt(0);
        ascii = (int)c;
      }

      sum += ascii;//increment the sum

      if (i == n_letters - 1) {
        letters += c;
      }
      else {
        letters += c + " , ";
      }
    }

    //finally, compute the average
    average = sum/(double)n_letters;

    System.out.println("The average of the letters: \"" + letters + "\" is " + average);
  }
}

SAMPLE INPUT:

Letter 1 (A-z or A-Z):
a
Letter 2 (A-z or A-Z):
v
Letter 3 (A-z or A-Z):
b
Letter 4 (A-z or A-Z):
x
Letter 5 (A-z or A-Z):
e

OUTPUT:

The average of the letters: "a , v , b , x , e" is 106.8

REFERENCES:

Take a char input from the Scanner

http://www.asciitable.com/

Community
  • 1
  • 1
jrd1
  • 9,026
  • 4
  • 30
  • 48