Java Program Help

Discussion in 'OT Technology' started by usd2020, Oct 7, 2005.

  1. usd2020

    usd2020 New Member

    Joined:
    May 16, 2004
    Messages:
    101
    Likes Received:
    0
    Location:
    Belmont, NC
    Ok, so i'm in a basic level java course and i have a professor that refuses to answer any questions so i figured someone here could help me out.

    I have to write a program that will ask the user to enter a string, and then ask the user to enter a character. The program should count and display the number of times that the character appears in the string and display it.

    We are just now starting on If/else statements and i'm really not sure how to even start this program. So if anyone can help me out or point me in the right direction i'd really appreciate it. thanks
     
  2. Aimless

    Aimless Resident drunkey

    Joined:
    Nov 5, 2001
    Messages:
    2,534
    Likes Received:
    0
    Location:
    Wisconsin
    Meh, I'm bored. Here's what you need to do.

    Store the input as a String and Char.

    Make an int variable to store the count results. Set it to 0.

    Do a for loop for the length of the String.
    If the char at the index (of that loop iteration) of the string equals the char you have stored, increment the count variable one.

    Display the count variable to the user.
     
  3. Dwight Schrute

    Dwight Schrute New Member

    Joined:
    Oct 11, 2003
    Messages:
    8,616
    Likes Received:
    0
    <code>
    import java.util.Scanner;

    public class CharacterCount
    {
    public static void main( String args[] )
    {
    Scanner input = new Scanner( System.in );
    String userString;
    String userChar;
    int charCount = 0;

    System.out.print( "Enter string: " );
    userString = input.nextLine();

    System.out.print( "Enter character: " );
    userChar = input.nextLine();

    for( int i = 0; i < userString.length(); i++ )
    {
    if( userString.substring( i, i + 1 ).equals( userChar ) )
    charCount++;
    }

    System.out.printf( "'%s' exists %d times in '%s'", userChar, charCount, userString);
    }
    }
    </code>
     
  4. Joe_Cool

    Joe_Cool Never trust a woman or a government. Moderator

    Joined:
    Jun 30, 2003
    Messages:
    299,326
    Likes Received:
    566
    What kind of piece of shit instructor won't answer questions?
     
  5. Aimless

    Aimless Resident drunkey

    Joined:
    Nov 5, 2001
    Messages:
    2,534
    Likes Received:
    0
    Location:
    Wisconsin
    Well I wasn't going to do it for him :o
     
  6. usd2020

    usd2020 New Member

    Joined:
    May 16, 2004
    Messages:
    101
    Likes Received:
    0
    Location:
    Belmont, NC
    Whenever you ask him a question he immediatly assumes that the book should answer all of your questions and he basically changes the subject if you keep asking about it. He's also one of the cockiest professor/teacher i've ever met and never likes to admit he's wrong.




    As for the program, i tried danidentity's code and it kept failing to compile for me. I'm working on Aimless' ideas now, hopefully i can get this working sometime this morning.

    Thanks for the responses
     
  7. Peyomp

    Peyomp New Member

    Joined:
    Jan 11, 2002
    Messages:
    14,017
    Likes Received:
    0
    What are you ediiting code in? Doesn't it have an integrated debugger? If not, www.eclispe.org download that, and learn to use an IDE.
     
  8. samm

    samm Next in Line

    Joined:
    Dec 22, 2000
    Messages:
    2,630
    Likes Received:
    0
    Location:
    San Jose, CA
    His code looks like it requires J2SE 5.0, at least I think the java.util.Scanner class was added in that version.
     

Share This Page