1

so I have to write a program for an assignment, and for that i have to accept a string, make sure it's got the right number of sentences and print the frequency of each word. I've got this almost completely right, but at the end, when I print the words (which I've stored in an array), each word is preceeded by Ljava.lang.String; @xyznumber. I have no idea why this is happening and I can't find a solution on the net. Here's my code:

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]));
            }
        }
    }
}
wottle
  • 13,095
  • 4
  • 27
  • 68
Vaibhav
  • 15
  • 2
  • How sure are you of this statement `a[c++]=a+s.substring(d,i);` Do you want `a.substring` or `a[someindex].substring`? – bradimus Mar 03 '16 at 15:10
  • 1
    Also, if you are using labels in a for-loop, there is probably a better way – bradimus Mar 03 '16 at 15:10
  • This code is convoluted; simplifying your methodology might help. Go through and explain in english what each step should do--when you get stuck, you know where the problem is. – D. Ben Knoble Mar 03 '16 at 15:13

1 Answers1

0

The problem stems from this line here:

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

Since a is a String array, what this does is assigns one of the elements in a to be equal to the String representation of the entire array, plus a substring of s. Arrays don't have a very useful String representation though, which is where the Ljava.lang.String;@xyznumber you see is coming from.

Depending on what you want the first part of a[c] to be, either use an index into the array, or convert the array to a String

Community
  • 1
  • 1
resueman
  • 10,572
  • 10
  • 31
  • 45
  • Thanks, so that was the mistake I made. Changed it to a[c++]=s.substring(d,i); and it's fine now – Vaibhav Mar 03 '16 at 15:47