简体   繁体   中英

How to get rid of white spaces in the list of an array?

The code below is the class Hello which has the main method

import javax.swing.*;

public class Hello
{
    public static void main(String[] args)
    {
        int size, command;
        char inputChar;
        String inputString;
        //ask a user for an array size
        size = Integer.parseInt(JOptionPane.showInputDialog("Please enter a size for the array"));

        //instantiate a CharacterList Object
        CharacterList list1 = new CharacterList(size);

        //print the menu
        printMenu();

        do
        {
            //ask a user to choose a command
            command = Integer.parseInt(JOptionPane.showInputDialog("Please enter a command number, 1, 2, 3, 4, 5, 6, 7, or 8 (to quit)"));
            System.out.println("Entered command: " + command);
            switch(command)
            {
                case 1: //add a character
                    inputString = JOptionPane.showInputDialog("Please enter a character to add");
                    inputChar = inputString.charAt(0);
                    boolean added;

                    added = list1.addCharacter(inputChar);

                    if(added == true)
                    {
                        System.out.println(inputChar + " was added");
                    }
                    else
                    {
                        System.out.println(inputChar + " was not added");
                    }
                    break;
                case 2: //remove a character
                    inputString = JOptionPane.showInputDialog("Please enter a character to remove");
                    inputChar = inputString.charAt(0);
                    boolean removed;

                    removed = list1.removeCharacter(inputChar);

                    if(removed == true)
                    {
                        System.out.println(inputChar + " was removed");
                    }
                    else
                    {
                        System.out.println(inputChar + " was not removed");
                    }
                    break;
                case 3: //display the array
                    System.out.println(list1);
                    break;
                case 4: //compute and display the largest
                    inputChar = list1.findLargest();

                    if(inputChar == ' ')
                    {
                        System.out.println("\nThe list is empty");
                    }
                    else
                    {
                        System.out.println("\nThe largest character is: " + inputChar);
                    }
                    break;
                case 5: //compute and display the smallest
                    inputChar = list1.findSmallest();

                    if(inputChar == ' ')
                    {
                        System.out.println("\nThe list is empty");
                    }
                    else
                    {
                        System.out.println("\nThe smallest character is: " + inputChar);
                    }
                    break;
                case 6: //compute and display the sum of the unicode
                    System.out.println("\nThe sum of the unicode is: " + list1.computeSumOfUnicode());
                    break;
                case 7:
                    printMenu();
                    break;
                case 8:
                    break;


            }

        } while(command != 8);
    }

    public static void printMenu()
    {
        System.out.print("\nCommand Options\n" +
                "-----------------------------------\n" +
                "1: add a character in the array\n" +
                "2: remove a character from the array\n" +
                "3: display the array\n" +
                "4: compute and display the largest character\n" +
                "5: compute and display the smallest character\n" +
                "6: compute and display the sum of the unicode\n" +
                "7: display the menu again\n" +
                "8: quit this program\n\n");
    }
}

The second class CharacterList

import java.util.Arrays;

public class CharacterList {

    private char[] charArray;
    private int count;

    public CharacterList(int arraySize) {

        charArray = new char[arraySize];

        for (int i = 0; i < charArray.length; i++) {
            charArray[i] = ' ';
        }

        count = 0;

    }

    private void doubleArrayCapacity() {

        //create new array of char, which is double length
        char[] newCharArray = new char[this.charArray.length * 2];
        //prescribe values from old array to new one

        for (int i = 0; i < this.charArray.length; i++) {
            newCharArray[i] = this.charArray[i];
        }

        for (int i = this.charArray.length; i < newCharArray.length; i++) {
            newCharArray[i] = ' ';
        }

        //set newCharArray set new value of your field charArray
        this.charArray = newCharArray;
    }


    public int indexOf(char searchingChar) {

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

            if (charArray[i] == searchingChar) {
                return i;
            }

        }
        return -1;

    }

    public boolean addCharacter(char characterToAdd) {

        if (indexOf(characterToAdd) == -1) {

            if (count == charArray.length - 1) {

                doubleArrayCapacity();
            }

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

                if (this.charArray[i] == ' ') {

                    this.charArray[i] = characterToAdd;
                    break;
                }
            }
            count++;
            return true;

        } else
            return false;

    }

    public boolean removeCharacter(char characterToRemove) {

        if (indexOf(characterToRemove) != -1) {

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

                if (charArray[i] == characterToRemove) {

                    charArray[i] = charArray[charArray.length - 1];
                    charArray[charArray.length - 1] = ' ';
                }
            }
            count--;
            return true;

        } else
            return false;

    }

    public char findLargest() {

        char largest = charArray[0];

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

            if (charArray[i] > largest) {
                largest = charArray[i];
            }
        }
        return largest;
    }

    public char findSmallest() {

        char smallest = charArray[charArray.length - 1];

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

            if (charArray[i] < smallest) {
                smallest = charArray[i];
            }
        }
        return smallest;
    }

    public int computeSumOfUnicode() {

        int sum = 0;

        for (int i = 0; i < charArray.length; i++) {
            sum = sum + charArray[i];
        }
        return sum;
    }

    public String toString() {

        return Arrays.toString(charArray);

    }
}

I would like to add the characters which the user types in an array (size given by the user). And, if the size is not enough I want to double the size and add copy all the previous array elements in the new array and assign the old array to the new array (referencing).

The array which forms has white spaces which are made while doubling the length of the array. How do I get rid of those white spaces?

Output:
Command Options
-----------------------------------
1: add a character in the array
2: remove a character from the array
3: display the array
4: compute and display the largest character
5: compute and display the smallest character
6: compute and display the sum of the unicode
7: display the menu again
8: quit this program

Entered command: 1
a was added
Entered command: 1
y was added
Entered command: 1
L was added
Entered command: 1
p was added
Entered command: 1
a was not added
Entered command: 1
K was added
Entered command: 1
Y was added
Entered command: 1
S was added
Entered command: 3
[a, y, L, p, K, Y, S,  ,  ,  ,  ,  ]
Entered command: 4

The largest character is: y
Entered command: 5

The list is empty
Entered command: 6

The sum of the unicode is: 813
Entered command: 8

The spaces are disturbing the Unicode, largest, and smallest methods. Could someone help me regarding this

Thank you

The space character ' ' has a unique unicode value which will mess with the operations done on that. To get around this, I would recommend making your filler characters that with a unicode value of 0. For example, when your are populating charArray , populating with empty characters could be done with:

charArray[i] = '\u0000'; // Note that \u0000 can be replaced by the integer literal 0

This would mess with the calculation for the smallest character, however, because this filler character has the lowest unicode value. To get around that, make sure that the unicode value of the smallest character is not 0:

public char findSmallest() {

        char smallest = charArray[charArray.length - 1];

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

            if (charArray[i] < smallest && charArray[i] != 0) {
                smallest = charArray[i];
            }
        }
        return smallest;
    }

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