1,105,288 Community Members

java problem plzz help

Member Avatar
tartosha
Newbie Poster
2 posts since Mar 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Write a program that reads characters from the keyboard until a period is
received. Have the program count the number of spaces and display it to the
user. After that, ask the user if he/she would like to count a specific character.
Terminate when the user is done counting the characters.
plzzz help me

Member Avatar
stultuske
Posting Expert
5,290 posts since Jan 2007
Reputation Points: 935 [?]
Q&As Helped to Solve: 774 [?]
Skill Endorsements: 35 [?]
Featured
 
0
 

what help do you need?
you know what to do, the only thing you need to actually create and test that application is a keyboard, and since you've been able to write that post, I 'm pretty sure you already have one.

show us the code you've written and where you 're stuck, but we're not going to write code-on-demand.

Member Avatar
tartosha
Newbie Poster
2 posts since Mar 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 
import java.util.Scanner;
public class Characters 
{
    public static void main(String args[])
    {
        Scanner input = new Scanner(System.in);
        char ch; 
        int spaces = 0; 
        System.out.println("Enter . to stop."); 
        while(ch != '.') 
        { 

              ch = (char) System.in.read();
              System.out.println(ch); 
              spaces++;


         }
        System.out.println("number of spaces : "+spaces);
        System.out.println("would like to count a specific character(yes or no) : ");

    } 
}

I don't know how to count a specific character

Member Avatar
stultuske
Posting Expert
5,290 posts since Jan 2007
Reputation Points: 935 [?]
Q&As Helped to Solve: 774 [?]
Skill Endorsements: 35 [?]
Featured
 
0
 

first: let him choose yes or no, if yes, let him choose a certain char, which you store in a variable.

you're already comparing them to '.' so you know how to compare them ( if char == testChar)
also, at the start of your app, initialize an int counter and set it to 0;

if ( charIJustRead == testChar)
counter += 1;

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: