在迭代字符串时,在我的输出中获取Ljava.lang.String; @

时间:2016-03-03 15:06:21

标签: java string output

所以我必须为作业编写一个程序,为此我必须接受一个字符串,确保它有正确数量的句子并打印每个单词的频率。我几乎完全正确,但最后,当我打印单词(我已经存储在一个数组中)时,每个单词前面都有Ljava.lang.String; @xyznumber。我不知道为什么会这样,我无法在网上找到解决方案。这是我的代码:

import java.util.Arrays;
import java.io.*;

class frequency
{
    public static void main(String args[])throws IOException
    {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter the number of sentences");
        int cS = Integer.parseInt(br.readLine());
        System.out.println("Enter sentences");
        String s = br.readLine();
        int cS1 = 0;
        int cW = 0;

        for(int i = 0; i < s.length(); i++)
        {
            char ch = s.charAt(i);
            if (ch=='.'||ch=='?')
            {
                cW++;
                cS1++;
            }
            if (ch==' ')
            {
                cW++;
            }
        }

        if (cS1!=cS)
        {
            System.out.println("You have entered the wrong number of sentences. Please try again.");
        }
        else
        {
            int c = 0;
            int d = 0;
            String a[] = new String[cW];
            System.out.println("Total Number of words: "+cW);
            for (int i= 0;i<s.length();i++)
            {
                char ch=s.charAt(i);
                if (ch==' '||ch=='?'||ch=='.')
                {
                    a[c++]=a+s.substring(d,i);
                    d = i+1;
                }
            }

            int length=0;
            firstFor: for(int i=0;i<a.length;i++)
            {
                for(int j=0;j<i;j++)
                {
                    if (a[j].equalsIgnoreCase(a[i]))
                    {
                        continue firstFor;
                    }
                    else
                    {
                        length++;
                    }
                }
            }

            String words[] = new String[length];
            int counts[] = new int[length];
            int k=0;
            secondFor: for (int i =0;i<a.length;i++)
            {
                for(int j = 0; j<i;j++)
                {
                    if (a[j].equalsIgnoreCase(a[i]))
                    {
                        continue secondFor;
                    }

                }
                words[k]=a[i];
                int counter = 0;
                for (int j =0;j<a.length;j++)
                {
                     if(a[j].equalsIgnoreCase(a[i]))
                     {
                        counter++;
                     }
                }
                counts[k]=counter;
                k++;
            }
            for (int i=0;i<words.length;i++)
            {
                System.out.println(words[i]+"\n"+(counts[i]));
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

这个问题源于这一行:

a[c++]=a+s.substring(d,i);

由于a是一个String数组,所以这样做会将a中的一个元素指定为等于整个数组的String表示形式,再加上s的子字符串。但是,数组没有非常有用的String表示形式,这是您看到的Ljava.lang.String;@xyznumber的来源。

根据您对a[c]的第一部分的要求,使用数组中的索引,或convert the array to a String