计算字符数组中的字符 - JAVA

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19621926/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 18:53:09  来源:igfitidea点击:

Counting characters in a Character array - JAVA

javaarraysstringchar

提问by John Lapinski

First off i am pretty fresh when it comes to java. My program is supposed to ask the user for strings and print what they just put in. It is then supposed to change all characters to lowercase and remove all spaces from the strings and print this. After this, it is supposed to print a character array of the alphabet and use an asterisk (*) to show each time a character occurs in the string (I dont even know where to start here). Right now it just prints the String in an array(not correct). This is what I have so far. It will print either the string with no spaces or the original but not both. My object/array naming is atrocious and i apologize in advance. Any guidance would be greatly appreciated

首先,我对 Java 很新鲜。我的程序应该向用户询问字符串并打印他们刚刚输入的内容。然后应该将所有字符更改为小写并从字符串中删除所有空格并打印出来。在此之后,它应该打印字母表的字符数组并使用星号(*)来显示字符串中每次出现字符时(我什至不知道从哪里开始)。现在它只是在数组中打印字符串(不正确)。这是我到目前为止。它将打印没有空格的字符串或原始字符串,但不会同时打印两者。我的对象/数组命名很糟糕,我提前道歉。任何指导将不胜感激

EDIT: here is the question In this assignment you are to write a program that will do the following:

编辑:这是一个问题在这个作业中,你将编写一个程序来执行以下操作:

  1. Ask the user to input a positive integer m, and then read that integer. Use a Scanner object declared in main to read this integer.

  2. Call a method that will ask the user to input m strings. As the strings are read, they should be concatenated into a single string st. After reading the m strings and forming the single string st, the method should return st. NOTE: This method will have two parameters, an int to receive m and a Scanner object to receive the Scanner object declared in main.

  3. In main print the concatenated string received from the method.

  4. In main convert the String object to lower case.

  5. In main convert the lower case String object to an array of char. (All letters will be lower case.)

  6. In main print the character array just created. (Requires a looping structure.)

  7. Call a method that will compress the character array in the following way. The method will count the letters of the alphabet in the array, create a new array whose size is equal to that count, and copy only the letters of the original array into the new array. Return the new array.

  8. In main declare an integer array of size 26. Call a method with two parameters, a character array x (which will contain only lower case letters, and an integer array z that will receive the one declared in main). The method will set all entries in the integer array to zero. It will then process through the lower case letter array and count the number of times each letter occurs. HINT: z[x[i]-97]++ can do the counting. The ASCII code for a is 97, so if x[i] is ‘a', then z[0] will be incremented. ‘b' would cause z[1] to be incremented, etc. The integer array now contains a frequency distribution for the letters in the array of lowercase letters.

  9. Call a method with one integer array parameter (which will receive the frequency distribution array) and print each letter on a new line followed by the number of stars equal to the integer value in that array element. This must be neatly aligned. Hint: if i is an index with 0 ≤ ≤ 25, then (char)(i+97) is a lower case letter of the alphabet.

    package lab6;
    
    import java.util.Scanner;
    
    public class Lab6 {
    
        public char sent[];
    
    
    public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.println("enter the number of strings you want: ");
    int m = input.nextInt();
    Lab6 loo = new Lab6();
    loo.print(loo.loop(m));
    
    
    }
    
    public String loop(int m) { //print the string that was entered
    String total = " ";
    for (int i = 1; i <= m; i++) {
        Scanner input = new Scanner(System.in);
        System.out.println("enter string: " + i);
        String st = input.nextLine();
    
        total += st + "";
    
    }
    System.out.println(total);
    
    return total;
    
    }
    
    public void print(String ht) { //print array
    
    String st = ht.toLowerCase().replaceAll("\s", "");
    sent = st.toCharArray();
    for (int i = 0; i < sent.length; i++) {
        System.out.println(sent[i]);
    
            }
        }
    }
    
  1. 要求用户输入一个正整数 m,然后读取该整数。使用在 main 中声明的 Scanner 对象来读取这个整数。

  2. 调用一个方法,要求用户输入 m 个字符串。在读取字符串时,它们应该连接成一个字符串 st。在读取 m 个字符串并形成单个字符串 st 后,该方法应返回 st。注意:此方法将有两个参数,一个用于接收 m 的 int 和一个用于接收在 main 中声明的 Scanner 对象的 Scanner 对象。

  3. 在主打印中,从方法接收到的连接字符串。

  4. 在主要将 String 对象转换为小写。

  5. 在主要将小写字符串对象转换为字符数组。(所有字母均为小写。)

  6. 在主打印刚刚创建的字符数组。(需要循环结构。)

  7. 调用将按以下方式压缩字符数组的方法。该方法将计算数组中字母表的字母,创建一个大小等于该计数的新数组,并仅将原始数组的字母复制到新数组中。返回新数组。

  8. 在 main 中声明一个大小为 26 的整数数组。调用一个带有两个参数的方法,一个字符数组 x(将只包含小写字母,以及一个整数数组 z,它将接收在 main 中声明的那个)。该方法会将整数数组中的所有条目设置为零。然后它将处理小写字母数组并计算每个字母出现的次数。提示: z[x[i]-97]++ 可以进行计数。a 的 ASCII 码是 97,所以如果 x[i] 是 'a',那么 z[0] 将递增。'b' 会导致 z[1] 递增,等等。整数数组现在包含小写字母数组中字母的频率分布。

  9. 使用一个整数数组参数调用一个方法(它将接收频率分布数组),并在新行上打印每个字母,后跟等于该数组元素中整数值的星数。这必须整齐地对齐。提示:如果 i 是 0 ≤ ≤ 25 的索引,则 (char)(i+97) 是字母表中的小写字母。

    package lab6;
    
    import java.util.Scanner;
    
    public class Lab6 {
    
        public char sent[];
    
    
    public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.println("enter the number of strings you want: ");
    int m = input.nextInt();
    Lab6 loo = new Lab6();
    loo.print(loo.loop(m));
    
    
    }
    
    public String loop(int m) { //print the string that was entered
    String total = " ";
    for (int i = 1; i <= m; i++) {
        Scanner input = new Scanner(System.in);
        System.out.println("enter string: " + i);
        String st = input.nextLine();
    
        total += st + "";
    
    }
    System.out.println(total);
    
    return total;
    
    }
    
    public void print(String ht) { //print array
    
    String st = ht.toLowerCase().replaceAll("\s", "");
    sent = st.toCharArray();
    for (int i = 0; i < sent.length; i++) {
        System.out.println(sent[i]);
    
            }
        }
    }
    

采纳答案by Alexander

Sounds like computer science is not your cup of tea. I Strongly recommend you refactor this code and try to figure out why it does what it does.

听起来计算机科学不是你的菜。我强烈建议您重构此代码并尝试弄清楚它为什么会这样做。

public void print(String ht) { // print array

    String st = ht.toLowerCase().replaceAll("\s", "");
    sent = st.toCharArray();

    int[] alphabet = new int[26];
    //set all values to 0
    for(int i = 0 ; i < alphabet.length ; i++){
        alphabet[i] = 0;
    }
    //Loop through all characters and increment corresponding value
    for(int i = 0 ; i < sent.length ; i++){
        alphabet[sent[i] - 97]++;
    }
    //Print character + asterisk for each time the character is used
    for(int i = 0 ; i < alphabet.length ; i++){
        System.out.print((char)(i + 97) + ": ");
        for(int nChars = 0 ; nChars < alphabet[i] ; nChars++){
            System.out.print("*");
        }
        System.out.println();
    }

}