Hello, I have to write a program where I need to take an input from the user, a phrase with words seperated by spaces, and then print the phrase and show how many times a word is used in the phrase. I'm a beginner, I would like to know how everything works, so what I'm trying to do is at least understand how to take user input and manipulate in my program, so for now I just want to take input and print it. Here is my code:

import java.util.Scanner;
import java.lang.String;
import java.util.ArrayList;

public class wordlist
{
    public static void main(String[] args)
    {
        int USER_MAX = 50;
        System.out.print("Enter a phrase seperated by spaces: ");
        Scanner input = new Scanner(System.in);
        System.out.print('\n');
        String[] phrase = new String[USER_MAX];

         while (input.hasNextInt())
        {

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

                phrase[i] = input.next();

            }          


        }

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

            System.out.print(Arrays.toString(phrase));

        }

    }
}

When I compile, I get this error message:

C:\Users\Desktop\wordlist.java:31: error: cannot find symbol
            System.out.print(Arrays.toString(phrase));
                             ^
  symbol:   variable Arrays
  location: class wordlist
1 error
[Finished in 0.7s with exit code 1]

I don't know why. Is it because i have to use an Array List? I tried that and I don't know how it works. Thanks in advance for the help.

The fully-qualified name of that class is java.util.Arrays so either use the fully qualified name, OR (and this is what people usually do) import it at the beginning of your program just like you did with ArrayList or Scanner
import java.util.Arrays;

ps all the java.lang classes (eg String) are automatically imported, no need for you to import those.

After doing that, now I get this error:

C:\Users\Desktop\wordlist.java:23: error: no suitable method found for toString(String)
                System.out.print(Arrays.toString(phrase[i]));       
                                       ^
    method Arrays.toString(Object[]) is not applicable
      (actual argument String cannot be converted to Object[] by method invocation conversion)
    method Arrays.toString(double[]) is not applicable
      (actual argument String cannot be converted to double[] by method invocation conversion)
    method Arrays.toString(float[]) is not applicable
      (actual argument String cannot be converted to float[] by method invocation conversion)
    method Arrays.toString(boolean[]) is not applicable
      (actual argument String cannot be converted to boolean[] by method invocation conversion)
    method Arrays.toString(byte[]) is not applicable
      (actual argument String cannot be converted to byte[] by method invocation conversion)
    method Arrays.toString(char[]) is not applicable
      (actual argument String cannot be converted to char[] by method invocation conversion)
    method Arrays.toString(short[]) is not applicable
      (actual argument String cannot be converted to short[] by method invocation conversion)
    method Arrays.toString(int[]) is not applicable
      (actual argument String cannot be converted to int[] by method invocation conversion)
    method Arrays.toString(long[]) is not applicable
      (actual argument String cannot be converted to long[] by method invocation conversion)
    method Object.toString() is not applicable
      (actual and formal argument lists differ in length)
1 error
[Finished in 0.8s]

Arrays.toString takes a whole array in one go and converts it to a single string so you can print it without needing a loop. So instead of

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

you can just have

System.out.print(Arrays.toString(phrase));

Your latest code is an odd cross between the two, that isn't valid either way.

I dont know if this will help i split the text then loop through the splited word one after the other then compare

String temp="";
int count = 1;

String inputPhrase = input.nextLine(); //grad input phrase
String []splited = inputPhrase.split(" ");  //split

for ( int i=0; i<splited.length; i++)
{
    val = splited[i];

    //do your compare here 
    if(  val.compareToIgnoreCase(temp) == 0  )
    {
     count++;

    }               

    temp = val;   
}

input.close();

In the original post above, could you replace the loop at the end

    for(int i = 0; i < phrase.length; i++)
    {
        System.out.print(Arrays.toString(phrase));
    }

With this line?

    System.out.print(phrase.toString());

It will compile and run, but I am not sure if it is what you want the program to do.

Ok now it compiles and runs, so I enter a phrase, I tried "hello sir" but it only prints hello. I took out the loop and used System.out.print(phrase[i].toString()); so it would at least print the array and not the address

Ah now it prints whatever I entered when I kept the while loop, but does not show the spaces. I'm going to see if implementing any of the split compare will work

Member Avatar for iamthwee

Post your updated code.

All right, here is my updated code:

import java.util.Scanner;
import java.util.ArrayList;
import java.util.Arrays;

public class wordlist
{
    public static void main(String[] args)
    {
        int i = 0;
        String temp = " ";
        String val;
        int count = 1;
        int USER_MAX = 50;
        System.out.print("Enter a phrase seperated by spaces: ");
        Scanner input = new Scanner(System.in);
        System.out.print('\n');
        String[] phrase = new String[USER_MAX];
        String inputPhrase = input.nextLine(); 
        String[] splitted = inputPhrase.split(" ");


                while (input.hasNext())
                {
                    phrase[i] = input.next();

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

                        val = splitted[i];

                        if(val.compareToIgnoreCase(temp) == 0)
                        {
                                count++;
                        }               

                        temp = val; 
                    }

                        input.close();                  
                        System.out.print(phrase[i].toString());
                }     




    }

}

It compiles and runs, but will not do what I ask, I enter an input, but it won't print it

Member Avatar for iamthwee

What do you enter and what is the output...

Also what are you expecting from the output.

I enter hello world

and it should just print it out hello world

I forgot to say nothing prints out I hit enter when I type something in and it just goes to a new line

Ok I figured out what I needed to do to just take user input and print it my code works fine.

import java.util.Scanner;
import java.util.ArrayList;
import java.util.Arrays;


public class wordlist
{
    public static void main(String[] args)
    {
        int i = 0;
        int count = 0;
        int USER_MAX = 50;
        System.out.print("Enter a phrase seperated by spaces: ");
        Scanner input = new Scanner(System.in);
        System.out.print('\n');
        String[] phrase = new String[USER_MAX];
        phrase[i] = input.nextLine();
        System.out.print(phrase[i].toString());





    }

}

Now I just have a question, how would the best approach to find the number of times the words appear, would indexOf() work? Or not will with this?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.