Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Why does my code always print out true.

  1. #1
    Member
    Join Date
    Apr 2020
    Posts
    147
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Why does my code always print out true.

    Hello!

    Consider this code

     
    import java.util.Arrays;
    public class test {
     
    	static boolean isDigit(char ch) {
    		if( '0' <= ch  && ch <='9') {
    			Out.println("true");
    			return true;
    		} else {
    			Out.println("false");
    			return false;
    		}
    	}
     
     
     
    	public static void main (String[] args) {
    		Out.println("Put in a digit");
    		char ch = In.readChar();
    		isDigit(ch);
     
     
    	}
     
     
    }

    Now the goal is to print out true whenever we input a number between 0 and 9,and for every other number we want to print out false.But for some reason it is always printing out true,no matter what number I put in.Worth mentioning is that the numbers are not int but char.Why does it print out true even for values of 120 ?

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Why does my code always print out true.

    What is the value in the variable: ch?
    Print it to see. In case the value is not a printable character cast ch to int:
       System.out.println("ch=" + (int)ch + " " + ch);  // print ch to see value
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Apr 2020
    Posts
    147
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Why does my code always print out true.

    At what point in the code do I want to see the value of ch? When the user inputs the value?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Why does my code always print out true.

    Since it is never changed, any place would work.
    Probably immediately after it is assigned a value would be good.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Apr 2020
    Posts
    147
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Why does my code always print out true.

    Okay,very very interesting results;

    for 12;

    12
    ch=49 1
    true

    for 8

    8
    ch=56 8
    true

    for 222

    222
    ch=50 2
    true


    The code

     
    import java.util.Arrays;
    public class test {
     
    	static boolean isDigit(char ch) {
    		if( '0' <= ch  && ch <='9') {
    			Out.println("true");
    			return true;
    		} else {
    			Out.println("false");
    			return false;
    		}
    	}
     
     
     
    	public static void main (String[] args) {
    		Out.println("Put in a digit");
    		char ch = In.readChar();
    		System.out.println("ch=" + (int)ch + " " + ch);
    		isDigit(ch);
     
     
    	}
    }

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Why does my code always print out true.

    Do you see what the value of ch is? It is a single character. Only the first char of a group that is entered is returned by the readChar method.

    I didn't know what value readChar would return so I cast the ch to int to see its value.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Apr 2020
    Posts
    147
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Why does my code always print out true.

    Yea I see it as well, the values of the char are single digit that is why its always printing true.Which means my logic wasnt at fault but the readChar() method.Thank you for your help,I'll adjust this with the method.

Similar Threads

  1. Print just what I want, help me with my code.
    By JosPhantasmE in forum What's Wrong With My Code?
    Replies: 44
    Last Post: January 22nd, 2013, 07:44 PM
  2. Replies: 1
    Last Post: December 3rd, 2012, 02:35 PM
  3. letterGrade not print out on my code
    By troj4nk1ng in forum What's Wrong With My Code?
    Replies: 4
    Last Post: July 8th, 2011, 08:31 PM
  4. while(true){ section of code runs 200 times}
    By jack_nutt in forum Java Theory & Questions
    Replies: 7
    Last Post: June 23rd, 2011, 07:06 AM
  5. while(true){ section of code runs only once}
    By jack_nutt in forum Java Theory & Questions
    Replies: 5
    Last Post: June 19th, 2011, 06:15 PM