5 Replies Latest reply: Nov 26, 2006 1:35 PM by 807599 RSS

    2 related ?'s: Searching char array for decimal and

    807599
      I'm fairly new to Java so hopefully my programming doesn't seem too unconventional. I have been tasked with creating a mortgage calculator for a class I am taking. Very simple, really. I am trying to validate user input to make sure that only numbers with or without decimals are allowed as valid.

      Here is the code for validating the input:
           //Validate data 
           public boolean validateIsNum(String s)
           {
                char[] inArray = s.toCharArray();
                int valLength = (inArray.length);
                int x = 0, y = 0, decimal;
      
                for (int counter = 0; counter < valLength; counter++)
                     if (Character.isDigit(inArray[counter]))
                     {
                          x++;
                     }
                     else
                     {
                          y++;
                     }
      
                Arrays.sortArray(inArray);
                decimal = Arrays.binarySearch(inArray, ".");
      
                if (y > 0)
                {
                     System.out.println("Enter value only.  Do not add extra characters.");
                     return false;
                }
                else
                     return true;
      
           }
      The main question I have is based on:
                Arrays.sortArray(inArray);
                decimal = Arrays.binarySearch(inArray, ".");
      Is that the correct way to search for a decimal. If I just enter a decimal I get an error indicating the incorrect start of an expression or something of that matter.

      If you are wondering where I am going with this I was thinking if a decimal was found, since it is not a digit detectable by isDigit(); I would simply subtract 1 from my "y" count to keep the data valid. Right now the program works great but does not support decimal input.

      Also, right now when I try to compile I get:
      C:\My Stuff\Private\School\POS406\Week 2\MortgageCalc.java:48: cannot resolve symbol
      symbol  : method sortArray (char[])
      location: class java.util.Arrays
                Arrays.sortArray(inArray);
                            ^
      C:\My Stuff\Private\School\POS406\Week 2\MortgageCalc.java:49: cannot resolve symbol
      symbol  : method binarySearch (char[],java.lang.String)
      location: class java.util.Arrays
                decimal = Arrays.binarySearch(inArray, ".");
                                      ^
      2 errors
      I have been reading up on the Java collections, which is where binarySearch apparently contained, but I am still unsure what I have to do to get this to work. A search of my computer came up with no such binarySearch.class file.

      Any help would be appreciated.

      Also, I realize that I may be re-creating the wheel here for user validation of this type. I would love to solve this problem since I just spent 10 hours working on it but if you want to shoot me a link as well I can definetly use something more refined in later projects.

      Thanks!

      Kirk