Getting a number from  (char)System.in.read

I am very new to java so please forgive the seemingly basic question.
When I try to simply get a number for input into a program, and I use (char)Sysytem.in.read(),
it only gives me the acutual Keyboard number (ie.. 2 = 50, 1 = 49).
Even when I try to parse an Integer using :
public static void getNum( ) throws NumberFormatException
     char char1;
     String String1 = new String ("");
     int X;
     char1 = (char)System.in.read();
while ( char1 <= '0' && char1 <= '9' )      
     String1 = String1 + char1;
     char1 = (char)System.in.read();
     X = Integer.parseInt(String1);
System.in.read();
     return X;
     System.out.println(X);
This gives me a usable number but if I try to use a decimal point in the number and convert it to a double, it wont work because the decimal wont read into the (char)System.in.read();
Anyone have a suggestion ?

Hope this helps - although haven't tried out your code.
System.in.read() returns the next byte of information in the buffer, if the buffer is empty it'll prompt for input (passing control back to hardware/OS). Say the user types 10.56 and presses return - now the buffer has the information 10.56 plus a LF(line-feed) and and a CR(carriage return) character (this is how it is from DOS anyway) and control is passed back to your program which gets only the first byte of information from the buffer, which in this case would represent the character '1', only it doesnt get back the character '1', it gets an int type which represents the byte of information which represented the character '1' to your hardware/OS system (which I believe is decided by the machine local encoding system).
By casting this int to a char by the expression
(char) System.in.read()
its converted to the original character that the int represented from the buffer
If you were using System.in.read() only you would have to reconstruct the input as a String by concatenating the char's one by one only ignoring the last 2 bytes of the buffer (which captured the LF and CR).
Say you had the resulting String from the above reconstruction in the variable s1,
you could then attempt to convert the resulting String to a floating-point type (say a double) using
d = Double.parseDouble(s1)
where d was a double type (as mentioned by abnormal)
You'd have to put this in a try/catch statement because the user could type something invalid like 10.6a5
which would cause the parseDouble() method to throw an exception which unless you handle it yourself will cause your program to abend with a NumberFormatException.
This all seems to be a lot of hard work for input apparently so simple, which is why abnormal suggest using a BufferedReader to overcome the problem of 'manually' stripping the CR and LF from the input leaving you with just a String. I must admit I'm not sure of why abnormal's code wouldn't work for you. But I thought I'd post this anyway because you might find the background info helpful.

Similar Messages

  • What api would I use to get the number of chars in string?

    What api would I use to get the number of chars in string?

    Assuming that you really mean that you want the number of charaters in a String (not string), that would be documented in java.lang.String
    The method is length()

  • Can the Adobe PDF plugin carry exact phrase search criteria through from source system into Reader?

    In SharePoint 2013 if you search for an exact phrase and then view a PDF that is returned by the search (in Reader via the PDF plugin) then the Reader will treat the complete phrase as individual words.
    For example, search for "High Court" in SharePoint, get returned a set of PDF documents, and when you view one of them through the Reader the hit highlighting will be for each instance of "High" or "Court".
    What is wanted is hit highlighting only of instances of "High Court".
    I get the same behavior if the Adobe PDF plugin is installed with the Filesite DMS. So it seems to be standard behavior for the Adobe PDF plugin
    Is there a way to make the Adobe PDF plugin carry exact phrase search criteria through from source system into Reader? And then hit highlight the exact phrase only.

    Hi Sam,
    You can opt for 'Advance Search' option in Reader to match the whole words to get to the specific documents.
    Regards,
    Rave

  • FM to get Personel Number from USER ID

    Hi,
       Is there any FM to get Personel Number from the USER ID(sy-uname).
    Thank you,
    Ramu N.

    Hi Ramu,
    You can find the user-id & Personnel Number in the table PA0105.
    select single PERNR from pa0105 into PERSONEELSNUMMER
    where usrid eq USER_ID
    and   endda eq '99991231'.
    hope this helps!
    best regards,
    Thangesh

  • When logged in multiple times, I get annoying messages from AOL System Msg

    I am using iChat from home and work. In my iChat settings, I have "Allow multiple logins for this account" set.
    If I log in from work (and am still logged in from home, or vice versa), I get a message from "AOL System Msg" telling me that I am now logged in from two locations, and that I can reply with '1' to log off the other location. These messages are somewhat annoying, as "AOL System Msg" apparently cannot be blocked. Is there a way to avoid these and still being logged in from multiple locations?

    Is one version iChat 2.x ?
    If so then the answer is No.
    If both are iChat 3 try changing the setting when the other computer is Off. This maybe difficult but maybe the weekend would be the time to log out of the Work one first and then on Monday turn the Home one Off.
    9:10 PM Friday; August 24, 2007

  • Can you get IMEI number from device ID

    Dear all,
    We have had a user who's phone was stolen so we are trying to get the phon blacklisted. We need the IMEI number but I don't have this obviously so I wondered if Apple can get the IMEI number from the device ID which I can get from office 365.
    Thanks for any help.
    Ed

    greenkomodo wrote:
    I can't unfortunately, we buy a hardware only device and don't register to a number so it isn't tracked on their system for the user what device they last used.
    If the phone was used on a cellular carrier, it is likely that, even though you didn't buy the phone from the carrier, that the IMEI registered with the carrier's system when the phone was activated.

  • Get IDoc-number from flat IDoc using dynamic configuration

    Dear experts
    In an IDoc2File scenario I have added the IDoc-number to dynamic configuration using the folling code in an UDF:
    DynamicConfiguration conf = (DynamicConfiguration)
    container.getTransformationParameters().get(StreamTransformationConstants.DYNAMIC_CONFIGURATION);
    DynamicConfigurationKey FileName = DynamicConfigurationKey.create("http:/"+"/sap.com/xi/XI/System/File","DOCNUM");
    conf.put(FileName, a);
    The information is stored in the SOAP message
    <SAP:DynamicConfiguration SOAP:mustUnderstand="1">
      <SAP:Record namespace="http://sap.com/xi/XI/System/File" name="DOCNUM">0000000000012345</SAP:Record>
    </SAP:DynamicConfiguration>
    Can anybody tell me how I can access this information in the "variable substitution" section of the CC? Perhaps something like "message:docnum"?
    Additional information:
    Taking the IDoc-number from the payload using e.g. "payload:ORDERS05,1,IDOC,1,EDI_DC40,1,DOCNUM,1" does not work, since the XML-IDoc has been converted to an flat-IDoc.
    Thanks in advance for any good ideas
    Markus

    Dear Rodrigo and Sarvesh
    Thanks for your help so far! I applied your hints and now it is working fine.
    But now I have the following additional questions
    1.) My message mapping only maps the input IDoc to an output IDoc of the same type and structure. The MM is only required to process the UDF. Is there another, better solution to achieve my requirement that the IDoc-number shall be part of the filename?
    2.) In the CC you have the possibility to use temporary files (section "Processing", Option "Put File" = "Use Temporary File". I think this will not work with the given solution, will it?
    To possibly help somebody else or clarify the mechanism once again, I wrote down how my solution now looks like.
    My UDF in the message-mapping looks like this
    - Input = DOCNUM of IDoc, e.g. ORDERS05/IDOC/EDIDC/DOCNUM
    - Outpt = DOCNUM of IDoc, e.g. ORDERS05/IDOC/EDIDC/DOCNUM
    - UDF:
    public String putDynamicConfiguration(String docnum, Container container) throws StreamTransformationException{
    try
         DynamicConfiguration conf = (DynamicConfiguration) container.getTransformationParameters().get(StreamTransformationConstants.DYNAMIC_CONFIGURATION);
         DynamicConfigurationKey key = DynamicConfigurationKey.create("http://sap.com/xi/XI/System/File","FileName");
         String filename = "Prefix_" + docnum + ".txt";
         conf.put(key, filename);
         return docnum;
    catch (Exception e)
         return docnum;
    In the CC the settings are now as follows
    File Name Scheme = . -> only a dot or something else, because this field is obligatory, even if it is not used in this case.
    Variable substitution (...)
        Enable -> unchecked
    Adapter-Specific Message Attributes
        Use Adapter-Specific Message Attributes -> checked
        Fail If Adapter-Specific Message Attributes Missing -> checked
        File Name -> checked
        Directory, File Type, Temporary Name Scheme for Target File Name -> unchecked
    When I send an IDoc to PI and view the Adapte-Engine Version of the message (the SOAP-document, not the payload) I can find the Filename:
    <SAP:DynamicConfiguration SOAP:mustUnderstand="1">
    <SAP:Record namespace="http://sap.com/xi/XI/System/File" name="FileName">Prefix_0000000000012345.txt</SAP:Record>
    </SAP:DynamicConfiguration>

  • How to get page number from the PDF using Javascript

    Hi,
    We are having list of Single page PDF. The pdf are named in the order,
    ISBN_Author_01.PDF  (with real page number as i)
    ISBN_Author_02.PDF  (with real page number as ii)
    ISBN_Author_03.PDF  (with real page number as iii)
    ISBN_Author_04.PDF  (with real page number as 1)
    ISBN_Author_05.PDF  (with real page number as 2)
    ISBN_Author_06.PDF  (with real page number as 3)
    ISBN_Author_nn.PDF  (with real page number as 500)
    Here each pdf has a page number and in sequential order.
    The task is to check whether all the pdfs are in sequential order (i.e i, ii, iii, 1, 2, 3). If any page is missing, the script should throw an error report.
    To do this task, I am writing a Javascript to get the real page number from the PDF.
    Can anybody help me how to get the page number from the PDF using Javascript.
    Thanks,
    Gopal

    The "real" page number within a PDF is the count of the physical page starting at 0, zero.
    pageNum numPages
    The number printed on each page is the page label.
    setPageLabels  getPageLabel
    You will have to open each PDF and your script would need to know the page label for that file. I would expect you would need to build a 2 dimensional  array of the file names and the page label for the page within that array.

  • How can I get Notification Number from Material Document?

    Can you please tell me that how can I get Notificatin Number(QMEL-QMNUM) from Material Document(MSEG-BELNR)?
    Regards,
    Subhasish

    This question is closed but unanswered.

  • How to get week number from date

    Hi,
       please mention how to get the number of week from a particular date using fn module?

    Hi Debarshi,
                       Use FM <b>DATE_GET_WEEK</b>
    Reward points if helpful.
    Regards,
    Hemant

  • How can I get the number of chars in a String, without countig the Spaces?

    When I do String.length(), he returns the length with Spaces, but I want the number of chars and numbers.
    thx

    I'm afraid you'll need to write a method of your own for that. It's very simple fortunately:public static int getLengthWithoutWhitespace(String s) {
        java.util.StringTokenizer st = new java.util.StringTokenizer(s, " \t\r\n");
        int counter = 0;
        while (st.hasMoreTokens()) counter += st.nextToken().length();
        return counter;
    }This code is not tested... Here's another, a bit more reliable approach that simply passes through the characters of the string:public static int getLengthWithoutWhitespace2(String s) {
        int counter=0;
        for (int i=0; i < s.length(); i++)
            if (Character.isWhitespace(s.charAt(i))) counter++;
        return counter;
    }

  • Get records number from internal table with condition.

    Internal table itab got more than 1000 records,now i need to get the number of records with condition that itab-field1 = 'XXXX'.
    actully, i got an inefficient logic to count the number in a loop statement. is there better way to implement it?

    hello,
    Every time assigning data into temp table and delete may that may not be much efficient. You can check in loop logic how much time is taken. You can check the below code. here I am trying to check the numbers of plant for for one material.
    In this logic material is the first field. So if there is option to make the required field in to the 1 st position then it will be nice.
    TYPES: BEGIN OF x_count,
            matnr TYPE matnr,
            count TYPE i,
           BEGIN OF x_count.
    DATA: i_marc  TYPE STANDARD TABLE OF marc,
          i_count TYPE STANDARD TABLE OF x_count,
          wa_count TYPE x_count.
    FIELD-SYMBOLS: <wa_marc> TYPE marc.
    SELECT * UP TO 1000 ROWS
      FROM marc
      INTO TABLE i_marc.
    IF sy-subrc = 0.
      SORT i_marc BY matnr.
      LOOP AT i_marc ASSIGNING <wa_marc>.
        wa_count-count = wa_count-count + 1.
        AT END OF matnr.
          wa_count-matnr = <wa_marc>-matnr.
          APPEND wa_count TO i_count.
        ENDAT.
      ENDLOOP.
    ENDIF.
    Thanks
    Subhanakr

  • Relation EQUI EQUI- EKKO to get PO number from EKKO

    hi,
    i would like to take the PO number (from EKKO-EBELN) starting from the SERNR in the EQUI, what is the relation? there are some functions?
    thanks

    my guess:
    EQUI-SERNR ==> EQUI-MATNR = EKPO-MATNR => EKPO-EBELN (=EKKO-EBELN)

  • Applescript to get phone number from name

    Hi, I am trying to make an applescript that will simply return a phone number from contacts when given a name. It is part of a larger script, but everything else is already working. It seems like it would be pretty simple, but nothing that I have tried will work.
    Here is my best guess:
    tell application "Contacts"
    get phone of "John Doe"
    end tell
    I just need to set a variable to equal the phone number that is returned. Thanks in advance.

    Hello
    You may try something like this.
    set n to "John Doe"
    tell application id "com.apple.AddressBook"
        set p to value of phone 1 of (person 1 whose name = n)
        set q to {label, value} of phones of (people whose name = n)
        set r to value of phone 1 of (person 1 whose name = n) whose label = "home"
    end tell
    return {p, q, r}
    Note that database may contain multiple records for the same person name and a person's record may contain multiple phone numbers.
    Tested with AddressBook 5.0.3 under OS X 10.6.8.
    Regards,
    H

  • Getting quotation number from service order

    hi
      i have a situation where i need to find the quotation number created by DP80 from a service order.
        i'm able to find service order number from quotation but that does not solve my problem. i need to find the quotation number from service order number.
       pl help.
    thanks in advance

    hi Koustav,
        I'm in same situation as yours. i need to update preceding Qutation from the service order and only  if a precticular item category is precent in service order.
    As u have already worked on Qutation n service order stuff.. can u plz suggest me some thing.
    thanks in Advance

