I can ot get my output to reflect accurate info, does anyone have a possible solution?

///////////////////////////////////////////////////////////
import java.util.Scanner;

public class Count
{
public static void main (String[] args)
{
String phrase; // a string of characters
int countBlank; // the number of blanks (spaces) in the phrase
int length; // the length of the phrase
int countA = 0; // the number of a's in the phrase
int countT = 0; // the number of t's in the phrase
char ch; // an individual character in the string

Scanner scan = new Scanner(System.in);

// Print a program header
System.out.println ();
System.out.println ("Character Counter");
System.out.println ();

// Read in a string and find its length
System.out.print ("Enter a sentence or phrase ('quit' to quit): ");
phrase = scan.nextLine();
length = phrase.length();

// Initialize counts
countBlank = 0;

// a while loop to allow user to keep entering phrases
while (!phrase.equals("quit"))
phrase = scan.nextLine();
length = phrase.length();

{
// a for loop to go through the string character by character
// and count the blank spaces
for(int i = 0; i < phrase.length(); i++)
{
ch = phrase.charAt(i);

switch (ch)
{
case ' ': countBlank++;
break;
case 'a':
case 'A': countA++;
break;
case 't':
case 'T': countT++;
break;
}

}
// Print the results
System.out.println ();
System.out.println ("Number of blank spaces: " + countBlank);
System.out.println ("Number of A's: " + countA);
System.out.println ("Number of T's: " + countT);
System.out.println ();
}
}
}
////////////////////////////////////////////////////////////

Dani AI

Generated

Brief diagnosis for : the program’s control flow and brace placement prevent per-line processing, so input lines get overwritten and the sentinel word ends up being counted. Counters are declared once and never reset, which makes results accumulate across iterations. correctly identified the loop problem, but the overall fix requires rearranging the read/check/process sequence and tightening up variable scope.

Suggested, minimal workflow (no code shown here):

  1. Read a single input line into a local variable.
  2. Immediately test that local variable against the sentinel (use a case‑insensitive test if desired) and stop before any processing if it matches.
  3. Reset all counters to zero for each new line so counts don’t carry over.
  4. Iterate the characters of that single line and update counters (normalizing characters to lower case simplifies comparisons and reduces branches).
  5. Print the counts for that line, then return to step 1.

Practical notes and gotchas:

  • Always use braces for while/for blocks even for single statements; missing braces are a common source of the “only the last line is processed” bug seen here.
  • Use a fresh local line variable for each read rather than continually concatenating into a phrase unless accumulation across multiple reads is the intent. If accumulation is needed, initialize the accumulator (empty string or StringBuilder) before using it.
  • Use exact string comparison methods (case-insensitive when appropriate) rather than ==.
  • Remember to close the Scanner when the program finishes.

Quick test to verify behavior: a single input like “This is a test” should report 3 blanks, 1 A (case-insensitive), and 2 T’s. Applying the read-then-check-then-process pattern and resetting counters each loop will produce the correct, repeatable results.

Recommended Answers

All 3 Replies

Your error lies in the following code:

while (!phrase.equals("quit"))
phrase = scan.nextLine();

This reads lines but only saves the last line in phrase. Each time it overwrites the last line with the new line. Also, your loop is stopping too late. It includes quit as a phrase.

The correct code would be

//loop
while (true)
{	
[INDENT]//read a line
String line=scan.nextLine();
	
//if its quit stop right now
if(line.equals("quit"))
	break;
else
//ADD the line to the phrase
	phrase += line;[/INDENT]
}

For more help,

public class Bilang {
    public static void main(String[]args) {

    Counter b0 = new Counter();
    Counter b1 = new Counter();
    Counter b2 = new Counter();
    Counter b3 = new Counter();

    b0.setNext(b1);
    b1.setNext(b2);
    b2.setNext(b1);
    b3.setNext(null);
    System.out.println("count");
    System.out.println("exit");
}

    public void count() {
    if(b==0){
    b=1;
    }else{
    b0=0;
    if(next.count(b1));
}
    if(b1==0){
    b1=1;
    }else{
    b1=0;
    if(next.count(b2));
}
    if(b2==0){
    b2=1;
    }else{
    b2=0;
    if(next.count(b3));
}
    if(b3==1){
    b3=0;
    }else{
    b3=1;
    if(next!=null);
}
}

    class counter extends Bilang {
    private int b;
    private counter next;

    Counter b0 = new Counter();
    Counter b1 = new Counter();

    b0.setNext(b1);
    b1.setNext(null);

    System.out.println("Count");
    System.out.println("Exit");

}
    public void count() {
    if (b == 0){
    b=1;
    }else{
    b=0;
    if(next!=null);
}
}
}

How did you ever find this 6 year old thread to revive?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.