1 2 Previous Next 28 Replies Latest reply: Jan 21, 2009 4:24 PM by 807588 RSS

    Entering a non-numeric character into an if statement...

    807588
      Hi guys,

      I've created an if statement which produces these outputs....

      if you enter 0, you get an output of +"zero"+.
      if you enter any number over 0, you get an output of +"numeric"+.
      if you enter a non-numeric character, you get an output of +"non-numeric"+.

      It works when i enter in numeric characters, but when i enter in a non-numeric character, like h, i get a "Error: cannot find symbol - variabal h" on the object. I've written it in brackets and it still won't work.

      Can anyone give me hint where i'm going wrong?

      Here's my code...
      public class Numbers
      {
          private char c;
          
          /**
           * Constructor for objects of class Numbers
           */
          public Numbers()
          {
              // initialise instance variables
              char c = 1;
          }
      
          /**
           * Enter value...
           */
          public void getValue(char c)
          {
              if(c < 1) {
                  System.out.println("Zero");
              }
              else if (c >= 1) {
                  System.out.println("Number");
              }
              else {
                  System.out.println("Non-numeric");
              }
          }
      }
        • 1. Re: Entering a non-numeric character into an if statement...
          807588
          why are you using char here? Don't you want to use int? This may involving parsing a String to an int using Integer.parseInt(...) in a try/catch block.
          • 2. Re: Entering a non-numeric character into an if statement...
            807588
            I think you may be confusing chars like '0' with their unicode values:
            public class Example {
                public static void main(String[] args) {
                   char ch = '0';
                   System.out.println(ch == 48); //true!
                }
            }
            • 3. Re: Entering a non-numeric character into an if statement...
              807588
              Encephalopathic wrote:
              why are you using char here? Don't you want to use int? This may involving parsing a String to an int using Integer.parseInt(...) in a try/catch block.
              Hi Encephalopathic,

              The reason why i used char here...
              public void getValue(char c)
              ...is because char can use any character, so i assumed that char would be appropriate, instead of int.

              I used int before and it wasn't working, so i assumed that char would make it work (and that's not the case).
              • 4. Re: Entering a non-numeric character into an if statement...
                807588
                BigDaddyLoveHandles wrote:
                I think you may be confusing chars like '0' with their unicode values:
                public class Example {
                public static void main(String[] args) {
                char ch = '0';
                System.out.println(ch == 48); //true!
                }
                }
                Hi BigDaddyLoveHandles,

                When you say that do you mean that i think they can use any character?
                • 5. Re: Entering a non-numeric character into an if statement...
                  807588
                  Just a thought, it seems to me that you are rather miss thinking the problem. That might be because of the way the assignment in your class is set up, or it might be because you didn't understand or any number of possible reasons. My advice is to set the solution aside for a moment and think about the problem.

                  0. A user enters a value (Area there any limits on WHAT they can enter?)
                  1. What is the most restrictive case? This is the one that will catch MOST of the passes through.
                  2. What are the rules that make that case true or false?
                  3. What is the next most restrictive case?
                  4. What are the rules that make that case true or false?
                  5. Do any of those rules conflict with previous cases?
                  6. Repeat until there are no cases left.

                  So what are the possible cases?

                  0. Numeric value entered
                  1. Numeric value of 0 entered (Though less frequently encountered than the next one it's actually a sub case of 0)
                  3. Non-numeric value entered

                  So now ask for each case what are the rules?

                  0. Numeric value (How do we determine if a value is numeric?)
                  1. Numeric value of 0 (For this to be true case 0. must also be true)
                  2. Guess what if it doesn't fall into case 0 or case 1 it falls here.

                  By now the code is practically writing itself.

                  Hope this helps,

                  PS.
                  • 6. Re: Entering a non-numeric character into an if statement...
                    807588
                    puckstopper31 wrote:
                    Just a thought, it seems to me that you are rather miss thinking the problem. That might be because of the way the assignment in your class is set up, or it might be because you didn't understand or any number of possible reasons. My advice is to set the solution aside for a moment and think about the problem.

                    0. A user enters a value (Area there any limits on WHAT they can enter?)
                    1. What is the most restrictive case? This is the one that will catch MOST of the passes through.
                    2. What are the rules that make that case true or false?
                    3. What is the next most restrictive case?
                    4. What are the rules that make that case true or false?
                    5. Do any of those rules conflict with previous cases?
                    6. Repeat until there are no cases left.

                    So what are the possible cases?

                    0. Numeric value entered
                    1. Numeric value of 0 entered (Though less frequently encountered than the next one it's actually a sub case of 0)
                    3. Non-numeric value entered

                    So now ask for each case what are the rules?

                    0. Numeric value (How do we determine if a value is numeric?)
                    1. Numeric value of 0 (For this to be true case 0. must also be true)
                    2. Guess what if it doesn't fall into case 0 or case 1 it falls here.

                    By now the code is practically writing itself.

                    Hope this helps,

                    PS.
                    Okay, let me read this and see if i can solve the problem i'm having.

                    Thanks puckstopper31.
                    • 7. Re: Entering a non-numeric character into an if statement...
                      807588
                      Okay puckstopper31,

                      I've read what you worte and i think it isn't as deep as you might think it is.

                      It's basically if you enter 0 you get a printout of "Zero".

                      If you enter any other number (no matter how long), you get a printout of "Numeric".

                      If enter any other character that is not numeric (like A or Z), you get a printout of "Non-Numeric"

                      The first two works, but i can't get the third to work....maybe it's the data type, not sure (all i know is i have to use char for c).
                      • 8. Re: Entering a non-numeric character into an if statement...
                        807588
                        What value is the code which reads the input returning when you input '0'?
                        • 9. Re: Entering a non-numeric character into an if statement...
                          807588
                          pm_kirkham wrote:
                          What value is the code which reads the input returning when you input '0'?
                          it is c

                          Here's the part..
                           public void getValue(int c)
                              {
                                  if (c < 1) {
                                      System.out.println("Zero");
                                  }
                                  else if (c >= 1) {
                                      System.out.println("Number");
                                  }
                                  else {
                                      System.out.println("Non-numeric");
                                  }
                              }
                          • 10. Re: Entering a non-numeric character into an if statement...
                            800282
                            LevelSix wrote:
                            ...
                             public void getValue(int c)
                            {
                            // snip
                            else {
                            System.out.println("Non-numeric"); // that will never happen
                            }
                            }
                            (see my comment in your code)

                            I have no idea what you are really trying to do. But it seems you had better look at the various methods in the Character class instead of what you're doing now.

                            [http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Character.html#isDigit(char)]
                            • 11. Re: Entering a non-numeric character into an if statement...
                              807588
                              What we're asking is where does c come from when you pass it to getValue()? And, by the way an int is bound to be either < 1 or >= 1, and therefore the third case can't happen whatever value c is.
                              • 12. Re: Entering a non-numeric character into an if statement...
                                807588
                                prometheuzz wrote:
                                LevelSix wrote:
                                ...
                                 public void getValue(int c)
                                {
                                // snip
                                else {
                                System.out.println("Non-numeric"); // that will never happen
                                }
                                }
                                (see my comment in your code)

                                I have no idea what you are really trying to do. But it seems you had better look at the various methods in the Character class instead of what you're doing now.

                                [http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Character.html#isDigit(char)]
                                Okay, i'll look at it and get back to you.
                                • 13. Re: Entering a non-numeric character into an if statement...
                                  807588
                                  malcolmmc wrote:
                                  What we're asking is where does c come from when you pass it to getValue()? And, by the way an int is bound to be either < 1 or >= 1, and therefore the third case can't happen whatever value c is.
                                  Not sure what you mean, but c comes from the char.

                                  Okay, here's the question i'm trying to answer...

                                  Assuming that a variable has been declared like this:*

                                  char c;*

                                  and that a value has already been assigned to c,*
                                  write an if statement that will print the message*
                                  +"Zero" if it is '0',+
                                  +"Number" if it is one of the other numeric characters and+
                                  +"Non-numeric" if it is not numeric+
                                  • 14. Re: Entering a non-numeric character into an if statement...
                                    807588
                                    So, problem solved?
                                    1 2 Previous Next