Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.
Need Help With A Simple Program (indexOf)...
This is the my first message. I joined Programmers Heaven today because I am seeking help in my assignments. I find Java really fun, challenging, and frustrating. I am here to learn and improve my Java skills, so that I could become better
. I am currently having difficulties with an assignment. I need some help because I "think" I am doing something wrong with my loop, but I am not sure. Please take a look at the bottom to further understand what I am saying.
THE OBJECTIVE: Write a program that lets an user enter in any word, and any character in lower case. Print the number of occurrences of that character in the word. Change the word to lower case before finding the number of occurrences in the word.
Lets say a user enters in the word "Canada", and the character "a". The program is supposed to test how many times "a" is in "Canada". Since "a" goes in "Canada" 3 times, it is suppose to print in the output screen 3, correct? This is not what my program does unfortunately. I do not know what to do, but I will post my source code to see if anyone can help me
NOTE: In our school, we use c.print instead of system.out, so please do not get confused
// The "Chap3Program8" class.
public class Chap3Program8
static Console c; // The output console
public static void main (String  args)
c = new Console ();
String word, lowerword;
int count, length;
c.println ("Please enter in any word.");
word = c.readString ();
lowerword = word.toLowerCase ();
length = lowerword.length ();
c.println ("Please enter in any lower case character");
letter = c.readChar ();
for (count = 0 ; length > count ; count++)
c.println (lowerword.indexOf (letter));
} // main method
} // Chap3Program8 class
0 · ·