IsDigit / digit in Character class - ouput ?

Can somebody help me in knowing why such an output is coming while I use forDigit / digit from Character class?
The output that I get is:
The for Digit is
The digit is -1
Note : There is a blank space after the is there.
class chard
     public static void main(String[] args)
          int a = 66;
          char ch1 = 66;
          System.out.println("The for Digit is" + Character.forDigit(a, 2));
          System.out.println("The digit is " + Character.digit(ch1, 2));
}

Why aren't you reading the documentation????
http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Character.html
digit(char ch, int radix)
"Returns the numeric value of the character ch in the specified radix.
If the radix is not in the range MIN_RADIX <= radix <= MAX_RADIX or if the value of ch is not a valid digit in the specified radix, -1 is returned. A character is a valid digit if at least one of the following is true: "
How do you expect value 66 which is the same as 'B' to be a valid value in base 2?
/Kaj

Similar Messages

  • Regular Expressions Character Class shortcuts

    I have been learning to use regular expressions to modify some of my text files. I noticed that on my ARCH box the Character Class shortcuts do not work e.g. [[:digit:]] in an expression works but \d does not. Is this normal or is my installation broken in some way?

    Bebo wrote:
    There are several regexp "dialects". It's quite painful actually For instance, as far as I know, \d works in perl, but not in sed or grep.
    So, yes, this is normal.
    Yeah -- Henry Spencer's regexp stuff is always generally considered the portable form for sed, awk, since they're all based from it.  Newer versions of grep though do allow for a -P flag for perl-regexps to be used, but this is non-portable, obviously.
    -- Thomas Adam

  • Regular Expression back-reference in character class

    I am trying to capture quoted text (excluding the quotes) with the following pattern:
    "(['\"])([^\\1]+)\\1"
    The input string might look like:
    "That's strange"
    or:
    'valid "regex" pattern'
    I get an exception when trying to compile the pattern, because of the back-reference within character-class brackets: [^\1]
    This pattern worked with the org.apache.oro package. Is this a bug in the 1.4 Pattern class? Can you offer an alternative solution?
    Thanks,
    Tony

    Thanks for the reply. Apparently, my pattern wasn't working as I thought with ORO. The pattern you suggested, however, wouldn't do what I need either, because it would match anything between the first and last quote character. I don't want to match the string you included in your reply, because it includes the same quote character that encloses the entire string. If you think about my pattern again, you'll notice that the first capture group should match either a single or double quote character. Then the back-reference should be the exact character (single character) that was matched and captured. So the second capture group should be any number of characters, as long as they don't match the first capture group character, followed by the exact string matched in the first capture group (the single character). You probably are aware that when one or more characters are included in square brackets [], that it specifies a match of a single character that is found anywhere within that character class. So if the first capture group happened to match more than one character (impossible due to the pattern in that grouping), the following character class [^\1] should match any character EXCEPT any of the ones matched in the first capture group...right? There must be a bug in ORO that doesn't match correctly per my description, but it seems there is a bug in JDK 1.4 Pattern that will not even compile it. Just to re-iterate...if \1 matched "abc", [^\1] should match any single character EXCEPT a or b or c. I can get what I want, though, if I do it in 2 matches:
    Pattern pat = Pattern.compile("(['\"])(.*)");
    Matcher mat = pat.matcher(sText);
    mat.matches();
    String sQuote = mat.group(1);
    String sRem = mat.group(2);
    pat = Pattern.compile("([^" + sQuote + "]*)" + sQuote);
    mat = pat.matcher(sRem);
    mat.matches();
    String sWhatIWanted = mat.group(1);
    Thanks,
    Tony

  • Unclosed character class near index 0

    Hi foks,
    I am tryin to remove few characters from a string with the help of replaceAll ( String, String ) method.
    But at the first replacement itself it gives error "Unclosed character class near index 0".
    what does this error mean? I dont have any ' [ ' character in that string. It used to be there, but now I am removing all the occuring of the ' [ ' char right at the creation of string.
    Thanks in advance.

    Hi,
    Here is the code.
    [ CODE ]
    public static String extract(String para)
    String definition = para.substring(para.indexOf("<ol>"),para.indexOf("</ol>"));
    StringBuffer meanings=new StringBuffer();
    StringReader reader = new StringReader(definition);
    StringWriter writer = new StringWriter();
    boolean append=false;
    int ch;
    try
    while((ch=reader.read()) != -1)
    if(ch=='<' || ch=='&')
    append = false;
    continue;
    else if(ch=='>' || ch==';')
    append = true;
    continue;
    if(append && ch != '[' && ch != ']')
    writer.write(ch);
    catch(IOException e)
    System.out.println("IOException:"+e.getMessage());
    catch(Exception e)
    System.out.println("IOException:"+e.getMessage());
    meanings=writer.getBuffer();
    String temp = meanings.toString();
    System.out.println("temp:"+temp);
    temp=temp.replaceAll("["," ");
    System.out.println("temp:"+temp);
    temp=temp.replaceAll(" "," ");
    System.out.println("temp:"+temp);
    return temp;
    [ /CODE ]

  • Changing from upper to lower case without character class

    hi all,desperatley need help.
    I need to create an algorithm as follows
    the implement into a program without using character class
    i have no idea how to go about this
    Create an algorithm for a program that continually reads (loops) a single character from the user and displays the ordinal (ASCII) value of the character. The program should then change the case of the character, so if it is an 'a' change to an 'A' and vise-versa and then display the new ordinal value. The program should quit when the user enters the '#' character. The program should display an error message if an invalid character is entered.
    Implement the program from question 1 into a java program. You are not allowed to use any of the methods provided in the Character class to implement your solution
    regards Paul

    How time flies...they are already assigninghomework
    for the fall term.Yeah, the nice thing about the summer is that we don't
    get as many homework problems on the forums. Oh
    well...I love how they don't even take the time to reword the hw and instead just post the prof's exact text.
    "I need help with a problem, its Ch. 9, Q 23a, can anyone help me?"

  • Regex character classes

    You can match ']' in a regex character class by specifying it as the first literal
    "[]]"How do you match '[' in a regex character class?
    Thanks in advance, Mel

    sabre150 wrote:
    Escape it as in
    "[\\[]"Check the Javadoc for Pattern.I swear i initially tried that... thanks sabre

  • PatternSyntaxException: Unclosed character class near index

    Hi,
    I want to replace in a string a expression like "^1:2" by "^(1/2)":
    For example, "V/Hz^1:2" would be converted to "V/Hz^(1/2)".
    I tried the following code:
    String oldExponent = "[^](\\d+):(\\d+)"; // e.g. ^1:2
    String newExponent = "^($1/$2)";         // e.g. ^(1/2)
    myString = myString.replaceAll(oldExponent, newExponent);but the following exception is thrown in the third line:
    java.util.regex.PatternSyntaxException: Unclosed character class near index 13
    [^](\d+):(\d+)
                 ^Any idea?
    Thanks in advance.

    Thank you, jverd, you are pretty right.
    Now I tried with
    "\\^(\\d+):(\\d+)"and it works.
    I tried to avoid ^ being interpreted as the beginning of the line, and didn't realize the interpretation inside the brackets.
    Escaping this character works well.

  • PR Release Strategy- transport of charact/class/ values using ALE

    Does anyone have the information relative to ALE set up for release strategy-
    I am using transactions BD91 for characteristics; BD 92 for Class and BD 93 for the actual values into release strategy.
    I need the information as to what needs to be set up in Development client and receiving clients ( Q and Prod) for these transactions i.e., RFC destinations, define ports, Identify Message types & setup partner profile configurations etc
    This will be a great help.
    Thanks
    Raj

    Hi Raj,
    Please check the below notes for more inrofmation:
      86900 -  Transport of Release strategies (OMGQ,OMGS)
      799345    Transport of release strategy disabled
      10745     Copying classification data to another client
      45951     Transporting class data: system / client. - has details of what you are looking for.
    Hope this helps.
    Regards,
    Ashwini.

  • Match string against regex or character class in Applescript

    Hello,
    In my script i get string from user and need to ensure that string contains only alphanumerical characters. There is a sample:
    #!/bin/bash
    REGEX="^[[:alnum:]]*$"
    osascript <<EOF
    tell application "SystemUIServer"
    repeat
    set username to text returned of (display dialog "Enter your name" with icon caution default answer ""  buttons{"Continue"})
    if text returned of (do shell script "if ! [[ " & quoted form of username & " =~ $REGEX ]]; then echo \"notok\"; fi") as text is equal to "notok" then
    display alert "WRONG CHARACTER"
    else
    exit repeat
    end if
    end repeat
    end tell
    EOF
    The error:
    execution error: Can▒t make text returned of "notok" into type text. (-1700)
    How can one fix this? Is it possible to do this in pure AS without invoking shell?

    Follow Tony's advice.
    Yes, you can code it entirely in AppleScript, but is a verbose beast, none the least, do to the lack of an AppleScript range operator that would permit range('A'..'z') and avoid arduous lists.
    Code:
    set goodStr to "cAt134"
    set badStr to "cAt_134"
    if not isalnum(badStr) then
      display dialog "Not ok!"
    end if
    on isalnum(username)
              set uCase to {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", ¬
                        "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", ¬
                        "U", "V", "W", "X", "Y", "Z"}
              set lCase to {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", ¬
                        "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", ¬
                        "u", "v", "w", "x", "y", "z"}
              set nbr to {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"}
              set alnum to uCase & lCase & nbr
              set status to true as boolean
              repeat with charptr from 1 to count of username
                        set current_char to item charptr of username
      -- log current_char
                        if current_char is not in alnum then
                                  set status to false
      -- display dialog current_char
                                  exit repeat
                        end if
              end repeat
              return status
    end isalnum

  • TOLERANCE SETTINGS FOR BATCH/CHARACTER/CLASS

    Hi gurus
    A material with a alternate unit conversion like Kg and Pair, suppose material with 1 pair equal to 40kg as standard. We set a batch mgmt to this material,
    Here i have to set a tolerance limit with +or - 8% reference to 40 kg.
    Where i have to set the tolerance , help me out
    Chris

    Hi,
    All material data is updated in the base unit of measure. The tolerances can be had on the Base unit of measure.
    So, choose this unit carefully since an exact quantity can be expressed in an alternative unit of measure only if its value can be shown with decimal places. It is therefore important to observe the following two principles when defining the base unit of measure:
    - The base unit of measure is the unit that provides the maximum precision necessary.
    - Conversion from an alternative unit of measure to the base unit of measure should result in a simple decimal, not a recurring (or repeating) decimal.
    Regards,
    Narayana.

  • Regular Expression Escaped Digit "\d" Illegal Escape Character

    Hello,
    I'm trying to write a regular expression to determine if a String matches a date format that is defined as YYYYMMDD. For example, March 11, 2009 would be "20090311"
    For the time being I don't care if an invalid month or day is entered. I've attempted both of the following
    if (date.matches("(19|20)\d{4}")) {
      // warn the user
    }and
    if (java.util.regex.Pattern.matches("(19|20)\d{4}"), date)) {
      // warn the user
    }And both yield Illegal Escape Character compilation errors, for the "\d" part of the regular expression.
    http://java.sun.com/j2se/1.4.2/docs/api/java/util/regex/Pattern.html#sum
    Says that "\d" is the predefined digit character class. So at this point, I don't know what I'm doing wrong. I realize I could just define the character class myself, and use a pattern like "(19|20)[0-9]{4}", but I would like to know why "\d" isn't being recognized by the compiler.
    Thanks,
    Paul

    paulwooten wrote:
    Can someone give me an explanation of heuristics, as they might apply to SimpleDateFormat? Does this mean that if the format was similar the parser might figure it out? Say, if instead of "yyyyMMdd", it was "yyyyddMM", or "yyMMdd"?No. Since all of these are valid formats, there's no way for the parser to distinguish this.
    Or does this have to do with rejecting February 29, and other dates like that.That's the one. When setLenient(false) is called, then the 29th February is only accepted in leap years.
    It will also reject the 57th January when lenient is set to false (try parsing that with lenient=true, you'll be surprised).
    I've read some of the wikipedia article about heuristics, but I'm confused as to how it would apply to this example.Don't concentrate to much on the term heuristics. Just remember: lenient=true means that not-really-correct dates will be accepted, lenient=false means more strict checks.

  • Weird character

    Hi all
    I am kinda new to Java!
    I have an anemic class (defines no behaviour) that has a field of primitive type char. I know that fields are always initialized even if don´t do it, so I created this class to find out what character java assigns to a character field.
    I didn´t find it out yet!
    The code is working and as a shortcut i prefer to run it in the command prompt by typing:
    javac CharacterTest.java
    java CharacterTest (Here you have to omit the extension)
    /******************************************************************BEGGINING OF THE CLASS**********************************************************************/
    public class CharacterTest {
         public static void main(String args[]) {
              System.out.println("AnemicClass.caractere " + Test.caractere);
              boolean isWhiteSpace = (AnemicClass.caractere == ' ')? true: false;
              System.out.println("Is AnemicClass.caractere field a whitespace using primitive type char comparison? " + isWhiteSpace);
              String character = String.valueOf(AnemicClass.caractere);
              isWhiteSpace = (" ".equals(character))? true: false;
              System.out.println("Is AnemicClass.caractere field a whitespace using String object comparison? " + isWhiteSpace);
              //Just to be sure if the string has any character in its state
              System.out.println("How many characters are there in the String object that encapsulates the character? " + character.length());
              //print the codepoint of our Unicode character
              System.out.println("What codepoint represents such character? " + character.codePointAt(0));
              //Is this character defined in Unicode?
              System.out.println("Is this character defined in Unicode? " + Character.isDefined(Test.caractere));
              //Is this character (Unicode code point) an ISO control character?
              System.out.println("Is this character (Unicode code point) an ISO control character? " + Character.isISOControl(Test.caractere));
              //Is this character (Unicode code point) a white space according to Java?
              System.out.println("Is this character (Unicode code point) a white space according to Java? " + Character.isWhitespace(Test.caractere));
              //Is this character (Unicode code point) a letter or a digit?
              System.out.println("Is this character (Unicode code point) a letter or a digit? " + Character.isLetterOrDigit(Test.caractere));
              System.out.println("Just to make sure the field character from class AnemicClass is not a whitespace let´s check the whitespace character
    codepoint: " +
              " ".codePointAt(0));
    This is an anemic (defines no behaviour) class. It is only a repository for data.
    class AnemicClass {
         public static char caractere; //primitive type char     
    /*************************************************************************END OF THE CLASS******************************************************************/
    AND THIS IS THE PROGRAM OUTPUT
    AnemicClass.caractere
    Is AnemicClass.caractere field a whitespace using primitive type char comparison? false
    Is AnemicClass.caractere field a whitespace using String object comparison? false
    How many characters are there in the String object that encapsulates the character?  1
    What codepoint represents such character? 0
    Is this character defined in Unicode? true
    Is this character (Unicode code point) an ISO control character? true
    Is this character (Unicode code point) a white space according to Java? false
    Is this character (Unicode code point) a letter or a digit? false
    Just to make sure the field character from class AnemicClass is not a whitespace let&#9508;s check the whitespace character codepoint 32
    Edited by: charllescuba1008 on Mar 20, 2009 11:20 AM
    Edited by: charllescuba1008 on Mar 20, 2009 11:21 AM

    Hi all
    I am kinda new to Java!
    I have an anemic class (defines no behaviour) that has a field of primitive type char. I know that fields are always initialized even if don´t do it, so I created this class to find out what character java assigns to a character field.
    I didn´t find it out yet!
    The code is working and as a shortcut i prefer to run it in the command prompt by typing:
    javac CharacterTest.java
    java CharacterTest (Here you have to omit the extension)
    /******************************************************************BEGGINING OF THE CLASS**********************************************************************/
    public class CharacterTest {
    public static void main(String args[]) {
    System.out.println("AnemicClass.caractere " + Test.caractere);
    boolean isWhiteSpace = (AnemicClass.caractere == ' ')? true: false;
    System.out.println("Is AnemicClass.caractere field a whitespace using primitive type char comparison? " + isWhiteSpace);
    String character = String.valueOf(AnemicClass.caractere);
    isWhiteSpace = (" ".equals(character))? true: false;
    System.out.println("Is AnemicClass.caractere field a whitespace using String object comparison? " + isWhiteSpace);
    //Just to be sure if the string has any character in its state
    System.out.println("How many characters are there in the String object that encapsulates the character? " + character.length());
    //print the codepoint of our Unicode character
    System.out.println("What codepoint represents such character? " + character.codePointAt(0));
    //Is this character defined in Unicode?
    System.out.println("Is this character defined in Unicode? " + Character.isDefined(Test.caractere));
    //Is this character (Unicode code point) an ISO control character?
    System.out.println("Is this character (Unicode code point) an ISO control character? " + Character.isISOControl(Test.caractere));
    //Is this character (Unicode code point) a white space according to Java?
    System.out.println("Is this character (Unicode code point) a white space according to Java? " + Character.isWhitespace(Test.caractere));
    //Is this character (Unicode code point) a letter or a digit?
    System.out.println("Is this character (Unicode code point) a letter or a digit? " + Character.isLetterOrDigit(Test.caractere));
    System.out.println("Just to make sure the field character from class AnemicClass is not a whitespace let´s check the whitespace character
    codepoint: " +
    " ".codePointAt(0));
    This is an anemic (defines no behaviour) class. It is only a repository for data.
    class AnemicClass {
    public static char caractere; //primitive type char
    }/*************************************************************************END OF THE CLASS******************************************************************/
    AND THIS IS THE PROGRAM OUTPUT
    AnemicClass.caractere
    Is AnemicClass.caractere field a whitespace using primitive type char comparison? false
    Is AnemicClass.caractere field a whitespace using String object comparison? false
    How many characters are there in the String object that encapsulates the character? 1
    What codepoint represents such character? 0
    Is this character defined in Unicode? true
    Is this character (Unicode code point) an ISO control character? true
    Is this character (Unicode code point) a white space according to Java? false
    Is this character (Unicode code point) a letter or a digit? false
    Just to make sure the field character from class AnemicClass is not a whitespace let&#9508;s check the whitespace character codepoint 32

  • Creating XSD from Castor created classes

    I am trying to create the original XSD files from java classes created by Castor 0.4.3. Is there a way to do this without having to dig into the classes themselves?
    Thanks for your help,
    Scott

    Two things:
    I think your custom ClassLoader isn't delegating. In general a ClassLoader should begin by asking it's parent ClassLoader to get a class, and only if the parent loader fails get it itself. AFAIKS you could do what you're trying to do more easilly with URLClassLoader.
    Second, beware that a java source file can, and often does, generate more than one class file. Ignore any class files whose names contain a $ character. It's possible you are loading an internal class which doesn't extend your abstract class.

  • Character ? string ?

    Hi all,
    i have a combination of numbers and characters say for example
    100mg
    250gm
    10-15gm
    23%
    i should seperate the numbers from the units for generating an xml with customized tags
    say 100 and mg seperately 10-15 and gm seperately,23 and % seperately. How can i write a common code which identifies numbers and characters. If Character class is to be used for checking isDigit or isChar(i suppose i am right) how to use that?.Please help me
    urs friendly
    kannan.s

    You could use Regular Expression.
    it's good for matching.
    =)

  • Finding out if a char is a digit

    Hi,
    I was trying to find out if a char is a digit.For some reason, this does not work out. Any help rendered will be greatly appreciated.
    //Declare variables
    String value = "24886dj0";
    char ch = ' ';
    int num = 0;
    //Check if each character in string is digit
    for(int j = 0; j < value.length();j++){
    ch = value.charAt(j);
    if(ch.isDigit(j)){
    num = ch - 48;
    }//end for loop

    change this
    ch.isDigit(j)to
    Character.isDigit(ch)

Maybe you are looking for

  • Unable to Communicate with Printer. HP Officejet Pro 8600 N911g

    Unable to Communicate with Printer. HP Officejet Pro 8600 N911g {Removed for privacy} I cant even connect to the printer on IE, Firefox I get in just fine. I have it set to only warn of ink when printing so what fix won't work I noted my Firmware I a

  • Reading multiples files from a folder and processing

    Hello everyone, Im working on a project to analyst and process Acoustic Emission Signals. My problem is that I have a folder that containts several files .txt. Every file contains 16 waveforms (16 channels) that I need to process with an equation to

  • Updated Firefox on my mac; now it cannot open, fear have lost all my bookmarks. How do I get it back?!

    Fire fox has always been my preferred browser. I received a message that i needed to update Firefox. I did this and moved the icon to my application file as shown; noting only one of two steps was completed. there was no opportunity to do anything el

  • Missing text in X

    All text seems to have gone missing. Look at the login screen. I use xfce4, this started a few days ago. Xorg.0.log, journalctl etc looks fine. Whats going on here?

  • Survey progress bar

    HI, Sharepoint 2013 survey with paging (page break questions) . I need a progress bar, I can see posts about using the task %complete field to generate a client side progress bar but ther's no such field in a Survey List. Whats my options ? Thanks