Jump to content

Back to the basics: Having trouble with Swtich Case Statement

- - - - -

  • Please log in to reply
10 replies to this topic

#1
An Alien

An Alien

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 260 posts
import java.util.Scanner;


public class FAvowelsCount {

	public static void main(String[] args) {

	String checkForVowels;

	int[] array = new int[6]; //a,e,i,o,u,non-vowels

	String[] arrayString = {"A","E","I","O","U","Non-Vowels"};

	

	//Scan the string:

	Scanner input = new Scanner(System.in);

	System.out.println("Enter a string");

	checkForVowels = input.nextLine();

	

	//read each character in the string by looping

	for(int x = 0; x < checkForVowels.length(); x++){

		

		//if the character being read 

		switch (checkForVowels.charAt(x)) {

	      case 'a': array[0]++; break;

	      case 'e': array[1]++; break;

	      case 'i': array[2]++; break;

	      case 'o': array[3]++; break;

	      case 'u': array[4]++; break;

	      default:  array[5]++; break;

	    }


	}

	

	//results

	int x = 0;

	for(int y : array){

		System.out.println(arrayString[x++]+ " " + y);

	}

	

	

		

}

}

This is a program that counts the occurrences of vowels and non-vowels and then prints them out.
The program counts the vowels alright, for example, if I enter a string: a e i o u f
It would print something like this:
a: 1
e: 1
I: 1
o: 1
u: 1
non-vowels: 6 <<<<<-- This is not correct. The default in the switch statement for some reason I think isn't working right.

#2
Sinipull

Sinipull

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 386 posts
This is correct, because space is not a vowel, which means you have 6 non-vowels.

a e i o u as vowels
and between them
_ _ _ _ _ f as non-vowels
.

#3
An Alien

An Alien

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 260 posts
Thank You! +rep.
For people who are looking for a solution. In the switch case statement, I simply added one more case:
case 'a': array[0]++; break;
case 'e': array[1]++; break;
case 'i': array[2]++; break;
case 'o': array[3]++; break;
case 'u': array[4]++; break;
case ' ': break;
default: array[5]++; break;

#4
Sinipull

Sinipull

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 386 posts
The array[5] is for consonants, right? I'm not sure how are you planning to use it, but there are lots of other symbols, which might appear in text. for example (!?"%:;,.-) and so on. You should just check if the character really is a consonant, and only then add one to the array.

You could use Character.isLetter('c') to see if the character is one of the letters, not a symbol.
If it's letter, and not a vocal, then it's probably consonant.
.

#5
An Alien

An Alien

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 260 posts
Well, I didn't think of that till now. But I don't think we have to make it that robust. I'm taking an intro to programming java class again because I've become rusty. And so they're not really expecting the students to have a program with all the details.

But it is a good thing to know. I'm not sure how I would do that. You said use the method: Character.isLetter('c')
Are you saying it to use that for every non vowel? And what's the difference between using charAt and Character.isLetter('c') other than strings and chars?

#6
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP

An Alien said:

Well, I didn't think of that till now. But I don't think we have to make it that robust. I'm taking an intro to programming java class again because I've become rusty. And so they're not really expecting the students to have a program with all the details.

But it is a good thing to know. I'm not sure how I would do that. You said use the method: Character.isLetter('c')
Are you saying it to use that for every non vowel? And what's the difference between using charAt and Character.isLetter('c') other than strings and chars?
When you use charAt you're pulling any type of character from the String.
A char can be 'i' 'I' 'A' '4' '1' '$' '/' '_'. Using Character.isLetter(char) you can determine if char is a letter between a-z or A-Z.

There are several other methods you can call to determine if a char is a letter, number, etc.
Character (Java Platform SE 7 )

#7
An Alien

An Alien

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 260 posts
Hey Lethal! It's good to see you again.

I started a java class in college again cause I didn't do any java at all over the summer. Also, I got a 3/5 on the AP exam thanks to you.

And thanks for the explanation, I get it now. +rep.

#8
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP

Quote

Also, I got a 3/5 on the AP exam thanks to you.
And you thought you were going to get a 1 or a 2! That's what all-nighters can do for you! :P

#9
An Alien

An Alien

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 260 posts
haha yeah, I learned that pulling all nighters is good for short term, but if you keep doing it, it can have a negative impact on your daily life and you'll be less productive so now I try to keep a good routine in college. I had a quick java question that I'm posting here. I felt that there wasn't a need to make a new thread because it was a nooby question.
3) Which of the following is an invalid way to instantiate a String object?

a) String title = new String(“Java Software Solutions”);
b) String name = “John Lewis”;
c) String empty = “”;
d) String alsoEmpty = new String(“”);
e) all of the above are valid

#10
Sinipull

Sinipull

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 386 posts
Why are you asking this question?

I mean, you DO have java installed on your computer, so why don't you try to compile these and see for yourself?
.

#11
An Alien

An Alien

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 260 posts
I'm sorry lol. I wasn't thinking when I asked this question. Actually I've got midterms for all of my subjects and I've been pretty stressed so I completely forgot I can test this in my IDE. Just ignore it.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users