3 Replies - 3765 Views - Last Post: 06 October 2009 - 12:55 PM Rate Topic: -----

#1 Catiepaige0821   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 06-October 09

Java Sentence Program

Posted 06 October 2009 - 12:15 PM

Resolved.

This post has been edited by Catiepaige0821: 07 October 2009 - 07:27 AM

Is This A Good Question/Topic? 0
  • +

Replies To: Java Sentence Program

#2 Tanira   User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 102
  • Joined: 30-May 09

Re: Java Sentence Program

Posted 06 October 2009 - 12:34 PM

View PostCatiepaige0821, on 6 Oct, 2009 - 11:15 AM, said:

Hi everyone,
I am very new to java. I just started teaching myself 2 weeks ago. I have been using the Java Foundations book to teach myself. I have been going through problems that I have found on the internet and try to write those programs. I have found one that has me absolutely stumped.

Write a java program that prompts the user for a sentence with 4 words

This user should enter this sentence with no punctuation and exactly one blank between words

Each of the following must be labeled in the output
1. Print the number of letters in the sentence
2. Print the average word length
3. Print each word on a separate line
4. Print the sentence with the order of the words reversed

Use String variables to store the value of each word in a sentence

So far I have started with the basics and everything I know how to do.

Import java.util.Scanner;

public class Sentence
{
 public static void man(String[] args) {
   Scanner scan = new Scanner(System.in);

   //



That's all I have. I am completely stumped.


Welcome to the Wonderful World of Java!

First off Import should be import
Second you need ending curly braces
man should be main

Methods are the most important part of java (arguably)
you might want to use a scan.next() to read the next input if its a string or scan.nextInt() if its an int
and a scan.next.length() to find the size of the next String
You can look up all the methods for specific types in the java docs

System.out.println("Text"); is the most common output for java
Was This Post Helpful? 1
  • +
  • -

#3 payamrastogi   User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 31
  • Joined: 30-July 08

Re: Java Sentence Program

Posted 06 October 2009 - 12:46 PM

This may be helpful to you...
you can modify the program according to yourself...
program to count no. of words, no. of lines, no. of senetences in a file

import java.io.*;
class Count
{
	private static int countLine;
	private static int countWord;
	private static int countSentence;
	private static int countSpace;
	
	public static void  countLine()
	{
		countLine++;
	}
	
	public static void countWord()
	{
		countWord++;
	}
	
	public static void countSentence()
	{
		countSentence++;
	}
	
	public static void setCountSpace()
	{
		countSpace = 0;
	}
	
	public static void countSpace()
	{
		countSpace++;
	}
	
	public static int getCountSpace()
	{
		return countSpace;
	}
	
	public static void show()
	{
		System.out.println("No. of words: "+countWord);
		System.out.println("No. of Lines: "+countLine);
		System.out.println("No. of Sentences: "+countSentence);
	}
	
	public static void main(String args[])throws IOException
	{
		if(args.length<1)
		{
			System.out.println("parameters missing");
			System.exit(0);
		}
		else if(args.length>1)
		{
			System.out.println("too many parameters");
			System.exit(0);
		}
		else
		{
			File f = new File(args[0]);
			if(!f.exists())
			{
				System.out.println("Source file does not exist");
				System.exit(0);
			}
			else
			{
				FileInputStream fis = new FileInputStream(f);
				BufferedInputStream bis  =  new BufferedInputStream(fis);
				//BufferedOutputStream bos =  new BufferedOutputStream()
				int ch;
				label1:while((ch=bis.read())!=-1)
				{
					if(ch=='\n')
					{
						Count.countLine();
						countSpace();
					}
					else if(ch == '.')
					{
						countSentence();
					}
					else if((ch==' ')||(ch>='a'&ch<='z')||(ch>='A'&ch<='Z'))
					{
						if(ch == ' ')
						{
							countSpace();
							continue label1;
						}
						else if((getCountSpace()>=1)&&(ch>='a'&ch<='z')||(ch>='A'&ch<='Z'))
						{
							setCountSpace();
							countWord();
						}
					}
				}
			}
			Count.show();
		}
	}
}


This post has been edited by payamrastogi: 06 October 2009 - 12:47 PM

Was This Post Helpful? 0
  • +
  • -

#4 Tanira   User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 102
  • Joined: 30-May 09

Re: Java Sentence Program

Posted 06 October 2009 - 12:55 PM

View Postpayamrastogi, on 6 Oct, 2009 - 11:46 AM, said:

This may be helpful to you...
you can modify the program according to yourself...
program to count no. of words, no. of lines, no. of senetences in a file

import java.io.*;
class Count
{
	private static int countLine;
	private static int countWord;
	private static int countSentence;
	private static int countSpace;
	
	public static void  countLine()
	{
		countLine++;
	}
	
	public static void countWord()
	{
		countWord++;
	}
	
	public static void countSentence()
	{
		countSentence++;
	}
	
	public static void setCountSpace()
	{
		countSpace = 0;
	}
	
	public static void countSpace()
	{
		countSpace++;
	}
	
	public static int getCountSpace()
	{
		return countSpace;
	}
	
	public static void show()
	{
		System.out.println("No. of words: "+countWord);
		System.out.println("No. of Lines: "+countLine);
		System.out.println("No. of Sentences: "+countSentence);
	}
	
	public static void main(String args[])throws IOException
	{
		if(args.length<1)
		{
			System.out.println("parameters missing");
			System.exit(0);
		}
		else if(args.length>1)
		{
			System.out.println("too many parameters");
			System.exit(0);
		}
		else
		{
			File f = new File(args[0]);
			if(!f.exists())
			{
				System.out.println("Source file does not exist");
				System.exit(0);
			}
			else
			{
				FileInputStream fis = new FileInputStream(f);
				BufferedInputStream bis  =  new BufferedInputStream(fis);
				//BufferedOutputStream bos =  new BufferedOutputStream()
				int ch;
				label1:while((ch=bis.read())!=-1)
				{
					if(ch=='\n')
					{
						Count.countLine();
						countSpace();
					}
					else if(ch == '.')
					{
						countSentence();
					}
					else if((ch==' ')||(ch>='a'&ch<='z')||(ch>='A'&ch<='Z'))
					{
						if(ch == ' ')
						{
							countSpace();
							continue label1;
						}
						else if((getCountSpace()>=1)&&(ch>='a'&ch<='z')||(ch>='A'&ch<='Z'))
						{
							setCountSpace();
							countWord();
						}
					}
				}
			}
			Count.show();
		}
	}
}



A period is a bad way to measure a sentence length. Else Mr., Mrs., Dr. etc Cir. St. Ave. would end sentences.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1