0

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

Edited by peter_budo: Please use code tags as [code]YOUR CODE HERE[/code]

5
Contributors
5
Replies
7
Views
6 Years
Discussion Span
Last Post by Ezzaral
0

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.

0

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

-1
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

Edited by Reverend Jim: Fixed formatting

0

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.