简体   繁体   English

打印字符串数组也打印为null

[英]Printing string array also prints null

When I am printing contents of a string array the output is printing 'null' as well. 当我打印字符串数组的内容时,输出也会打印'null'。 I am not sure what's wrong with this. 我不确定这有什么问题。 Here is the output, what I expect is the output without 'null' 这是输出,我期望的是没有'null'的输出

null(wa?>=0)nullnull*(wo?>=0)nullnull*(4*wa?+7*wo?>=50)nullnull*(d1=10)nullnull*((d2=1)+(k=2))nullnull

Thanks and appreciate your help. 谢谢,感谢您的帮助。 I would say my skill in Java in beginner level and I started two weeks back. 我会在初学者水平上说我的Java技能,并且我在两周后开始了。

Here is the actual code: 这是实际的代码:

        String[] arrStr = new String[50];
        int countStr = 0;

        for (int i = 0; i < para.length; i++) {
            if (para[i] == '(') {
                count = count + 1;
            }
            if (para[i] == ')') {
                count = count - 1;
            }
            if (count > 0) {
                arrStr[countStr] = arrStr[countStr] + para[i];
            } else {
                if (para[i] == ')') {
                    arrStr[countStr] = arrStr[countStr] + para[i];
                    countStr += 1;
                } else {
                    countStr += 1;
                    arrStr[countStr] = arrStr[countStr] + para[i];
                    // System.out.println(para[i]);
                }
            }
        }
        System.out.println(countStr);

        for (int i = 0; i < countStr; i++) {
            System.out.print(arrStr[i]);
        }

Before this part, I am reading the following string from a word document: 在此部分之前,我正在从word文档中读取以下字符串:

(wa?>=0)AND(wo?>=0)AND(4*wa?+7*wo?>=50)AND(d1=10)AND((d2=1)+(k=2))

I think the problem may be due to the line: 我认为问题可能是由于这条线:

arrStr[countStr] = arrStr[countStr] + para[i]; 

Since arrStr[countStr] is null initially and I add an element to it, it saves it as null+para[i] . 由于arrStr[countStr]最初为null并且我向其添加了一个元素,因此将其保存为null+para[i] Do you think it is possible? 你认为有可能吗?

Like when I try: System.out.println(arrStr[0]); 就像我尝试的那样: System.out.println(arrStr[0]); I get the output as 我把输出作为

null(wa?>=0)
System.out.println(null + "Bla");

prints nullBla . 打印nullBla A null represented as String is "null". 表示为String的null为“null”。 The issue here is that initially all your String[] is made of null. 这里的问题是,最初所有的String []都是由null组成的。 You need to initialize them first. 您需要先将它们初始化。 Typically, 通常情况下,

Arrays.fill(arrStr, "");

Should do. 应该做。

If any element in arrStr is null while printing, it will print that faithfully. 如果arrStr任何元素在arrStr为null,它将忠实地打印出来。

Without knowing specifics in your code, you can either ensure that no element in your array becomes null (ensure that the lengths match up and you're not skipping element in arrStr , or check for null before printing the element. 在不知道代码中的细节的情况下,您可以确保数组中的任何元素都不为null(确保长度匹配,并且您不是在arrStr跳过元素,或者在打印元素之前检查null

You don't initialize the values of the arrStr array, you just allocate size for it, so each element of the array is null. 您没有初始化arrStr数组的值,只是为它分配大小,因此数组的每个元素都为null。 When you assign a value to an element of the array, you concatenate its value (which is null) with the value of para[i] . 为数组的元素赋值时,将其值(null)与para[i]的值连接起来。

You should initialize your array before using it: 您应该在使用之前初始化数组:

String[] arrStr = new String[50];
for (int i = 0; i < arrStr.length; i++) {
    arrStr[i] = "";
}

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

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