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

    Default 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
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    6,285
    My Mood
    Mellow
    Thanks
    252
    Thanked 777 Times in 762 Posts

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

    Duplicate post closed.

Similar Threads

  1. what's wrong with my code
    By seamus1000 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: January 1st, 2014, 06:55 AM
  2. There are something wrong with my code...
    By khanhnq in forum Object Oriented Programming
    Replies: 0
    Last Post: January 13th, 2013, 09:04 PM
  3. What is wrong with my code ?
    By rajarshi in forum What's Wrong With My Code?
    Replies: 0
    Last Post: January 11th, 2013, 10:54 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's wrong with my code ?
    By mithani in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 5th, 2010, 09:57 AM