import java.io.*;

class InputDiagnosis{

      public static void main(String args[]) throws IOException
      {
         char ch;
         int digit=0;
         int upper=0;
         int lower=0;
         int other=0;

         BufferedReader inputstream =new BufferedReader(new InputStreamReader(System.in));

         System.out.println(" Type some text. When done, press Enter to Quit:");


         do{

          ch=(char) inputstream.read();

          if(Character.isDigit(ch))
            digit++;
          else if(Character.isUpperCase(ch))
            upper++;
          else if(Character.isLowerCase(ch))
            lower++;
          else
            other++;

          }while(ch !=' ');


          System.out.println("No Of Digits:" +digit);
          System.out.println("No Of Uppercase Characters:" +upper);
          System.out.println("No Of Lowercase Characters:" +lower);
          System.out.println("No Of Other Characters:" +other);

         }
     }

Dani AI

Generated

The loop is behaving as observed because the program tests for a space character while Console Enter produces line terminators — not a space — and because the code casts the reader result directly to char. BufferedReader.read() returns an int (and -1 at EOF); casting that straight to char makes EOF invisible and will also let carriage-return/line-feed characters be counted as “other.” That combination explains why pressing Enter doesn't stop the loop the way the poster expected.

A cleaner, more reliable approach is to read whole lines and stop when the line is empty (Enter on an empty line) or null (EOF). This avoids dealing with CR/LF differences across platforms and keeps classification logic simple:

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int digit=0, upper=0, lower=0, other=0;
String line;
while ((line = br.readLine()) != null && !line.isEmpty()) {
    for (char c : line.toCharArray()) {
        if (Character.isDigit(c)) digit++;
        else if (Character.isUpperCase(c)) upper++;
        else if (Character.isLowerCase(c)) lower++;
        else other++;
    }
}
System.out.println("Digits: " + digit + " Upper: " + upper + " Lower: " + lower + " Other: " + other);

If character-by-character reading is required, read into an int, check for -1 first, and explicitly handle '\r' and '\n' so Windows CRLF doesn’t confuse the loop:

int r;
while ((r = br.read()) != -1) {
    if (r == '\n') break;    // Enter
    if (r == '\r') continue; // ignore CR on Windows
    char c = (char) r;
    // classify c...
}

Notes for debugging: print the numeric values of what read() returns to see CR/LF/EOF values; remember do/while always executes its body once; and was correct to point toward a newline-based exit, but using readLine() or checking int with -1 is safer and portable. ’s suggestion to describe observed behavior is useful for reproducing environment-specific issues.

Recommended Answers

All 2 Replies

What errors do you get and how does the code behave? What does it do that it is not suppose to?

Try

while(ch != '\n');

in the while condition because you do not have any space in your input stream

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.