简体   繁体   中英

How do I make arrays interact with each other?

I'm having trouble with a school assignment:

Write a program to create two arrays namely asciiArray and decimalArray of 52 elements each. In the asciiArray, store letters of the English alphabet; both lower and upper case letters. In the decimalArray, store the corresponding decimal values of each of the letters in the specific position in the asciiArray. For example, If asciiArray[0] holds 'A' then decimalArray[0] will hold the value 65. Pass these arrays to a method displayDecValue. Inside the method, prompt the user to enter any of the letters of the English alphabet and display the corresponding decimal value.

I have some of the coding down, but I do not know how to make the arrays interact with each other and return the value. Sorry if this is simple; I have never done Java before. We also can't use anything advanced to write the code (it's beginner's Java).

import java.util.Scanner;

public class ParallelArrays {
    public static void main (String [] args) {
        char[] asciiArray = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
                              'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
                              'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
                              'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
                              'W', 'X', 'Y', 'Z' };
        int[] decimalArray = {97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,
                              114,115,116,117,118,119,120,121,122,65,66,67,68,69,70,71,72,73,74,
                              75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90};
        displayDecValue(asciiArray, decimalArray);
    }

    public static void displayDecValue(char [ ] ascii, int [ ] dec) {
        Scanner input = new Scanner(System.in);

        System.out.print("Enter a letter (a-z or A-Z): ");

        ascii = input.next().charAt(0);

        dec[0] = (int)ascii[0];

        System.out.printf("Decimal value of %c is: " + dec[0], ascii);
    }
}

Following are things which you should wonder

1) You are storing the input from the user into array variable ascii. What would happen here?

2) You are reassigning predefined dec array at index position 0, with ascii[0]. What does this mean?

3) How would you search for the char in the ascii array(Hint : loop) and match it with dec array?

4) Is the print statement following C or Java syntax?

Then you will be able to fix the code easily.

You no need to manually feed decimal array values. you can do something like below. if we convert or typecast char to int we will get decimal value of that character.

eg int output= (int)'a'; // here output is 97 that equivalent to decimal value of character 'a'.

    int[] decimalArray = new int[asciiArray.length];
    for (int i = 0; i < asciiArray.length; i++) {
        decimalArray[i] = (int)asciiArray[i];
    }

the complete example is below.

import java.util.Scanner;

public class ParallelArrays {
    public static void main(String[] args) {
        char[] asciiArray = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
                'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
                'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
                'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
                'U', 'V', 'W', 'X', 'Y', 'Z' };

        int[] decimalArray = new int[asciiArray.length];
        for (int i = 0; i < asciiArray.length; i++) {
            decimalArray[i] = (int)asciiArray[i];
        }
        displayDecValue(asciiArray, decimalArray);
    }

    public static void displayDecValue(char[] ascii, int[] dec) {
        Scanner input = new Scanner(System.in);

        System.out.print("Enter a letter (a-z or A-Z): ");

        char inputChar = input.next().charAt(0);

        for (int i = 0; i < ascii.length; i++) {

            if (inputChar == ascii[i]) {
                System.out.printf("Decimal value of '" + ascii[i] + "' is: "
                        + dec[0]);
                break;
            }

        }

    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM