Results 1 to 3 of 3

Thread: need help

  1. #1
    marc is offline Member
    Join Date
    Apr 2010
    Posts
    4
    Rep Power
    0

    Default need help

    hi

    i have a problem in my code
    i shoud make it by switch,if or if else

    i tried switch but doesn't work and this is my code
    i don't know what shoud i do with str2 and str3

    i'm not strong in it
    could you please help me

    Java Code:
    import java.util.*;
    public class E3
    {
    static Scanner console = new Scanner (System.in);
    public static void main(String [] args )
    {
    
    int len, counterV, counterS;
    String str2;
    counterV = 0;
    counterS = 0;
    String str, str3;
    len = str.length();
    
    str3 =" "+(char)str.charAt(len/2-5)+(char)str.charAt(len/2-4)+(char)str.charAt(len/2-3)+(char)str.charAt(len/2-2)+(char)str.charAt(len/2-1)+(char)str.charAt(len/2)+(char)str.charAt(len/2+1)+(char)str.charAt(len/2+2)+(char)str.charAt(len/2+3)+(char)str.charAt(len/2+4);
    str2 = (char)str3.substring(str.charAt(len/2-5),str.charAt(len/2+4));
    System.out.println(" Enter string consist of more than or equal  10 letters :");
    str = console.next();
    
    switch (str2)
    {
    case 'a':
    counterV++;
    case 'e':
    counterV++;
    case 'i':
    counterV++;
    case 'u':
    counterV++;
    case 'o':
    counterV++;
    break;
    
    case '@':
    counterD++;
    case '?':
    counterD++;
    case '!':
    counterD++;
    case '$':
    counterD++;
    case '#':
    counterD++;
    break;
    }}}

  2. #2
    marc is offline Member
    Join Date
    Apr 2010
    Posts
    4
    Rep Power
    0

    Default

    Sorry i forget the Q


    Write a program to read a (not less than 10 letters) string (str) and in the middle 10 characters of the string, count the number of vowel letters(cap or small) : (e, i,a ,o,u ), and number of these special_symbols : (? , $, #, @, !) .
    Example 1 :
    Input : str =
    N o ! o n e @ K S U ? w h y ?
    output :
    Number of vowels = 3
    Number of special symbols = 2

    Example 2 :
    Input : str =
    H i I T @ C C I S ! ?
    output :
    Number of vowels = 3
    Number of special symbols = 1

  3. #3
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    952
    Rep Power
    5

    Default

    Your sample output looks wrong to me:
    Quote Originally Posted by marc View Post
    Example 1 :
    Input : str =
    N o ! o n e @ K S U ? w h y ?
    output :
    Number of vowels = 3
    Number of special symbols = 2
    Looks like 4 vowels and 4 special symbols to me.
    Quote Originally Posted by marc View Post
    Example 2 :
    Input : str =
    H i I T @ C C I S ! ?
    output :
    Number of vowels = 3
    Number of special symbols = 1
    And this one looks like 3 vowels and 3 special symbols.

    Anyway, your code initializes a variable counterS, but then you try to use counterD. You are casting a bunch of things to be char that are already char. You're trying to pass char parameters where ints are expected. Your switch logic for values is considering lower-case vowels only.

    You should write two methods like this:
    Java Code:
            private static boolean isVowel(char c) {
                    // TODO: return true if char c is a vowel, false otherwise
            }
    
            private static boolean isSpecialCharacter(char c) {
                    // TODO: return true if char c is a "special" character, false otherwise
            }
    Then your main method can be much simpler.
    Java Code:
            get String from user
            initialize numVowels and numSpecialChars to 0
            for each char in the String
                    // use the methods you wrote above
                    if char is a vowel, increment numVowels
                    if char is a special character, increment numSpecialChars
            print the output
    Don't forget to make sure that isVowel() works for upper or lower case letters.

    -Gary-

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •