12 Replies Latest reply: Dec 10, 2008 9:50 AM by 807589 RSS

    Trying to check if the next character is a number.... NEED HELP!!

    807589
      Okay so I am doing a molecular weight problem. I thought I had it down pack but I am having an issue with the charAt() method. Here is what I have right now.

      String value = "";
      try
      {value = request.getParameter("equation");}
      catch(Exception e) {}

      double count = 0.0;
      double idk = 0.0;
      String elem = "";

      **for (int i = 0; i < value.length(); i++)**
      **{**
      **int k = i +1;**
      **char nextchar = value.charAt(k);**
      **char c = value.charAt(i);*if (( c >= 'A') && (c <= 'Z'))*
      *{* if (( nextchar ?= 'a') && (nextchar <= 'z'))
      {
      String newc = String.valueOf(c);
      String newnextchar = String.valueOf(nextchar);
      elem = newc + newnextchar;
      for (int j = 0; j < 111; j++)
      {
      if ( element[j] == elem)
      { count = count + weight[j];}
      }
      i++;
      }
      else if (nextchar >= '1') && (nextchar <='9'))
      {
      String b = String.valueOf(nextchar);
      idk = Double.parseDouble(b);
      for(int j=0; j<111); j++)
      {
      if (element[j] == elem)
      {
      idk = weight[j] * idk;
      }
      }
      }
      else
      {
      for( int j = 0; j<111; j++)
      {
      if ( element[j] == elem )
      {
      count = count + weight[j];
      }

      }}}}}


      Of course there is more code but this is the basic part that I am having trouble. I haven't even been able to successfully run it. The part that is giving me the ttrouble is in bold. I have tried everything that I know how to... If someone could help me then that would be great. I have already done a google and yahoo search. The problem is not being prodduced in my IDE. This problem occurs when I load the jsp.
        • 1. Re: Trying to check if the next character is a number.... NEED HELP!!
          800282
          Have a look at the various (static) methods of the Character class: [http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Character.html]
          • 2. Re: Trying to check if the next character is a number.... NEED HELP!!
            807589
            char nextchar = value.charAt(k);
            This is going to throw an out of range exception on the last character of the value string, because it tries to get one past the end.

            Use Character.isLetter(ch);
            • 3. Re: Trying to check if the next character is a number.... NEED HELP!!
              807589
              Wouldn't I have the same issue as with charAt? I am already focused a a letter. For instance, the user types in H2O. So I have my the value at char c = value.charAt(i); would be H. Then I want to see what is at the index i + 1. It would be a 2 so I change the to a double and I multiple 2 times 1.00794. Then I skip the too and move to the O then the I check the next character to see if it is a lowercase/uppercase letter or a digit. But there is nothing there so I just add the value of O 15.9994 to the running count. But I think the issue is that maybe i+1 is not a valid arguement in the method charAt...

              what do you think?
              • 4. Re: Trying to check if the next character is a number.... NEED HELP!!
                800282
                NotYetAPro wrote:
                Wouldn't I have the same issue as with charAt? I am already focused a a letter. For instance, the user types in H2O. So I have my the value at char c = value.charAt(i); would be H. Then I want to see what is at the index i + 1. It would be a 2 so I change the to a double and I multiple 2 times 1.00794. Then I skip the too and move to the O then the I check the next character to see if it is a lowercase/uppercase letter or a digit. But there is nothing there so I just add the value of O 15.9994 to the running count. But I think the issue is that maybe i+1 is not a valid arguement in the method charAt...

                what do you think?
                I must say that your original post, the code in there and your latest reply, are all very vague, in my opinion. I answered the question/requirement you posted in the title of your thread: "Trying to check if the next character is a number [...]" which can be done with
                char c = '7';
                boolean digit = Character.isDigit(c); // returns true
                If you have some other question, please ask it.
                • 5. Re: Trying to check if the next character is a number.... NEED HELP!!
                  807589
                  NotYetAPro wrote:
                  what do you think?
                  I think the index value you use in String.charAt() must be less than String.length().
                  • 6. Re: Trying to check if the next character is a number.... NEED HELP!!
                    807589
                    If my initial request was vague, I am am sorry. But the response that I gave walked through the problem and actually gave a detailed idea of what it is that I am trying to accomplish. I do not know how to make it clearer. But thanks for your help...
                    • 7. Re: Trying to check if the next character is a number.... NEED HELP!!
                      800282
                      NotYetAPro wrote:
                      If my initial request was vague, I am am sorry.
                      No problem.
                      NotYetAPro wrote:
                      But the response that I gave walked through the problem and actually gave a detailed idea of what it is that I am trying to accomplish.
                      Err, I disagree. You didn't tell what "the problem" or "the issue" actually is: runtime errors? If so, which ones?
                      Also, there are suddenly some numbers in your second reply, what are they? What is "the total count", is it a total count of "H2O" only, or from all the strings the user enters?

                      And did you understand what malcolmmc was talking about (out of bounds exception)?
                      NotYetAPro wrote:
                      I do not know how to make it clearer. But thanks for your help...
                      Then, I can't help. Good luck though.
                      • 8. Re: Trying to check if the next character is a number.... NEED HELP!!
                        800308
                        I do not know how to make it clearer.
                        Use &#123;code} tags (for starters)... and maybe try explaining stuff better.
                        • 9. Re: Trying to check if the next character is a number.... NEED HELP!!
                          807589
                          Firstly if your not gonna be helpful then maybe u should try not responding... It's not your job (and in my opinion totally childish) to try to berate someone because you don't understand. IF YOU CAN'T HELP THEN DON'T RESPOND.... I am not a student doing a project for school I am trying to better understand a mistake I made. corlettk: Would the code tags help you understand it better... NO!! So don't be a dimwitt....

                          This a molecular calulator. So the numbers were me running through an example of how I would hope this works. It is only computing the Equation entered at that time. One equation at a time... No whitespaces. The problem that I am having is with the charAt() method. I did not give the error message because it wouldn't be helpful. But the error message says... The server encountered an internal error() that prevented it from fulfilling your request. That stuff that is bold in my initial post is where it is saying that the problem was encountered....


                          I agree with him about the exception. So my issue is that I don't know how to fix the problem.... or how to use something different....
                          • 10. Re: Trying to check if the next character is a number.... NEED HELP!!
                            800282
                            NotYetAPro wrote:
                            Firstly if your not gonna be helpful then maybe u should try not responding... It's not your job (and in my opinion totally childish) to try to berate someone because you don't understand. IF YOU CAN'T HELP THEN DON'T RESPOND.... I am not a student doing a project for school I am trying to better understand a mistake I made. corlettk: Would the code tags help you understand it better... NO!! So don't be a dimwitt....

                            ...
                            Christ man, chill. Posting your code with code tags DOES help those who want to help you. I'm not interested in helping those who bite the hand that (can) feed them.

                            Good luck.
                            • 11. Re: Trying to check if the next character is a number.... NEED HELP!!
                              802908
                              Hi,

                              i think i understand the requirement. H2O is water and its molecular weight is 2*weight(H)+weight(O)
                              (where weight(H)=1,00794 and weight(O)=15,9994) so the desired output is 18,01528.

                              Ok, but as far as i understand you said your real problem is not in the IDE, but when you load your JSP page.
                              So in the IDE everything compiles and works well (produces the correct output). Correct?
                              But in JSP it does not? So i guess its not a problem with the charAt() function but rather some kind of JSP problem?!
                              I'm not very familiar with JSP i hope there is somebody here who can shed some more light on this issue than i can.

                              friendly greetings

                              BB
                              • 12. Re: Trying to check if the next character is a number.... NEED HELP!!
                                807589
                                NotYetAPro wrote:
                                But I think the issue is that maybe i+1 is not a valid arguement in the method charAt...

                                what do you think?
                                Definitely. You need to special-case the last time around the loop and deal specially with the case that there is no next character.

                                Instead of looking ahead, I'd keep the last info read. For example you could have a Character variable for the last element read (if any) and an Integer variable if you were in the process of accumulating a number.

                                So, one leg might look like:
                                if(Character.isLetter(ch)) {  // new element
                                   if(element != null)  {
                                       processElement(element.charValue(), count == null ? 1 : count.intValue());
                                      
                                   }
                                   element = Character.valueOf(ch);
                                    count = null;
                                   } else if(Character.isDigit(ch)) { // finding a digit
                                   ...
                                
                                  }