Search
Search titles only
By:
Search titles only
By:
Forums
New posts
Search forums
What's new
New posts
Latest activity
Feedback
View Statistics
Members
Current visitors
Buy Sell Trade
WTB
Log in
Register
Search
Search titles only
By:
Search titles only
By:
New posts
Search forums
Menu
Install the app
Install
Reply to thread
Forums
Technology
Programming
A simple java query
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Message
<blockquote data-quote="R0H!T" data-source="post: 884996" data-attributes="member: 21231"><p>Hi,</p><p></p><p>I am doing this assignment for my friend <img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" class="smilie smilie--sprite smilie--sprite7" alt=":p" title="Stick Out Tongue :p" loading="lazy" data-shortname=":p" /></p><p>and I am new to java [tho I had learned some basics a year ago]</p><p></p><p>anyways, one of the assignment question is "Write a program that accepts a string and finds the no of vowels in it."</p><p></p><p>now, I have written the following code:</p><p></p><p>[CODE]import java.util.*;</p><p>public class vowel </p><p>{</p><p> public static void main(String[] args) </p><p> {</p><p> Scanner a = new Scanner(System.in);</p><p></p><p> System.out.println("Enter a string:");</p><p> String s = a.next();</p><p> int i,count = 0;</p><p></p><p> for (i = (s.length() - 1); i >= 0; i--)</p><p> {</p><p> if (s.charAt(i) == 'a' || s.charAt(i) == 'e' || s.charAt(i) == 'i' || s.charAt(i) == 'o' || s.charAt(i) == 'u')</p><p> {</p><p> count++;</p><p> }</p><p> else if (s.charAt(i) == 'A' || s.charAt(i) == 'E' || s.charAt(i) == 'I' || s.charAt(i) == 'O' || s.charAt(i) == 'U')</p><p> {</p><p> count++;</p><p> }</p><p> }</p><p> System.out.println("The number of vowels in the above string: " + count);</p><p> }</p><p>}[/CODE]</p><p></p><p>now it prints the number of vowels only for the first word i.e. it does not consider anything after a white space.</p><p></p><p>so if I type say 'abc' it prints 1, but for 'abc abc' also it prints 1 :huh:</p><p></p><p>Sorry, for asking such a simple question :ashamed:</p></blockquote><p></p>
[QUOTE="R0H!T, post: 884996, member: 21231"] Hi, I am doing this assignment for my friend :P and I am new to java [tho I had learned some basics a year ago] anyways, one of the assignment question is "Write a program that accepts a string and finds the no of vowels in it." now, I have written the following code: [CODE]import java.util.*; public class vowel { public static void main(String[] args) { Scanner a = new Scanner(System.in); System.out.println("Enter a string:"); String s = a.next(); int i,count = 0; for (i = (s.length() - 1); i >= 0; i--) { if (s.charAt(i) == 'a' || s.charAt(i) == 'e' || s.charAt(i) == 'i' || s.charAt(i) == 'o' || s.charAt(i) == 'u') { count++; } else if (s.charAt(i) == 'A' || s.charAt(i) == 'E' || s.charAt(i) == 'I' || s.charAt(i) == 'O' || s.charAt(i) == 'U') { count++; } } System.out.println("The number of vowels in the above string: " + count); } }[/CODE] now it prints the number of vowels only for the first word i.e. it does not consider anything after a white space. so if I type say 'abc' it prints 1, but for 'abc abc' also it prints 1 :huh: Sorry, for asking such a simple question :ashamed: [/QUOTE]
Insert quotes…
Verification
Post reply
Forums
Technology
Programming
A simple java query
Top
Bottom