The program should tell user how many vowels occured on each word in a given string. The string was "Once upon a midnight dreary, while I pondered weak and weary."

The output generated by the codes below was:

Enter string(s): Once upon a midnight dreary, while I pondered weak and weary.
Once - 1
upon - 2
a - 0
midnight - 3
dreary - 2
while - 2
I - 0
pondered - 4
weak - 2
and - 1
weary - 2

some are correct, some are not.

import java.util.*;
import java.io.*;
public class part{
	public static void main (String[]args) throws IOException{
		BufferedReader br=new BufferedReader(new InputStreamReader (System.in));
		String sinput,sloop;
		System.out.print ("Enter string(s): ");
		sinput=br.readLine();
			StringTokenizer soutput=new StringTokenizer(sinput,".,,,!,?, ");
		while (soutput.hasMoreTokens()){
			sloop=soutput.nextToken();
			StringTokenizer soutput2=new StringTokenizer(sloop,"a,e,i,o,u,A,E,I,O,U");
			System.out.println (sloop + " - " + soutput2.countTokens());
		}
	}
}

You are using StringTokenizer to split the words. Thats ok.
But why are you using it to count the vowels? It doesn't give the right result, because it ignores adjacent wovels and counts them as one.

You have to make a comparison char by char.

import java.util.*;
import java.io.*;

public class Part {
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String sinput, sloop;
		System.out.print("Enter string(s): ");
		sinput = br.readLine();
		StringTokenizer soutput = new StringTokenizer(sinput, ".,,,!,?, ");
		while (soutput.hasMoreTokens()) {
			sloop = soutput.nextToken();
			char[] wovels = { 'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U' };
			int count = 0;
			for (char c : wovels) {
				for (char c2 : sloop.toCharArray())
					if (c == c2)
						count++;
			}
			System.out.println(sloop + " - " + count);
		}
	}
}

thanks for the reply.

here's the other code that I tried.

import java.util.*;
import java.io.*;
public class SToken{
	public static void main (String[]args) throws IOException{
		BufferedReader br=new BufferedReader(new InputStreamReader (System.in));
		String sinput,sloop,sch,vowels="aeiouAEIOU";
		System.out.print ("Enter string(s): ");
		sinput=br.readLine();
		StringTokenizer soutput=new StringTokenizer(sinput,".,,,!,?, ");
		int x=0,z=0;
		char iv,vv;
		while (soutput.hasMoreTokens()){
			sloop=soutput.nextToken();
			x=sloop.length();
			for (int y=0;y<x;y++){
				iv=sloop.charAt(y);
				for (int a=0;a<10;a++){
					vv=vowels.charAt(a);
					if (iv==vv){
						z=z+1;
					}
				}
			}
			System.out.println (sloop + " - " + z);
			z=0;
		}
	}
}

geee... nice codes. I just discovered that loops can have that kind of format. I'll try doing that starting today.

yes, this kind of loops generally known as

foreach

loops in other languages.

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.