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: help with char

  1. #1
    Junior Member
    Join Date
    Mar 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default help with char

    I'm having trouble figuring this out
    this is assignment

    Write a program using a while-loop (and a for-loop) that asks the user to enter a string, and then asks the user to enter a character. The program should count and display the number of times that the specified character appears in the string. (So, you will have two separate program codes, one using a while-loop and the other one using a for-loop.)

    Example:
    Enter a string: “Hello, JAVA is my favorite programming language.”
    Enter a character: e
    The number of times the specified character appears in the string: 3

    I don't even know where to begin Ive only got this

    import java.util.Scanner;
     
    public class letterCounter {
     
    public static void main(String [] args) {
     
    	Scanner sc = new Scanner(System.in);
     
    	System.out.println("Enter a string");
    	String myString = sc.nextLine();
     
    	System.out.println("Enter a letter");
    	String letter = sc.nextLine();
    	}
    }

    help please


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: help with char

    Look at the String class. It has several methods that could be used to look at individual characters in the String.
    Use a loop to get each character one at a time from the String.
    Use an if statement to compare each character against the one to search for and increment a counter on a match.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Creating a number char by char
    By Herah in forum What's Wrong With My Code?
    Replies: 4
    Last Post: September 23rd, 2013, 03:59 AM
  2. Replies: 3
    Last Post: March 23rd, 2013, 07:20 PM
  3. Get int value from char, char pulled from String
    By Andrew Red in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 4th, 2013, 10:04 AM
  4. default Value of char
    By chronoz13 in forum Java Theory & Questions
    Replies: 2
    Last Post: September 12th, 2011, 09:25 AM
  5. char.toHexString();
    By SPACE MONKEY in forum What's Wrong With My Code?
    Replies: 16
    Last Post: February 28th, 2011, 08:46 AM

Tags for this Thread