Maybe you are looking for

  • Can i choose the resolution of my external display for when the lid is closed when my lid is open

    When I close the lid of my MacBook Pro for an external display apple chooses a resolution that isn't compatible with my display can I choose the resolution when the lid is open in any way of my external display for the mode when the lid is closed?

  • Create a condition based on a select

    Hi, I have the necessity to create a condition, inside a report, based on a select, for example (when I build a condition): Item: "Item (RUO)".Description Condition: IN Values: select item_description from ISUPP_LOV_RUO_SLA_PRODUCTS_V -> 3 values How

  • CIAC 3.1.1 - Machine turned on or off

    Hi all Does anybody know if CIAC 3.1.1 is able to register the state (turned on or off) of a machine? We have lifecycle options in the My Servers page and there we can Power a machine up and/or down. Is that registered somewhere in the database ? I'd

  • External Video Output

    Hi, I'm having trouble sending the image from Color to an external monitor for accurate color balancing. When I go to Video Output pop up menu in the set up preferences, the only option they let me click is "Disabled". It's an HD image to a Sony HDV

  • Windows Operating System; Version: 6.1.7601.18409; Event ID: 36888; Event Source: Schannel; AlertDesc 10; ErrorState10

    The computer consistantly freezes for about one to two seconds over and over, making it difficult to navigate through web pages.  I have looked at the Event log and this is the only consistent error that has taken place that falls in line with this i