I am trying to write a program that reads an inputted sentence and takes each word an puts them on a separate line. Then the program needs to count the number of words. I really do suck at this and am only still in this class because I have to take continue to be a Math major. Any help is greatly appreciated :)

here is it so far. The compiler is telling me it expects ")" in the last line. I have no idea why. Again...I suck at this.

import java.util.Scanner;
        
public class Sentence_count
{   
    public static void main (String [] args)
    {
        Scanner keyboard = new Scanner (System.in);
        System.out.println ("Enter a single line of text with words separated by a single space");
        String text = keyboard.next();
        int numwords = 0;
        int index = text.indexOf(' ');
        while (index != 1);
        {
            numwords++;
            String word = text.substring (0, index);
            System.out.println (word);
            text = text.substring (index + 1).trim();
            index = text.indexOf(' ');
        }
        System.out.println (+text);
        System.out.println ("There are " +numwords " in your sentence.");
    }
}
System.out.println (+text);

+ is a concatenation operator here. You use a + to concatenate two separate things that you want to print, so you should always have something on each side of the + sign. You only have one item to print, so lose the + sign.

System.out.println (text);
System.out.println ("There are " +numwords " in your sentence.");

Here you are concatenating three things so you need a + sign between each of them. You have no + sign between numwords and the second quoted string. You need one.

System.out.println ("There are " +numwords + " in your sentence.");

Also when you try to print the sentence in the end:
System.out.println (+text) (remove the '+' of course), the text variable has been modified in the while loop. So you will not print the original sentence. Save the input to one more variable and print that one in the end

Try using the StringTokenizer class. It will automatically break the string into tokens or substrings based on space as the default delimiter. And then u can traverse through the StringTokenizer object to list all the substrings.

yes follow sanaulla123's instructions and you shudnt have a problem.. its a fairly simple program, carry on from here..

import java.util.Scanner;

public class Sentence_count{
	public static void main(String args[]){
		Scanner keyboard = new Scanner (System.in);
		System.out.println ("Enter a single line of text with words separated by a single space");

		String text = keyboard.next();
		int numwords = 0;
		int index = text.indexOf(' ');
		while (index != 1){
			numwords++;
			String word = text.substring (0, index);
			System.out.println (word);
			text = text.substring (index + 1).trim();
			index = text.indexOf(' ');
		}
		System.out.println (text);
		System.out.println ("There are " + numwords +" in your sentence.");
	}
}
This article has been dead for over six months. Start a new discussion instead.