简体   繁体   English

Eclipse 不打印空字符

[英]Eclipse not printing null character

I'm writing a java program in Eclipse.我正在 Eclipse 中编写一个 java 程序。 Lets say I declare an array:假设我声明了一个数组:

char[] array = new char[5];

and then I initialize only few elements.然后我只初始化了几个元素。 When I try to print entire array, Eclipse stops when uninitialized element is reached, and does nothing.当我尝试打印整个数组时,Eclipse 在到达未初始化的元素时停止,并且什么也不做。 On the other hand, when I compile it in terminal using javac command, it works just fine, and prints blank spaces for uninitialized elements (or I should say null characters).另一方面,当我使用 javac 命令在终端中编译它时,它工作得很好,并为未初始化的元素打印空格(或者我应该说空字符)。 Why does this happen?为什么会发生这种情况?

EDIT: here is the full code (the program finds sub array taking only letters from the original one)编辑:这是完整的代码(程序发现子数组只取原始字母)

public static void main(String[] args) {
    // TODO Auto-generated method stub
    Scanner input=new Scanner(System.in);

    int n=4;
    char[] array=new char[n];
    System.out.println("Input array elements: ");
    for(int i=0; i<n; i++){
        array[i]=input.next().charAt(0);
    }
    char[] A=new char[n];
    int j=0;
    for(int i=0; i<array.length; i++){
        if ((array[i]>=65 && array[i]<=90)||(array[i]>=97 && array[i]<=122)){
            A[j]=array[i];
            j++;
        }

    }

    System.out.printf("subarray is A=[ ");
    for(int i=0; i<n; i++){
            System.out.printf(A[i]+" ");
    }
    System.out.printf("].");
}

for example if input is st1p, it outputs [ stp and stops there.例如,如果输入是 st1p,它会输出 [ stp 并在那里停止。 Doesn't execute last print.不执行最后一次打印。

Well that is because the default value of a char attribute is '\' (the null character) as stated in the Java Language Specification, section § 4.12.5 Initial Values of Variables .那是因为 char 属性的默认值是 '\'(空字符),如 Java Language Specification, section § 4.12.5 Initial Values of Variables 中所述。

I have modified and created a small project that converts a word into a string and prints it entirely.我修改并创建了一个小项目,该项目将一个单词转换为一个字符串并完全打印出来。 It does print numbers as well.它也打印数字。 I hope this helps you in what you are looking for.我希望这对您正在寻找的东西有所帮助。 Of course then you can create other char arrays and populate each second letter, third etc etc and get an n'th grand total char array with each substring from each word.当然,然后您可以创建其他字符数组并填充每个第二个字母、第三个等等,并使用每个单词的每个子字符串获得第 n 个总计字符数组。 Just like you did.就像你做的那样。

 public class stringToArray {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        System.out.println("Enter string: ");
        String word = input.next();

        int index = word.length(); // get index of word to create array of exact length
        char[] chars = new char[index]; // create the array of exact length

        System.out.print("The char array from the String is: ");
        for (int i = 0; i < chars.length; i++) {
            chars[i] = word.charAt(i); // populate the char array with the word, by breaking the word in chars 

            System.out.print(chars[i] + " "); // print it to check that it is working
        }

    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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