简体   繁体   中英

How do I print out an array?

I'm almost done with my practice except that I can't seem to figure out on how to display the array. The practice is to take a group of strings from the user with the length of 10 and check if there's a number 0-9 in the string that corresponds to the array with the length of 10.

This is what I have so far.

import java.util.*;

public class CharacterFrequency {

public static void main(String[] args) {
    Scanner keyboard = new Scanner(System.in);
    int telNumber[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};

    System.out.print("Give me a word of 10 characters. "
            + "\nI will tell you if it contains any numbers, and how many: ");
    String word = keyboard.nextLine();
    int length = word.length();
    if (length < 10 || length > 10) {
        System.out.println("Wrong length, you have entered an invalid word.");
        System.exit(0);
    } else {


        for (int index = 0; index < word.length() - 1; index++) {
            char ch = word.charAt(index);
            if ('0' == ch) {
                telNumber[0]++;
            } else if ('1' == ch) {
                telNumber[1]++;
            } else if ('2' == ch){
                telNumber[2]++;
            } else if ('3' == ch){
                telNumber[3]++;
        }   else if ('4' == ch){
                telNumber[4]++;
        }else if ('5' == ch){
                telNumber[5]++;
        }else if ('6' == ch){
                telNumber[6]++;
        } else if ('7' == ch){
                telNumber[7]++;
        } else if ('8' == ch){
                telNumber[8]++;
        }else if ('9' == ch){
                telNumber[9]++;
        } else {
        System.out.println("\nWrong length, you have entered an invalid word.");
        System.exit(0);
    }
        }
        System.out.println();

    }
}
}

I can't figure out how to print out the output looking like this:

Count of 0 is (number of 0 in the string)

Count of 1 is (number of 1 in the string)

Count of 2 is (number of 2 in the string)

Count of 3 is (number of 3 in the string)

Count of 4 is (number of 4 in the string) etc.

The easiest way to print an int array in general is probably Arrays.toString(int[])

System.out.println(Arrays.toString(telNumber));

Your declaration of telNumber should look more like,

int telNumber[] = new int[10]; // {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};

your declaration gives every element an initial count equal to it's index (I'm fairly certain you wanted them to be zero). Then to get the format like you've asked for (with printf() and a for loop) might look something like

for (int i = 0; i < telNumber.length; i++) {
  System.out.printf("Count of %d is %d%n", i, telNumber[i]);
}

Finally, your long if else chain could be significantly shortened using Character.digit(char, int) like

if (ch >= '0' && ch <= '9') {
    telNumber[Character.digit(ch, 10)]++;
} else {
    System.out.println("\nWrong length, you have entered an invalid word.");
    System.exit(0);
}

Few things to change :

1) telNumber array initialization, another option is :

int telNumber[] = new int[10];

2) avoid all the 10 if's with one assignment based on the ascii value of the char value, using :

telNumber[ch - '0'] ++;

for more reference : ascii table

3) iterate through the array and print it

for (int i = 0; i < telNumber.length; i++) {
                  System.out.printf("Count of %d is %d%n", i, telNumber[i]);
                }

4) fixing the loop to :

for (int index = 0; index < word.length() ; index++)

because in your code it doesnt count the last char in your input.

fixed code:

Scanner keyboard = new Scanner(System.in);
        int telNumber[] = { 0,0,0,0,0,0,0,0,0,0};

        System.out
                .print("Give me a word of 10 characters. "
                        + "\nI will tell you if it contains any numbers, and how many: ");
        String word = keyboard.nextLine();
        int length = word.length();
        if (length < 10 || length > 10) {
            System.out
                    .println("Wrong length, you have entered an invalid word.");
            System.exit(0);
        } else {

            for (int index = 0; index < word.length() ; index++) {
                char ch = word.charAt(index);
                if (ch >= '0' && ch <= '9') {
                telNumber[ch - '0'] ++;
                }
                else {
                  System.out.println("\nWrong length, you have entered an invalid word.");
                  System.exit(0);
                }
            }
            System.out.println();

        }
        for (int i = 0; i < telNumber.length; i++) {
              System.out.printf("Count of %d is %d%n", i, telNumber[i]);
            }
    }
public static void printIntArray(int[] array) {
    System.out.print("{");

    for (int i = 0; i < array.length - 1; i++)
        System.out.print (array[i] + ", ");

    System.out.println(array[array.length - 1] + "}");
}

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