The Artima Developer Community
Sponsored Link

Java Answers Forum
tricky problem with a code

5 replies on 1 page. Most recent reply: Jun 26, 2003 8:47 PM by gaurav kumar

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 5 replies on 1 page
gaurav kumar

Posts: 9
Nickname: tomar
Registered: Jun, 2003

tricky problem with a code Posted: Jun 26, 2003 10:03 AM
Reply to this message Reply
Advertisement
following code is not working properly;
-----------------
the code calculates uppercase,lowercase,digits and other symbols in a string
-----------------------------
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("\n\n Type some text. When done, press Enter to Quit: ");

ch=(char) inputstream.read();

while(ch !='\n')
{

if(Character.isDigit(ch))
digit++;
else if(Character.isUpperCase(ch))
upper++;
else if(Character.isLowerCase(ch))
lower++;
else
other++;
ch=(char) inputstream.read();
}


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

}
}
----------------------------------------------------------
when the above code is made to run
it always displays the value of 'other' one more than it actually is
i.e. if i inputs---'123asdASD!@#'
the result is--digit=3
lowercase=3
uppercase=3
other=4
iam not able to figure it out
can anyone explain it to me
thanks!


FEMI FASHAKIN

Posts: 3
Nickname: fash1
Registered: Jun, 2003

Re: tricky problem with a code Posted: Jun 26, 2003 10:51 AM
Reply to this message Reply
I looked hard into your code, and it looked fine to me. However, i will suggest that you insert print statements after every increment(counter) in your while loop just to see the values of your counters after each loop.This might make your result clumsy,nevertheless, it should make it easier for you to debug.

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: tricky problem with a code Posted: Jun 26, 2003 12:06 PM
Reply to this message Reply
Maybe you are counting the '\r' part of a '\r\n' sequence at the end of the line?

Also, along the lines of the last suggestion, you could also collect the results for each category and show not just the count, but also the characters that are comprise it. That is your output might be something like:

There were 3 lowercase characters: 'a', 's', 'd'
There were 3 uppercase characters: 'A', 'S', 'D'
...

Nicholas Partridge

Posts: 2
Nickname: nkpart
Registered: Jun, 2003

Re: tricky problem with a code Posted: Jun 26, 2003 4:08 PM
Reply to this message Reply
I had a very similar problem recently, which I never solved :P.

I think the problem was as Matt described with the '/r/n' sequence, but I'd do a print of each character to be sure.

Mr Plow

Posts: 18
Nickname: mrplow
Registered: Jun, 2003

Re: tricky problem with a code Posted: Jun 26, 2003 5:09 PM
Reply to this message Reply
The two replys above are correct. The problem is that there is a trailing '\r' character attached to the end of the inputstream.

I have modified the original code below to
1) print out each char
2) break the loop if it encounters a '\r' character

Cheers.



import java.io.*;

public 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("\n\n Type some text. When done, press Enter to Quit: ");

ch=(char) inputstream.read();

//while(! Character.isWhitespace(ch))
while(ch != '\r' && ch != '\n')
{

if(Character.isDigit(ch))
{
digit++;
System.out.println("Digit: " + ch);
} else if(Character.isUpperCase(ch)) {
upper++;
System.out.println("Upper: " + ch);
} else if(Character.isLowerCase(ch)) {
lower++;
System.out.println("Lower: " + ch);
} else {
other++;
System.out.println("Other: " + ch);
}//end of else
ch=(char) inputstream.read();
}//end of while


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

}//end of main

}//end of class

gaurav kumar

Posts: 9
Nickname: tomar
Registered: Jun, 2003

Re: tricky problem with a code Posted: Jun 26, 2003 8:47 PM
Reply to this message Reply
thanks to all of u
the problem was really in the '\r' part.
it was really reading the '\r' character also which was inputed into the stream when i pressed 'return' after writing my input string.
thanks again.

Flat View: This topic has 5 replies on 1 page
Topic: how to resize window to fit viewers screen resolution ?? Previous Topic   Next Topic Topic: setLocation Dialog for Center

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use