Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Not sure what is wrong with my code.

  1. #1
    Junior Member
    Join Date
    Apr 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Angry Not sure what is wrong with my code.

    Hi all,
    I tried to make a program that takes a string str, and char a and checks how many times the char is used in the string.
    Example: the string Welcome and the letter e, is 2 times. so the program should print 2.
    It compiles but when I run it and enter the information, i cannot get the printing line out.
    Thanks!!

    Heres my code:

    import java.util.Scanner;
    class program
    {
    public static void main(String[] args)
    {
    Scanner user_input=new Scanner(System.in);
    String str;
    String b;
    System.out.print("Please enter a word");
    str=user_input.next();
    System.out.print("Enter the charachter you would like to check for");
    b=user_input.next();
    char a=b.charAt(0);
    int result;
    result=count(str,a);
    System.out.println(" The number of times the letter " +a+ " has been used in the word " +str+ " is " +result);

    }




    public static int count( String str, char a)
    {
    int counter=0;
    int i=0;
    while(str.length()>i)
    {
    while(str.charAt(i)==a)
    {
    counter++;
    i++;
    }
    str.substring(counter);
    }
    return counter;
    }
    }


  2. #2
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    285
    My Mood
    Amused
    Thanks
    6
    Thanked 64 Times in 61 Posts

    Default Re: Not sure what is wrong with my code.

    Hi! Welcome to the forum! Please read Announcements - What's Wrong With My Code?
    and wrap your code please.

Similar Threads

  1. [SOLVED] what s wrong with my code
    By essayd100 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 21st, 2014, 01:59 PM
  2. What Did I Do Wrong With My Code?
    By zendorz in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 14th, 2013, 02:01 PM
  3. what is wrong with my code? please help
    By black.angel in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 13th, 2012, 02:34 PM
  4. What is wrong with my code??
    By joes71monte in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 27th, 2012, 11:56 AM
  5. what is wrong with my code?
    By bmb in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 7th, 2011, 09:03 AM