Click here to Skip to main content
15,900,325 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Java
class Car
{
   static String a;
   static String str="";
   static int count=0;
   public static void input()
   {
       Scanner scan=new Scanner(System.in);
       System.out.println("Enter the sentence :");
       a=scan.nextLine();
    
     String str=a.toUpperCase();
    str=""+str;
}
    public static void Vowels()
    {
        for(int i=0;i<str.length()-1;i++)
        {
            char b=str.charAt(i);
            if(Character.isWhitespace(b)&&(char)(b+1)=='A')
            {
                count++;
            }
        }
        System.out.println("The total number of words starting with letter 'A' is :"+count);
    }


What I have tried:

The above code is what I tried.
Posted
Updated 25-May-20 10:12am
v3

b+1 will give you the character which is directly following the character in b in an ASCII table, not the next character in input string. Moreover, your code only tests for uppercase A, but will fail at finding a word beginning with a lowercase a.
I won't write it for you since it is important for you to understand what is wrong, and and to correct it yourself. Try it, this is not difficult at all; and you will feel proud by having solved it by yourself.
 
Share this answer
 
v2
Java
char b=str.charAt(i); // here, b is the letter at position i
if(Character.isWhitespace(b)&&(char)(b+1)=='A') // here b+1 is next letter in alphabet, not next letter in sentence

try
Java
char b=str.charAt(i);
char c=str.charAt(i+1);
if(Character.isWhitespace(b)&& c=='A')

-----
By the way, your count will be wrong if first word starts with a 'a'. and you forgot to call the function 'vowels'
-----
Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900