Find out Integers in String

Hi all:
I am stuck with a simple problem here. I have a file where there are 2 data types String(Item_Name) and int(Item_Code). Something like this: ----------------------------------------------------
  Item_Code            |          Item_Name  |
     34                                  Pens
       7                                   Ice Cream
---------------------------------------------------- Now, I can catch the error if users input character in Item_code like '34ab' with NumberFormatException but how to catch if they put digits in Item_Name like 'Pen345s' ?
Should I first use string tokenizer to divide the strings and then check by individual characters? How to know if its a digit?
thanks in advance.

However if the question was what I had thought it was
then I am right and you are wrong. Catching the
exception IS the right thing to do.
Because of overflow.Here's the code for Integer.parseInt() from the JDK 5.0 source. You can avoid the overhead of catching an exception if you code a simple isNumeric() function. As you can see, they iterate over each character in the string and check if it's a digit.
By the way, most code I've written simple just catch the exception :)
public static int parseInt(String s, int radix)
          throws NumberFormatException
        if (s == null) {
            throw new NumberFormatException("null");
     if (radix < Character.MIN_RADIX) {
         throw new NumberFormatException("radix " + radix +
                             " less than Character.MIN_RADIX");
     if (radix > Character.MAX_RADIX) {
         throw new NumberFormatException("radix " + radix +
                             " greater than Character.MAX_RADIX");
     int result = 0;
     boolean negative = false;
     int i = 0, max = s.length();
     int limit;
     int multmin;
     int digit;
     if (max > 0) {
         if (s.charAt(0) == '-') {
          negative = true;
          limit = Integer.MIN_VALUE;
          i++;
         } else {
          limit = -Integer.MAX_VALUE;
         multmin = limit / radix;
         if (i < max) {
          digit = Character.digit(s.charAt(i++),radix);
          if (digit < 0) {
              throw NumberFormatException.forInputString(s);
          } else {
              result = -digit;
         while (i < max) {
          // Accumulating negatively avoids surprises near MAX_VALUE
          digit = Character.digit(s.charAt(i++),radix);
          if (digit < 0) {
              throw NumberFormatException.forInputString(s);
          if (result < multmin) {
              throw NumberFormatException.forInputString(s);
          result *= radix;
          if (result < limit + digit) {
              throw NumberFormatException.forInputString(s);
          result -= digit;
     } else {
         throw NumberFormatException.forInputString(s);
     if (negative) {
         if (i > 1) {
          return result;
         } else {     /* Only got "-" */
          throw NumberFormatException.forInputString(s);
     } else {
         return -result;
    }

Similar Messages

  • How to find out if a string is all alphabets

    Hi,
    How can I find out if a string contains all alphabets? Please help. Thanks.

    Hi,
    How can I find out if a string contains all
    all alphabets? Please help. Thanks. I am not sure if there's easier way. But this code should do what you want:
    boolean bAlpha = true;
    for (int i=0; i<str.length; i++)
    if (!Character.isLetter(str.charAt(i))){
    bAlpha = false;
    break;
    //bAlpha is true if str contains only alphabets.

  • How to find out index of string

    Hi,
    Can you help me to find out index of following strting.
    "sss.xxxxx_pain.001.001.02_1002225618.xml"
    need to get value "1002225618".
    ideally i need to get the value inbetween after second "_"  and value before ".xml".
    Thanks
    Mahi

    Hi Mahi,
    UDF should be like this:
    If you do not want .xml in the out replace statement
    var1=var1.substring(sublen+1, len);
    with
    var1=var1.substring(sublen+1, len-4);
    Regards,
    Krupa

  • How to find out what a string contains?

    I'm in the process of writing an XML parser for a project that I am working on. There will be one field in the XML that can be used to store an int, a date, or a string. I need to figure out a way to determine if that field contains an int, a date, or a string without modifying the XML at all. Meaning I can't just put an indicator in the XML itself. So given the string of data from the xml, how do I figure out what it is so that I can convert it to that type?
    Any help would be appreciated as I don't have much time left to work on this.

    How are you going to be able to tell?
    There will be one field
    in the XML that can be used to store an int, a date,
    or a string. "20010101" fits all three. In the absence of any other information, there's no way to tell what the sender meant the type to be.
    Now, if you assume some formatting information, then you have a chance. Let's say that "dates" MUST be in the format "YYYY-mm-dd hh:MM:ss". Use SimpleDateFormat and Calendar and try to parse the string. If it works - it's a Date.
    If it doesn't work - try to parse it as an Integer. If it works - it's an int.
    If THAT doesn't work - give up and just use the String.
    Does that help?
    Grant

  • How to find out whether the String contains chinese characters

    I need to check if the string contains chinese characters in Java. Does anyone know how to do it? thx.

    Since Java strings contain UNICODE code points, check the content against the relevant page here - http://www.unicode.org/charts/ .

  • How to find out the exact string in a single line?

    Hi,
    I have some data in table like below.
    CERTTEST
    CERT TEST
    CERT
    My requirement is fetch only CERT as a output.
    Please share your thoughts.
    Thanks

    Not sure what's the issue:
    with t as (
               select 'CERTTEST' str from dual union all
               select 'CERT TEST' from dual union all
               select 'CERT' from dual
    -- end of data sample
    select  str
      from  t
      where str = 'CERT'
    STR
    CERT
    SQL> SY.
    P.S. And if you need to seclect case insensitive CERT, use:
    SQL> with t as (
      2             select 'CERTTEST' str from dual union all
      3             select 'CERT TEST' from dual union all
      4             select 'CeRt' from dual union all
      5             select 'CERT' from dual
      6            )
      7  -- end of data sample
      8  select  str
      9    from  t
    10    where upper(str) = 'CERT'
    11  /
    STR
    CeRt
    CERT
    SQL>

  • How to find out if a string has 2 letters

    I am making an app in which I am getting information from a date selector and if the selected date is, for example, 1, it needs to be 01, so I need to get a command that can tell if the text has 2 digits.
    For example:
    if (dateTime.Value.Month.ToString() == TwoDigits)
    ToMonth_Text.Text = dateTime.Value.Month.ToString();
    else
    //Month for To Date
    if (dateTime2.Value.Month.ToString() == "1")
    ToMonth_Text.Text = "01";
    if (dateTime2.Value.Month.ToString() == "2")
    ToMonth_Text.Text = "02";
    if (dateTime2.Value.Month.ToString() == "3")
    ToMonth_Text.Text = "03";
    //And so on....
    Zack Bowling

    Thanks, this worked for me. Here's my code I used for any future people who have the issue:
    private void To_Closed(object sender, EventArgs e)
    var dateTime2 = To_Text.Value;
    ToMonth_Text.Text = dateTime2.Value.Month.ToString();
    ToDay_Text.Text = dateTime2.Value.Day.ToString();
    if (ToMonth_Text.Text.Length == 2)
    ToMonth_Text.Text = dateTime2.Value.Month.ToString();
    else
    //Month for To Date
    if (dateTime2.Value.Month.ToString() == "1")
    ToMonth_Text.Text = "01";
    if (dateTime2.Value.Month.ToString() == "2")
    ToMonth_Text.Text = "02";
    if (dateTime2.Value.Month.ToString() == "3")
    ToMonth_Text.Text = "03";
    if (ToDay_Text.Text.Length == 2)
    ToDay_Text.Text = dateTime2.Value.Day.ToString();
    else
    //Day for To Date
    if (dateTime2.Value.Day.ToString() == "1")
    ToDay_Text.Text = "01";
    if (dateTime2.Value.Day.ToString() == "2")
    ToDay_Text.Text = "02";
    if (dateTime2.Value.Day.ToString() == "3")
    ToDay_Text.Text = "03";
    Zack Bowling

  • Find out varchar2 string NULL columns and Empty columns

    Hi dev's ,
    my requiremnt is to find out the string columns Names of whose storing NULL values and EMPTY strings. for that i had written below code. it's getting some error.
    SET ECHO OFF;
    SET FEEDBACK OFF;
    SET SERVEROUTPUT ON;
    SET VERIFY OFF;
    SET PAGES 0;
    SET HEAD OFF;
    spool D:\stringnull.csv
    DECLARE
      v_tab_indent NUMBER(5);
      v_col_indent NUMBER(5);
      v_val1       VARCHAR2(20);
      v_val2       VARCHAR2(20);
      v_query1     VARCHAR(500);
      v_query2     VARCHAR(500);
    BEGIN
      --DBMS_OUTPUT.ENABLE(100000);
      SELECT MAX(LENGTH(table_name))+1,MAX(LENGTH(column_name))    +1
      INTO v_tab_indent,v_col_indent
      FROM user_tab_columns
      WHERE data_type='VARCHAR2';
    FOR i IN
      (SELECT table_name,
        column_name
      FROM user_tab_columns
      WHERE data_type IN ('NVARCHAR2', 'CHAR', 'NCHAR', 'VARCHAR2')
      ORDER BY table_name,
        column_name
      LOOP
        v_query1:='SELECT NVL('||i.column_name||',0) AS VAL    
                  FROM '||i.table_name||' where '||i.column_name||' IS NULL';
        v_query2:='SELECT '||i.column_name||' AS VAL    
                  FROM '||i.table_name||' where '||i.column_name||'=''''';
        --dbms_output.put_line(v_query1);
       -- dbms_output.put_line(v_query2);
        EXECUTE immediate v_query1 INTO v_val1;
        EXECUTE immediate v_query2 INTO v_val2;
        dbms_output.put_line (rpad(i.table_name,v_tab_indent,' ')||','||rpad(i.column_name,v_col_indent,' ')||' ,'||v_val1||','||v_val2);
      END LOOP;
    END;
    Spool OFF
    Set echo on
    Set feedback onERROR:
    Error report:
    ORA-01403: no data found
    ORA-06512: at line 31
    01403. 00000 -  "no data found"
    *Cause:   
    *Action:
    set feedback onpls help me on this issue..
    Thanks,

    Example:
    SQL> DECLARE
      2    v_val       VARCHAR2(20);
      3    v_query1     VARCHAR(32767);
      4  BEGIN
      5   FOR i IN (SELECT table_name, column_name FROM user_tab_columns
      6             WHERE data_type IN ('NVARCHAR2', 'CHAR', 'NCHAR', 'VARCHAR2')
      7             ORDER BY table_name, column_name
      8            )
      9   LOOP
    10     v_query1 := 'SELECT count(*) FROM '||i.table_name||' where '||i.column_name||' IS NULL';
    11     EXECUTE immediate v_query1 INTO v_val;
    12     dbms_output.put_line(rpad(i.table_name,30,' ')||' : '||rpad(i.column_name,30,' ')||' : '||v_val);
    13   END LOOP;
    14  END;
    15  /
    CHILD_TAB                      : DESCRIPTION                    : 0
    DEPT                           : DNAME                          : 0
    DEPT                           : LOC                            : 0
    EMP                            : ENAME                          : 0
    EMP                            : JOB                            : 0
    MYEMP_OLD                      : ENAME                          : 0
    MYEMP_OLD                      : JOB                            : 0
    MYNULLS                        : ENAME                          : 0
    MYNULLS                        : JOB                            : 4
    PARENT_TAB                     : DESCRIPTION                    : 0
    T                              : CHAR_VALUE                     : 0
    TABLE1                         : COL1_DESC                      : 0
    PL/SQL procedure successfully completed.

  • How to find out no. of occurrences of a sting in a given string

    Hi all,
    How do I find out the no. of occurrences of a sting in a given string.
    Your help would be appreciated.
    Thank you,
    Srinivas

    user636994 wrote:
    calculating no. of occurance of a string
    declare
    str1 varchar2(100) :='string';
    str2 varchar2(1000) := 'This is the string where all the string are placed. This pl/sql is used to search the accurance of a string into an another string';
    begin
    dbms_output.put_line(length(str2) - length(replace(str2,str1,substr(str1,1,length(str1)-1))));
    end;
    /A slightly shorter (in length) version of that would be...
    SQL> ed
    Wrote file afiedt.buf
      1  declare
      2  str1 varchar2(100) :='string';
      3  str2 varchar2(1000) := 'This is the string where all the string are placed. This pl/sql is used to search the accurance of a string into an another string';
      4  begin
      5  --dbms_output.put_line(length(str2) - length(replace(str2,str1,substr(str1,1,length(str1)-1))));
      6    dbms_output.put_line((length(str2) - length(replace(str2,str1)))/length(str1));
      7* end;
    SQL> /
    4
    PL/SQL procedure successfully completed.
    SQL>

  • How to find out the type of the value stored in a string variable?

    Hi,
    How do i find out the type of the value stored in a string variable?
    for example,
    I have a string variable str, in which the following type of values wil be stored
    1) Intger
    2) Long
    3) boolean
    4) Char
    Is there any method to find out the type of the value, anything like str.getType()?. Please kindly help me. Thanks in advance.

    Hi All, i'm sorry for the double posting, by mistake it occured.
    Thanks for your replies, i have a string variable str, in which the value is stored and i have another string variable type, in which the type of the value is stored.
    For example,
    String str = "15";
    String type = "Integer";
    Is there any way to verify whether the value stroed in str is of type stored in the variable 'type'. I want to write a method of type boolean, it will throw true of the value stored in str is of type 'type'. Thanks

  • How to find out if a long String has a "subString" twice or more.

    I need to find out if a long String has the same number twice or more.
    I need to look matches for numbers running from 000, 001....999 and if a number is found twice or more, return that number and lines there were found.
    example String:
    -;000 ; 1 ; 2006-12-11 ; -; job;
    x;001 ; 2 ; 2006-12-11 ; 2006-12-12; do this
    -;002 ; 3 ; 2006-12-11 ; -; work
    -;003 ; 0 ; 2006-12-11 ; -; some
    -;004 ; 2 ; 2006-12-11 ; -; thing
    x;005 ; 1 ; 2006-12-11 ; 2006-12-11; reads
    -;003 ; 0 ; 2006-12-11 ; -; here
    Should return from example String:
    003 at lines 4 and 7
    Any ideas?

    So there are newlines in the String?
    You could use a StringTokenizer to break the String into lines, then searching on each line if it contains any of the search strings. (You need to clarify if a line can contain more than one search string).
    Probably you should use a Map<String, Integer> to record the searchcounts.
    Or an int[] Array if you are really sure that the Strings you search for really are numbers.
    Another option is to use:
    LineNumberReader lnr = new LineNumberReader(new StringReader(searchString));This will save you from explicitly having to take care for the line number.
    In any case your example looks like the individual lines are semicolon separated fields and the numbers you search for always are in column two.
    So after breaking up the original String in lines, you could use another StringTokenizer to break up the line in fields.

  • How to find out if there are repeated characters in a string

    hey guys
    well i kinda have a little problem figuring out how to find out
    if there are repeated characters in a string.
    if anyone can help, would appreciate it.
    thanks
    milos

    Try using the StringTokenizer class. if u already know which character to trace. this could do the job.
    eg. String str = "here and there its everywhere";
    StringTokenizer st = new StringTokenizer(str, "e");
    int rep = st.countTokens();

  • How to find out the index in string.

    Hello Experts,
    I have a requirement in which i have to find out the index of the last space before the 50th position in a string.
    is there any way other than spliting the string.
    e.g. if string is - 'aaaa aaaa aaa........ aaa aaa'(length is more than 50)
    i want the last space before 50th char.because i want to split the string before last word withing 50 th character
    please help.
    Thanks,
    Shweta

    Hi Shweta,
    You can build up the logic from this below program.
    data: line(75) value 'abcdefghijk lmnopqrstuvwxyz zyxwvutsrqponml kjihgfedc ba',
           n_line(50),
           off type i,
           val1(50),
           val2(50).
    CALL FUNCTION 'STRING_REVERSE'
      EXPORTING
        STRING          = line(50)
        LANG            = ' '
    IMPORTING
       RSTRING         = n_line
    EXCEPTIONS
       TOO_SMALL       = 1
       OTHERS          = 2.
    IF SY-SUBRC NE 0.
       MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
               WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
    ENDIF.
    SPLIT n_line at space into val1 val2.
    off = strlen( val1 ).
    off = 50 - off.
    write: 'The index of the last space before the 50th position is at ', off.
    Hope this may help you.
    Regards,
    Smart Varghese

  • Looking for a way to find out the x,y coordinates of a string inside a PDF

    Hi!
    I'm looking for a way to find out the x,y coordinates of a string inside a PDF (with free java api's only)
    I got a pdf, and a string to find inside it, what i need to do is to get this string x,y position inside that pdf....
    If any one knows of such, plz let me know...
    Thanx ahead
    Daniel.

    vedmack wrote:
    Hi!
    I'm looking for a way to find out the x,y coordinates of a string inside a PDF (with free java api's only)
    I got a pdf, and a string to find inside it, what i need to do is to get this string x,y position inside that pdf....
    If any one knows of such, plz let me know...
    Thanx ahead
    Daniel.AFAIK, a string of text does not have an (x,y) location inside a PDF file. The location is exists on your screen, and will differ whenever you adjust the resolution of it. Text can have a location when it's stored as an image though, but than it's really the location of a certain number of pixels (not necessarily a string!).

  • How can I find out  the strings that will be presented to the UI

    Hi All,
    I am working on a Swing based application (Swing based UI)
    I am requested to do the following on the bytecode of that application:
    I am supposed to find out what strings from within that bytecode will be presented to the UI and what will be used for an internal usage.
    i have no way of running the application to verify that, i can only work on the static bytecode files.
    hard question ha?
    Thanks,
    EItan.

    You would of course have to run it. Either via a VM or recreating a VM yourself.
    As an example of that how would you figure out what string occurs in the following (pseudo) code?
    String msg = (cnt > 1) ? ("files=" + cnt) : ("file=" + cnt);
    if (cnt > 1) DisplayMessage(msg);

Maybe you are looking for

  • Cfchart CFmx8 charting points with no data

    We are having a problem with <cfchart> in CF 8. It worked fine in CF7.02 with all hotfixes installed. Basically it plots points where there is no data. some lines have data for 15 points and some for maybe only 13 or 14 points. It fills in teh missin

  • Printing form contents

    I have a form created in Adobe Acrobat X Pro.  I amsending it to others to complete the form.  Is there any way for them to print the form and it's contents after it is completed?

  • Discoverer Viewer vs Plus

    Hi, What is the difference between Discoverer Viewer and Plus? I'm using Oracle Discoverer Plus 9.0.2. Thanks,

  • Video transition effects

    Hi, Suddenly out of no where, when I click on two split video clips that are touching (on the same track), it no longer highlights the area to allow me to apply transitional effects... However, I can right click and it will give me "add cross dissolv

  • How do I have the same clip playing in reverse and forwards

    So essentially when I try to reverse one clip of two of the same clips it reverses both of them instead of just the one I selected. I've tried renaming it too and it didn't work. Thanks so much for your answers!