Home > database >  I have to write a for loop to print the ASCII values of chars which are in an array
I have to write a for loop to print the ASCII values of chars which are in an array

Time:09-30

This is what I have so far. I'm unsure how to print the individual value of each char in ascii without the values repeating on every line. Any help would be appreciated.

Create a for loop to print out all of the ASCII values associated with EACH
character of the town name or pet name (e.g., R = 125, a = 97 etc.)

import java.util.Arrays;

public class Lab1Part2{

    public static void main (String[] args){

    char B = 'B';
    char l = 'l';
    char a = 'a';
    char n  ='n';
    char h = 'h';
    int ascii = B;

        //create array
        char[] JavaCharArray = new char[6];
        JavaCharArray[0] = 'B';
        JavaCharArray[1] = 'l';
        JavaCharArray[2] = 'a';
        JavaCharArray[3] = 'n';
        JavaCharArray[4] = 'c';
        JavaCharArray[5] = 'h';

        //print array
        for(int i=0;i<JavaCharArray.length;i  ){
            System.out.println(JavaCharArray[i]   " = "   ascii);
             }



         }
        }

CodePudding user response:

The char type in Java is legacy, essentially broken. As a 16-bit value, it is incapable of representing most characters.

Code points

Instead, learn to use code point integer numbers to work with individual characters.

int[] codePoints = "Blanch           
  • Related