How to convert a String which contains integers to an integer

Hi, I'm doing a simple user-interaction program. For example I ask how many people are in a group. and they give me an answer with a integer number. But how can I convert the System.in to an real integer value, i.e. How can a String value which contain an integer convert to an integer value?
InputStreamReader inputStreamR = new                               InputStreamReader(System.in);
BufferedReader bufR = new BufferedReader(inputStreamR);
String ans = bufR.readLine();
and then what? (I want the answer is an integer);
Thanks

String ans = bufR.readLine();
try {
  int ansAsInt = Integer.parseInt(ans);
} catch (NumberFormatException nfe) {
  // What happens if the String was not an integer
}

Similar Messages

  • How to display a view which contain interactive form in an external window

    Anyone know how to display a view which contain interactive form in an external window?
    I managed to display the view in an internal window with the following code:
    IWDWindowInfo windowInfo = (IWDWindowInfo)wdComponentAPI.getComponentInfo().findInWindows("PdfReportWin");
    IWDWindow window = wdComponentAPI.getWindowManager().createWindow( windowInfo, true);
    For external window, I tried this method : wdComponentAPI.getWindowManager().createExternalWindow(String URL, String title, boolean modal) but there are no parameter for view name?
    Maybe this is not the correct way, really appreaciate if anyone can help.

    Hi,
                U need to the following
               1.Create another window
               2.Create a view in it tat contains Interacive form element.
               3. call this window dynamically thru the first window by creating a context element of IWDWindow  type
    The code may help:
      IWDWindowInfo windowInfo =(IWDWindowInfo) wdComponentAPI.getComponentInfo().findInWindows("SearchEmpwind");
          IWDWindow window = wdThis.wdGetAPI().getComponent().getWindowManager().createWindow(windowInfo, true);
          window.setWindowPosition(WDWindowPos.CENTER);
          window.setWindowSize(400, 250);
          window.open();
          wdContext.currentContextElement().setEmpSearchWind(window);
    Regards
    Ishita

  • How to convert a string value to date

    Dear All,
    I am new to powershell script, i was trying to store a Ad user password set date to a variable add, add a number of days to get the expire date.
    but when i try to convert the variable to date value, I am getting the error as below.
    Please help me......
    PS C:\script> $passwordSetDate = (get-aduser user1 -properties * | select PasswordLastSet)
    PS C:\script> $passwordSetDate
    PasswordLastSet
    7/15/2014 8:17:24 PM
    PS C:\script> $a = [datetime]::ParseExact($passwordSetDate,"MM/dd/yyyy HH:MM:SS", $null)
    Cannot find an overload for "ParseExact" and the argument count: "3".
    At line:1 char:1
    + $a = [datetime]::ParseExact($passwordSetDate,"MM/dd/yyyy HH:MM:SS", $null)
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: (:) [], MethodException
        + FullyQualifiedErrorId : MethodCountCouldNotFindBest
    PS C:\script> $a = [datetime]::ParseExact($passwordSetDate,"MM/dd/yyyy HH:MM:SS", $null)

    Dear All,
    I am new to powershell script, i was trying to store a Ad user password set date to a variable add, add a number of days to get the expire date.
    but when i try to convert the variable to date value, I am getting the error as below.
    Please help me......
    PS C:\script> $passwordSetDate = (get-aduser user1 -properties * | select PasswordLastSet)
    PS C:\script> $passwordSetDate
    PasswordLastSet
    7/15/2014 8:17:24 PM
    PS C:\script> $a = [datetime]::ParseExact($passwordSetDate,"MM/dd/yyyy HH:MM:SS", $null)
    Cannot find an overload for "ParseExact" and the argument count: "3".
    At line:1 char:1
    + $a = [datetime]::ParseExact($passwordSetDate,"MM/dd/yyyy HH:MM:SS", $null)
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: (:) [], MethodException
        + FullyQualifiedErrorId : MethodCountCouldNotFindBest
    PS C:\script> $a = [datetime]::ParseExact($passwordSetDate,"MM/dd/yyyy HH:MM:SS", $null)
    In your post you ask how to convert a string value to a date.  The value returned from the Get-AdUser is already a date.  It does not need to be converted.
    Bill has sshown one way to convert a date to a string and there are other methods.  You need to clarify your question.
    If you are really trying ot convert strings to dates then you can start with this:
    [datetime]'01/21/1965 13:33:23'
    Most date strings aer autodetected by the class.
    ¯\_(ツ)_/¯

  • How to convert a String("yyyy-mm-dd") to the same but in Date format ?

    Hi,
    can anyone plz tell me how to convert a String to a date format.I'm using MSACCESS database.I want to store this string in the database.So i need to convert it to a date format since the table is designed such a way with date/time type for date field.I used SimpleDateFormat ,but i can't able to convert.The code is given below:
    coding:
    public String dateconvertion(String strDate)
    try
    SimpleDateFormat sdfSource = new SimpleDateFormat("yyyy-MM-dd");
    Date date = sdfSource.parse(strDate);
    SimpleDateFormat sdfDestination = new SimpleDateFormat("yyyy-MM-dd ");
    strDate = sdfDestination.format(date);
    catch(ParseException pe)
    System.out.println("Parse Exception : " + pe);
    return(strDate);
    }

    i used prepared statement even now i am getting error like this.....
    i have included the prepared statement package also...
    my coding:
    ResultSet rsdatetemp = null;
    PreparedStatement ps = null;
    String query ="select distinct itemcode from sales where bill date between ? and ?";
    ps = precon.prepareStatement(query);
    ps.setDate(1,d1);//d1 and d2 are in date format
    ps.setDate(2,d2);
    rsdatetemp = ps.executeQuery();
    error :
    symbol : method setDate(int,java.util.Date)
    location: interface java.sql.PreparedStatement
    ps.setDate(1,d1);
    symbol : method setDate(int,java.util.Date)
    location: interface java.sql.PreparedStatement
    ps.setDate(2,d2);

  • How to convert a String to an Icon

    Hello,
    I'm trying to figure out how to convert a String (specifically one letter) to an Icon for a JTree. The program I am working on uses .png files for the icons at the moment, with each .png being a single uppercase letter. However, recent discussion has us wanting to change to something more dynamic, since we anticipate needing new letters in the near future.
    My task is to modify the current codebase so that the Icons used to identify nodes in the tree are created from a Font within code, rather than reading in .png files. I have done some experimenting with creating a Label with just that String, setting the font and trying to extract the image using calls like createImage(int width, int height) and so on, but so far to no avail.
    If I could figure out how to at least convert the String to an image, I could probably make it work (since I can play around with creating an ImageIcon with that image). Thanks in advance for any help.
    Allan

    Here is some quick and dirty code:
    import java.awt.*;
    import javax.swing.*;
    public class BasicIcon implements Icon {
        private String text;
        public BasicIcon(String text) {
            this.text = text;
        public int getIconWidth() {
            return 16;
        public int getIconHeight() {
            return 16;
        public void paintIcon(Component c, Graphics g,  int x, int y) {
            Color old = g.getColor();
            g.setColor(c.getForeground());
            g.drawString(text, x, y+13);
            g.setColor(old);
        public static void main(String[] args) {
            JPanel cp = new JPanel();
            for (char ch = 'A'; ch < 'F'; ++ch) {
                cp.add(new JButton("letter " + ch, new BasicIcon(String.valueOf(ch))));
            JFrame f = new JFrame("BasicIcon");
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.setContentPane(cp);
            f.pack();
            f.setLocationRelativeTo(null);
            f.setVisible(true);
    }One thing that could be improved is the vertical positioning of the letter in the Icon.
    The x,y values passed into paintIcon are the upper-left hand corner of the icon,
    but the y value needed by drawString is the position of the String's baseline.
    To do it right, use one of Font's getLineMetrics methods and play around with ascent.

  • How to convert a string from upper case to lower case in FOX formula

    Hi Experts,
    How to convert a string from upper case to lower case in FOX formula?
    Thanks,
    Cheers!!!
    PANKAJ

    The last result.append( c ) should be:
    result.append( Character.toLowerCase(c) );

  • How to convert a string from lower case to upper case withour using transla

    Hi all,
    how to convert a string from lower case to upper case withour using translate,
    Thanks in Advance,
    Reddy

    Refer to this related thread
    Re: hi guys this very urgent please help

  • How to convert a String to an array

    Can somebody tell me how to convert a String to an array

    ronisto wrote:
    Can somebody tell me how to convert a String to an arrayI assume you mean to convert it into an array of the individual characters that comprise the String.
    Can you not simply look at the API documentation? Nothing in the String API jumps out at you?
    http://java.sun.com/javase/6/docs/api/index.html

  • Help with how to convert a string to a number ??

    Hi All,
    I am having problem converting a string to number.
    Looks like my to_number function is not working.
    Help is very much appreciated.
    My query is below, and I know my problem is in my inner query.
    I am getting the following error:
    what am I doing wrong ? and how should I fix it.
    If there is a better approach, please share it.
    ORA-01722: invalid number
    01722. 00000 -  "invalid number"
    *Cause:   
    *Action:
    SELECT
        A.OBJECT_NAME,
        A.TIMESTAMP,
        A.SESSION_ID,
        A.REQUISITION_ID,
        TO_DATE(CUT(RR_PACE_FUNCTIONS.NAME(A.REQUISITION_ID),3,'.'),'YYYYMMDD') CSD,
        SUBSTR(SUBSTR(RR_PACE_FUNCTIONS.DESCRIPTION(A.REQUISITION_ID),9),1,30) COURSE_ID,
        A.WEEK_GROUP,
        A.APTITUDE_AREA,
        SUM(NVL(GTEP_ASGN,0)) GTEP_ASGN,
        SUM(NVL(AI_ASGN,0)) AI_ASGN,
        SUM(NVL(GTEP_ASGN,0) + NVL(AI_ASGN,0) ) BMT_ASGN
      FROM
        (SELECT OBJECT_NAME,
          SESSION_ID,
          TIMESTAMP,
          TO_NUMBER(FIELD1) REQUISITION_ID,  -- I believe here is the problem
          TO_NUMBER(FIELD2) WEEK_GROUP,     -- here as well
          SUBSTR(FIELD3,1,80) APTITUDE_AREA,
          TO_NUMBER(FIELD4) GTEP_ASGN,  -- here
          TO_NUMBER(FIELD5) AI_ASGN      -- here
        FROM RR_REPORT_QUEUES
        ) A
      GROUP BY OBJECT_NAME,
        TIMESTAMP,
        SESSION_ID,
        WEEK_GROUP,
        APTITUDE_AREA,
        REQUISITION_ID ;Many Thanks,

    Are you sure Field1, Field2, Field4 and Field5 contain a number information in all rows?
    If there is a non numeric String in your Field<x> columns in any row the error will occor.

  • [Oracle 8i] How to convert a string (time stamp) into a date?

    I'm having difficulty figuring out how to convert a time stamp string into a date (or possibly a number).
    The time stamp is 20 positions, character (NOT NULL, CHAR(20))
    in the format: YYYYMMDDHHMMSSUUUUUU
    where Y = Year, M = Month, D = Day, M = Minutes, S = Seconds, and U = Microseconds
    The reason I want to convert this is so that I can compare one time stamp to another (i.e. I want to be able to find the MIN(timestamp), MAX(timestamp), and do inequality comparisons).
    Is this at all possible?
    Thanks in advance for help on this!

    Hi,
    As Damorgan said, if all you want to do is find which is the earliest or latest, then you can just compare the strings: they happen to be in a format where that works.
    If you need to do other things, such as compare them to today's date, or see the difference between two of your rows in days, then you have to convert them to DATEs. (There's no point in converting them to NUMBERs).
    A new data type, TIMESTAMP, which handles fractions of a second, was introduced in Oracle 9.
    Since you're using Oracle 8 (according to your subject line), you either have to
    (1) ignore the microseconds, or
    (2) use a separate NUMBER column for the microseconds.
    Either way, use TO_DATE to convert the first 14 characters to a DATE:
    TO_DATE ( SUBSTR (txt, 1, 14)
            , 'YYYYMMDDHH24MISS'
            )where txt is your CHAR column.
    To convert the microseconds to a number (between 0 and 999999):
    TO_NUMBER (SUBSTR (txt, 15))

  • How to convert a string

    I have a string "March 2006", that I would like to convert into two dates, one is first date of the month and second is the last date of the month.
    For example:
    String strDate="March 2006"
    Now I want to convert it into two dates:
    03/01/2006 and 03/31/2006 and assign those two dates to two variables.
    I have been trying something like this just to convert string to date but no luck:
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
         java.util.Date mydate = new java.util.Date();
         try{
         mydate = sdf.parse("March 2006");
         }catch(ParseException e){}
         System.out.println("Java data object is"+mydate);
    I get following output : Tue Jun 20 10:34:30 CDT 2006

    Thanks for your patience guys. First I am not a java
    expert and just a new bee to Java, and second I guess
    we are going into a different direction here. My
    basic problem is that is there a way to convert
    "March 2006" string to 03/2006? Then once we get
    03/2006, can we find what is the first date ,
    offcourse 01, and what is the last date, which in
    this case is 31?Convert the String to a Date, as you've already been shown how to do
    Use the java.util.Calendar methods to get your last day of the month value (check the javadoc, especially the get method and look at the fields) Post back with any specific questions.
    Good Luck
    Lee
    >
    For example:
    My variable is "March 2006". The end product I want
    is:
    strDate1=03/01/2006
    strDate2=03/31/2006.
    How should I get my end product?
    I can write string manipulation functions to get this
    done,but trying to see if there is an easiest way to
    do this. Thanks.

  • How to convert hex string to time stamp?

    Hello everyone..
    I am currently working on a project in which I have to read the data from a unit and display that data using LabVIEW. I am using serial communication for reading the data. The read data is in hex format. 
    Now, I want to convert this hex string to a time stamp value. I am reading total 16 bytes. How to convert this hex data to a time stamp value. I have developed a VI. But I want to know that the displayed time stamp is correct or not? Or suggest me some other solution. 
    I am using LabVIEW 2011.
    Thanks & Regards,
    Manisha
    Attachments:
    Test.vi ‏7 KB

    Hi mancan,
                      As Iam using LV2009 Iam unable to open your example.Anyway for converting hex to time stamp
    Thanks as kudos only

  • How to check a string is contains numberic digits only ??

    Can anyone tell me what methods I should use and how to use ??
    It is because the string may contains char. or symbol, but I only want the string contains numberic only. (It means the wrong string can be convert into integer)
    Thank you in advance !!

    Well I guess you could always do like this:
    boolean digitsOnly = false;
    try {
    Integer.parseInt(yourString);
    digitsOnly = true;
    }catch(Exception e) {
    System.out.println("The string isn't a integer value");
    if (digitsOnly) {
    }

  • XML: How to convert xml string into Node/Document

    I have a string in xml form.
    <name>
    <first/>
    <second/>
    </name>
    I want to convert this string into DOM Node or Document.
    How can I do this?
    Any help or pointer?
    TIA
    Sachin

    Try this :
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document doc = db.parse(new org.xml.sax.InputSource(new StringReader(strXml)));Hope this helps.

  • How to create a table which contains relational data and Document data

    hai all
    i need to create a table which contains relational data(i mean coulumns whose data types are type NUMBER,VARCHAR) and documents(like xml file/html file/image)using iFS.
    when i store the document data(xml data/html data) in the iFS ,it will be stored as Document Object.so how do i relate this document object belongs to a particular row in a table.
    do guide me
    thanks

    Please see reply at http://technet.oracle.com:89/ubb/Forum36/HTML/000778.html

Maybe you are looking for