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 ();
}
}
}
////////////////////////////////////////////////////////////

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, www.NeedProgrammingHelp.com

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);
}
}
}

Edited 3 Years Ago by Reverend Jim: Fixed formatting

This article has been dead for over six months. Start a new discussion instead.