Jump to content

Cannot Get Java Program to Work

tp95112

Trying to do a java question and can't get it to work. I get the idea and I think I have it structured properly. 

Question

            Write a program that reads a string from the user, then determines and prints how many of each lowercase vowels (a, e. i, o, and u) appear in the entire string. 

Have a separate counter for each vowel. 

Also count and print the number of nonvowel characters.

 

What I did was use a series of if else statements test if the current letter is vowel or not. If it is a vowel then the int will increment, if its not a vowel then the nonvowel will increment by 1.

import java.util.Scanner;public class questionfour {	public static void main(String[] args) {				String input;		//a e i o u vowels===================		int va = 0;		int ve = 0;		int vi = 0;		int vo = 0;		int vu = 0;		//a e i o u vowels===================		int nonvowel=0;		int firstletter = 0;		int length;		String current;		System.out.println("Enter a string of words");		Scanner sc = new Scanner(System.in);			    input = sc.nextLine();	    length = input.length();	    //read first letter if vowel else non vowel repeat	    for( int count = 0; count < length;count++){	    	if (current.isLetter(a)){	    		va++;	    		}else if (current.isLetter(e)){	    			ve++;	    			}else if (current.isLetter(i)){	    				vi++;	    				}else if (current.isLetter(o)){	    		    		vo++;	    		    		}else if (current.isLetter(u)){		    		    		vu++;		    		    		}else nonvowel++;{			    		    		}		    		    		System.out.println("There are " + va +"vowels");		    		    		System.out.println("There are " + ve +"vowels");		    		    		System.out.println("There are " + vi +"vowels");		    		    		System.out.println("There are " + vo +"vowels");		    		    		System.out.println("There are " + vu +"vowels");		    		    		System.out.println("There are " + nonvowel +"nonvowels");	    			}	}}

Eclipse is saying the problem is the if statement conditons

Link to comment
Share on other sites

Link to post
Share on other sites

1. Your indenting is absolutely atrocious, the logic is essentially unreadable.

2. This is a situation to use the switch statement.

Link to comment
Share on other sites

Link to post
Share on other sites

http://docs.oracle.com/javase/7/docs/api/java/lang/Character.html#isLetter(char)

 

You're using isLetter() in the wrong way. isLetter is for checking whether the input is a Character. You also don't need that many else ifs, since you can just make the first if check the whole thing with indexOf(). Also, note that the guideline that you were given makes special mention of checking for only lowercase, so you're probably going to lose points if you don't include that in there.

 

And yes, as @DavidTheWin pointed out, your indenting is messy: You're not supposed to nest else if.

I own and use, sorted from newest to oldest: SteelSeries 6Gv2. Microsoft SideWinder X4. Mionix Naos 7000. Zowie EC1 Evo. Microsoft SideWinder X8. Microsoft IntelliMouse Explorer 3.0. Dell U2414H. Samsung P2270H. AKG K273 Pro. Sennheiser HD555. Razer Goliathus Speed Medium. Func 1030 L. Qpad CT Medium.

I used to own: Razer DeathAdder 3G. Razer Krait. IntelliMouse Optical 1.1. SteelSeries QcK.

Link to comment
Share on other sites

Link to post
Share on other sites

Tinkered with it for a while and got it to work but output is wrong and I havent learned switch statements

import java.util.Scanner;public class questionfour {	public static void main(String[] args) {				String input, current;		//a e i o u vowels===================		int va = 0;		int ve = 0;		int vi = 0;		int vo = 0;		int vu = 0;		//a e i o u vowels===================		int nonvowel=0;		int firstletter = 0;		int length;				System.out.println("Enter a string of words");		Scanner sc = new Scanner(System.in);			        input = sc.nextLine();	        length = input.length();	    	        //read first letter if vowel else non vowel repeat	        for( int count = 0; count < length;count++){	    	current = input.substring(firstletter);	    	if (current == "e"){	    		va++;	    		}else if (current == "e"){	    			ve++;	    			}else if (current == "i"){	    				vi++;	    				}else if (current == "o"){	    		    		vo++;	    		    		}else if (current == "u"){		    		    		vu++;		    		    		}else nonvowel++;	    	                        firstletter++;    		    			    			}		System.out.println("There are " + va +" vowels");		System.out.println("There are " + ve +" vowels");		System.out.println("There are " + vi +" vowels");		System.out.println("There are " + vo +" vowels");		System.out.println("There are " + vu +" vowels");		System.out.println("There are " + nonvowel +"nonvowels");	}}
Link to comment
Share on other sites

Link to post
Share on other sites

import java.util.Scanner;public class questionfour {	public static void main(String[] args){		String input, current;		//a e i o u vowels===================		int va = 0;		int ve = 0;		int vi = 0;		int vo = 0;		int vu = 0;		//a e i o u vowels===================		int nonvowel=0;		int firstletter = 0;		int length;				System.out.println("Enter a string of words");		Scanner sc = new Scanner(System.in);				input = sc.nextLine();		length = input.length();	    		//read first letter if vowel else non vowel repeat		for( int count = 0; count < length;count++){			current = input.substring(firstletter);			if (current == "e"){				va++;			} else if (current == "e"){				ve++;			} else if (current == "i"){				vi++;			} else if (current == "o"){				vo++;			} else if (current == "u"){				vu++;			} else nonvowel++;			firstletter++;    		    				}		System.out.println("There are " + va +" vowels");		System.out.println("There are " + ve +" vowels");		System.out.println("There are " + vi +" vowels");		System.out.println("There are " + vo +" vowels");		System.out.println("There are " + vu +" vowels");		System.out.println("There are " + nonvowel +"nonvowels");	}} 

fixed indentation

Mini-Desktop: NCASE M1 Build Log
Mini-Server: M350 Build Log

Link to comment
Share on other sites

Link to post
Share on other sites

error 1:

if (current == "e")
should be

 if (current == "a")
error 2:

current = input.substring(firstletter);
should be

 current = input.substring(count,count);
or

 current = input.charAt(count);

Mini-Desktop: NCASE M1 Build Log
Mini-Server: M350 Build Log

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×