hi all,, i really don`t know what its wrong in my code ,,,
what i want its the user print the sentence and the program count eash sentence and devide it here is my code,,hope u help me :)

import javax.swing.*;
public class tracing {
	public static void main(String args[])
	{
		String s = JOptionPane.showInputDialog("Please enter a sentence");
		int length=s.length();
		int count = 0;
		
		while(s.charAt(count) != ' ' )
		{
			count++;
		}
		String word1;
		word1=s.substring(0, count-1);
		count++;
		while(s.charAt(count) != ' ' )
		{
			count++;
		}
		String word2;
		int i2=count;
		word2=s.substring(i2, count-1);
		count++;
		while(s.charAt(count) != ' ' )
		{
			count++;
		}
		String word3;
		int i3=count;
		word3=s.substring(i3, length-1);
		
		System.out.println(word1 +" + "+ word2 +" +"+ word3);

	}
	

}// end class

Recommended Answers

All 5 Replies

The problem is not clear.
Could you please post sample output?
Is this what your expected output is?
User enter - This is a sentence.
and output will be - 4.

there are 3 sentence and the program will get it from the user then it should count eash sentence by substying till blank
so,i wrote this code but there is something wrong i dont know it :S

what exactly is the code supposed to count? what exactly is it counting (according to you)?

import javax.swing.*;
public class tracing {
    public static void main(String args[])
    {
        String s = JOptionPane.showInputDialog("Please enter a sentence");
        int length=s.length();
        int count = 1;
        for(int i =0;i<s.length();i++)
        {
            if(s.charAt(i) == ' ')
            {
                count++;
            }
        }
        System.out.println(count);
    }
}// end class

The above code will not work.

It should also be noted that we don't simply write a solution to someone's homework here and all code should be nested in [code]

[/code] tags.

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.