I have an assignment in which I have to read in a series of words using the nextLine method from the Scanner class and then count the number of words using the StringTokenizer class. I know you all like for people to show some initiative by starting the program but I am not sure what needs to be done. I have no experience whatsoever using the StringTokenizer class so I was hoping someone could explain to me how I could accomplish this task.

Thanks

Since your assignment specifies it, do you not have any class notes related to StringTokenizer? Seems strange to me that you have an assignment on things that were not covered in class at all.

StringTokenizer is also a legacy class and it's usage in new code is discouraged:

StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead.

I agree, it is strange. We have a visiting professor from another country and he is, to put it nicely, lacking in teaching ability in American colleges. And although it's use is discouraged he has specified that as the method to accomplish this assignment. The philosophy around here is teach everything the hard, old way and then after that teach the newer, simpler way of doing things.

Hope this will help you get started....

import java.util.Scanner;
import java.util.StringTokenizer;

public class Test 
{
	public static void main(String[] args)
	{
		String input;
		Scanner s = new Scanner(System.in);

		int count = 0;
		
		System.out.print("Enter a sentence: ");
		input = s.nextLine();
		
		StringTokenizer t = new StringTokenizer(input);
		
		while(t.hasMoreTokens())
		{
			count++;
			String word = t.nextToken();
			System.out.println("Found the word " + word);
		}
		
		System.out.println("Number of words: " + count);
	}
}

Thank you very much, that helps a lot. Now all I have to do is change it to nextLine scans for the input and then try to understand how the tokenizer works.

hexcode, while you obviously mean to help, please keep in mind that writing a student's entire assignment for them teaches nothing and is discouraged by the forum rules.

hexcode, while you obviously mean to help, please keep in mind that writing a student's entire assignment for them teaches nothing and is discouraged by the forum rules.

Fortunately I am mature enough to not just blindly copy and paste code and then turn it in. I will take what h3xc0de helped me start, make changes to it so that it complies with all aspects of the assignment specifications, and the experiment with it so that I can gain an understanding of the StringTokenizer method. Real code that you can read and manipulate to see how it works teaches much better than single lines of code or vague responses that don't really answer the questions asked because the poster doesn't want to provide "too much" information.

Real code that you can read and manipulate to see how it works teaches much better than single lines of code or vague responses that don't really answer the questions asked because the poster doesn't want to provide "too much" information.

I did not discourage "real code". I discouraged posting a complete working version of the assignment. The two are not the same.

Those "vague responses" may or may not be so vague depending upon how much effort and searching has gone into finding a solution. They may "really answer the question" if the student reads the referenced portions of the API documents and thinks about how the methods would apply to their situation.

I did not discourage "real code". I discouraged posting a complete working version of the assignment. The two are not the same.

Those "vague responses" may or may not be so vague depending upon how much effort and searching has gone into finding a solution. They may "really answer the question" if the student reads the referenced portions of the API documents and thinks about how the methods would apply to their situation.

I certainly understand that, and technically for me this was not a complete working version of the assignment. The assignment has to handle reading in multiple lines of text using nextLine. The code posted on the forum only read and counted words for one sentence.

In my view the code provided is basically the same as textbooks or professors providing syntax code. It is a way for people who learn better by example to learn the material.

By the way Ezzaral, I apologize if my previous post seemed offensive or antagonistic. I was just trying to explain my position but after further examination it donned on me that it might have seemed a bit coarse.

By the way Ezzaral, I apologize if my previous post seemed offensive or antagonistic. I was just trying to explain my position but after further examination it donned on me that it might have seemed a bit coarse.

Your post was not antagonistic, but if you spend much time here at all you will quickly see that we encounter many students who just want the code for their assignment handed to them and do not want to put any effort into learning the material. That is quite clearly not the intent of these forums. Some answers may be more vague than a student would like, but learning programming is a process of learning how to think things through in certain ways and learning how to use the elements of the language to craft a solution. Handing someone a completely working program does not invoke the same understanding of a concept as does providing enough guidance for the student to figure it out for themselves.

I did acknowledge that I understood hexcode was merely being helpful, but since he is a new member I did want to let him know that posting completed assignments is somewhat frowned upon.

hope this will help you get started....

import java.util.scanner;
import java.util.stringtokenizer;

public class test 
{
	public static void main(string[] args)
	{
		string input;
		scanner s = new scanner(system.in);

		int count = 0;
		
		system.out.print("enter a sentence: ");
		input = s.nextline();
		
		stringtokenizer t = new stringtokenizer(input);
		
		while(t.hasmoretokens())
		{
			count++;
			string word = t.nexttoken();
			system.out.println("found the word " + word);
		}
		
		system.out.println("number of words: " + count);
	}
}

here how can we find the number of each words?? Can you help me

Use this logic. A word is any sequence of characters delimited by a space....

import java.util.Scanner;
public class words{
	public static void main(String[] args)
	{Scanner in=new Scanner(System.in);
	System.out.println("Enter string:");
	String str=in.nextLine();
	int count=0;
String str1="";
if(str.equals(str1))
System.out.println("No. of words are 0");
else{ for(int i=0;i<str.length();i++)
	{
		if(i!=str.length()-1 && str.charAt(i)!=' ' && str.charAt(i+1)==' ')
		count++;
		}

	if(str.charAt(str.length()-1)==' ')
		count--;
	if(count>0)
	System.out.println("No. of words are "+(count+1));
	if(count==0)
		System.out.println("No. of words are "+(count+1));}
	}
}
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.