Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
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)...

LeKtriXLeKtriX Posts: 2Member
Hello,

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.
import java.awt.*;
import hsa.Console;

public class Chap3Program8
{
static Console c; // The output console

public static void main (String [] args)
{
c = new Console ();
String word, lowerword;
char letter;
int count, length;
c.println ("Please enter in any word.");
word = c.readString ();
lowerword = word.toLowerCase ();
length = lowerword.length ();
c.println ();
c.println ("Please enter in any lower case character");
letter = c.readChar ();
c.println ();
for (count = 0 ; length > count ; count++)
{
c.println (lowerword.indexOf (letter));
}

} // main method
} // Chap3Program8 class



Comments

  • LeKtriXLeKtriX Posts: 2Member
    Please help!!!









    : Hello,
    :
    : 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.
    : import java.awt.*;
    : import hsa.Console;
    :
    : public class Chap3Program8
    : {
    : static Console c; // The output console
    :
    : public static void main (String [] args)
    : {
    : c = new Console ();
    : String word, lowerword;
    : char letter;
    : int count, length;
    : c.println ("Please enter in any word.");
    : word = c.readString ();
    : lowerword = word.toLowerCase ();
    : length = lowerword.length ();
    : c.println ();
    : c.println ("Please enter in any lower case character");
    : letter = c.readChar ();
    : c.println ();
    : for (count = 0 ; length > count ; count++)
    : {
    : c.println (lowerword.indexOf (letter));
    : }
    :
    : } // main method
    : } // Chap3Program8 class
    :
    :
    :
    :

  • GameTroyGameTroy Posts: 214Member
    : Hello,
    :
    : 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.
    : import java.awt.*;
    : import hsa.Console;
    :
    : public class Chap3Program8
    : {
    : static Console c; // The output console
    :
    : public static void main (String [] args)
    : {
    : c = new Console ();
    : String word, lowerword;
    : char letter;
    : int count, length;
    : c.println ("Please enter in any word.");
    : word = c.readString ();
    : lowerword = word.toLowerCase ();
    : length = lowerword.length ();
    : c.println ();
    : c.println ("Please enter in any lower case character");
    : letter = c.readChar ();
    : c.println ();
    : for (count = 0 ; length > count ; count++)
    : {
    : c.println (lowerword.indexOf (letter));
    : }
    :
    : } // main method
    : } // Chap3Program8 class
    :
    :
    :
    :
    '-------------------------------------------
    hey i have a question... is your program a class or a script? i tried runing it as a script and it did nothing. i was just checking out your program and i don't know much about java other than how to tell the difference between C language. anyway thats all i was wondering.



    Moses 1:39 - "For behold, this is my work and my glory-to bring to pass the immortality and eternal life of man.


    -Troy



Sign In or Register to comment.