can anyone help me with this

import javax.swing.*;
public class Q6 {
public static void main ( String args [] ) {
int nv,np,ns,nc;
int c=0 , v=0 , p=0 , s=0;
String input = JOptionPane.showInputDialog(null,"please Enter the statement") ;
for (int i=0; i<input.length ( ); i++) {
switch (input.charAt (i)) {
case 'a': case 'A': case 'i': case 'I': case 'e': case 'E': case 'u': case 'U':
case 'o': case 'O':
case '.': case '?': case '{': case '}': case '[': case ']': case '-': case '_':
case ':': case '"': case ';': case ',': case '!':
case' ':
s++ ;
default :
c++ ; }
System.out.println("Number Of constant :"+ nc) ;
System.out.println("Number Of vowels :"+nv ) ;
System.out.println("Number Of punctuation charachter :"+ np) ;
System.out.println("Number Of spaces :"+ ns) ;

i got an error but i don't know where is it ?

7 Years
Discussion Span
Last Post by mvmalderen

You have made this a hard as possible for anyone who wants to help - unformatted code, zero comments, no description of the actual error.
Anyway, you count in c,v,p,s but then display the unused variables nc, nv, np, ns.


aahh babe i think you miss 1 closing curly brace at the end of your code..... ow and babe the intigers you declared has no use as you see its a garbage.. know what i mean..

Edited by kelvin33: forgot to add something


You're making things more complex than they are, why for example do you use a switch? It's very error-prone for this task.
I would rather opt for another approach, I'd make use of Strings which hold all the vowels, consonants and punctuation marks you want to count and then use the indexOf method to find out whether it is a character, vowel, or punctuation mark.
Here's an example of what I mean:

// Create a string which holds all the vowels
String vowels = "auioey";

Now you can use the indexOf method to find out whether a certain character is a vowel, like this:

char c = 'a';
if( vowels.indexOf( Character.toLowerCase(c) ) >= 0 )
  // The character contained in variable 'c' is a vowel
  // Take appropriate actions: increase the vowel count

Combining all this to a simple vowel count program, would make something like this:

public class VowelCount
  public static void main(String args[])
    // string containing all vowels
    final String vowels = "auioey";
    // hold the number of vowels
    int v = 0;
    // the string we're about to analyze
    String t = "Hello World!!!";
    // iterate over all characters in the string
    for(int i = 0; i < t.length(); i++)
      // check for vowel
      if( vowels.indexOf( Character.toLowerCase( t.charAt(i) ) ) >= 0 )

        /* TODO:
            add checks for:
                - consonants
                - punctuation marks
                - spaces
    // print out the result
    System.out.println("Number of vowels: " + v);

Similarly you can add checks for consonants, punctuation marks and spaces on your own by applying the method I used.

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.