Question about System.in.read()

Hello Everyone,
I am having a problem with System.in.read(). I want it to produce an int value, however when I use it the value always comes out to 49.
class Lawn2
     public static void main(String []args) throws Exception
          int lawnSize, lawnPrice, weeklyAmt, length, width;
          length = 50;
          width = 10;
          System.out.println("Enter Width");
          width = System.in.read();
          System.in.read();
          System.in.read();
          System.out.println("You entered " + width);
          lawnSize = length * width;
}

Obvious as System.in.read() reads a byte and converts to ASCII equivalent when assigning to Integer.
Here is a sample code that shows one of many ways of reading input console data
import java.io.*;
class Lawn2{     
     public static void main(String []args) throws Exception     {          
          int lawnSize, lawnPrice, weeklyAmt, length, width;          
          length = 50;          
          width = 10;          
          System.out.print("Enter Width : ");     
          BufferedReader ln = new BufferedReader (new InputStreamReader(System.in));
          width = Integer.parseInt(ln.readLine());
          System.out.println("You entered " + width);          
          lawnSize = length * width;
}

Similar Messages

  • Problem\question about System.in and DataInputStream

    Hello everybody,
    I have the following code, trying to read an int and a string into two variables using System.in and DataInputStream:
    package objectIO;
    import java.io.*;
    public class WriteStudentObject
         * @exception IOException Can not read the file
          public static void main(String[] args)  throws IOException
              a loop could be used here to create more than one
              student object and write each one to a file
              Student mary = new Student("Mary Martin", 12345);
              // open inputstream to read data from keyboard
              InputStream is = System.in;
              DataInputStream dis = new DataInputStream(is);
              int  g;
              String c;
              for (int i =0; i < mary.grades.length ; i++)
                  System.out.println("Enter a grade for test" + i);
                  g  = dis.readInt(); //*QUESTION 1*
                  System.out.println("Enter a comment for test"+i);
                  Datainput stream can read String
                 and store as text or reverse
                  c = dis.readUTF(); //*QUESTION 2*
                  mary.grades= new Grade(g,c);
    try
    FileOutputStream fos = new FileOutputStream("c:\\personal\\test\\studentObject.dat");
    ObjectOutputStream oos = new ObjectOutputStream(fos);
    oos.writeObject(mary); // write obj to file
    oos.close();
    } catch(IOException e) {
    System.out.print("Error: " + e);
    System.exit(1);
    The class uses a Student object -attributes are name\String, id\int and Grades (Grade class has grade obtained\int and a comment from the teacher\String).
    My questions are: (as bolded in the code)
    1) at "g  = dis.readInt()" I have to press Enter 3 times to finish the reading (one after entering a single int and then just 2 times Enter). Should I add bellow this line "System.in.read()" to clear the newline\return character?! What exactly does "System.in.read()" clears? After leaving the method readInt() the variable g has an enormous value... like 9 digits... why? I also tried "g  = (int)dis.readInt()" but the results are the same... I want to read an int from keyboard, but using DataInputStream, not with System.in.read()
    2) at this line the program never ends, it tries continuously to read characters... no matter what I enter it does not complete... I want to read a String from keyboard, but again using DataInputStream...
    Many thanks in advance (and hopefully now I can assign the offered stars...)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               

    don1983p wrote:
    I read an int form keyboard with 'int read_int = System.in.read();' but why I must, after this line, enter a new System.in.read()? (what does this clears? the return character? )Yep. Note that InputStream.read() only reads the next byte of data. If your user had input something that required multiple bytes to represent, you would not have read all of it.
    Fun Fact: Even if you had used Scanner.nextInt() to get your integer from the console, you still need to do a .nextLine() as .nextInt() only reads up to the newline and does not consume it.

  • Question about System.in, FileoutputStream and blocking

    Hello everybody,
    I have written a simple java program that read the System.in and append it to file (which is the first argument of the program).
    Here is the context: my client launch several instance of this program at the same time, with the same file passed in argument. In a first time, I did not use the FileChannel.lock() method because I didn't know there will be several instance, but this is not the question.
    The problem is simple: sometime, I can't figure why (and this is why I'm here to ask you), one of the process block, and stay like this until my client kill it.
    So why? It has something to do with the absence of FileChannel.lock()?
    My client is on Solaris, don't know which version, with a JDK 1.5.0_04
    Here is the code:
    InputStream is = System.in;
    InputStreamReader ir = new InputStreamReader(is, encodingMode);
    BufferedReader reader = new BufferedReader(ir);
    FileOutputStream fos = new FileOutputStream(args[0], true);
    String lineSeparator = System.getProperty("line.separator");
    String buf = null;
    while ((buf = reader.readLine()) != null) {
         fos.write(buf.concat(lineSeparator).getBytes(encodingMode));
    }I think the FileChannel.lock on the output file will solve the problem, but I'm just curious about that odd issue.
    Thanks for help, and excuse me for my bad english.

    Ok, I try to launch 20 occurences at the same time on opensolaris 2009.06 and here is what I've got:
    Error occurred during initialization of VM
    Could not reserve enough space for object heap
    # An unexpected error has been detected by Java Runtime Environment:
    # java.lang.OutOfMemoryError: requested 32756 bytes for ChunkPool::allocate. Out of swap space?
    #  Internal Error (allocation.cpp:120), pid=1603, tid=2
    #  Error: ChunkPool::allocate
    # Java VM: Java HotSpot(TM) Client VM (11.3-b02 mixed mode solaris-x86)
    # An error report file with more information is saved as:
    # /export/home/eric/SocGen/hs_err_pid1603.log
    # If you would like to submit a bug report, please visit:
    #   http://java.sun.com/webapps/bugreport/crash.jsp
    Error occurred during initialization of VM
    java/lang/ClassNotFoundException: error in opening JAR file /usr/jdk/instances/jdk1.6.0/jre/lib/rt.jarI've also got a nice core file and a hs_err_pid1603.log ( here: http://pastebin.com/m513df649
    Funny think is that I can't reproduce it on AIX.
    Hope that can help you helping me.
    Edited by: Eric-Fistons on 13 oct. 2009 13:43
    Edited by: Eric-Fistons on 13 oct. 2009 13:45

  • Urgent Question about System Center Server Names and Dashes

    Hey guys I have a quick Urgent question that would be awesome if it happens to get answered quickly :). 
    So in the past we had problems with different server names in components of System Center when setting up different servers.... I never myself saw the errors or what it did, but an absentee colleague did. For example is anything wrong with a name setup like
    xx-scom-01 / xx-scom-02  as compared to the standard scom01 scom02? We have a certain naming convention we are moving to and I wanted to see if this was fixed. I wanted to say it had something to do with the hyphens... anyways any foresight anyone can
    provide would be awesome. Thanks!

    Dashes in the name will work fine.
    Jonathan Almquist | SCOMskills, LLC (http://scomskills.com)

  • Various questions about System Restore Point

    How can I delete old System Restore Points?
    Is it possible to move the folder that holds them to a different drive then C:?
    Can I limit the amount of data System Restore is allowed to have?
    I have downloaded the TreeSize program to see what is using up so much space on my SSD and a folder named "System Volume Information" has 17GB of storage. Now I have found out that this is the folder that keeps System Restore Points and I checked
    on my restore points. There are too many of them and I was wondering if I could delete some of the old ones? I read on another post that someone said that you could limit the amount of data System Restore is allowed to use. Is this possible?
    Best Regards.

    Hello HurricaneHojax,
    Here I list the requirement, and please correct me if I have misunderstanding:
    1. Delete some System restore point
    2. Move the folder that hold system restore data to a different drive then C:.
    3. Limit the amount of system restore data.
    1, We can delete some system restore point.
    For more information, please take a look at the following article.
    http://windows.microsoft.com/en-hk/windows/delete-restore-point#1TC=windows-7
    2,We are not able to move the system restore folder to other Drive.
    3, We can limit the amount of system restore data.
    For more information, please take a look at the following article about managing the disk space that is used by System Restore.
    http://windows.microsoft.com/en-hk/windows7/how-much-disk-space-does-system-restore-require
    Best regards,
    Fangzhou CHEN
    Fangzhou CHEN
    TechNet Community Support

  • Question about System Properties?

    Can anyone shed some light apon this, if i have my midlet running in the background
    i.e. display set to null.... can j2me read the state of the phone system?
    Can j2me read any 'system property' like active/idle/keylocked??
    ..../fg

    Well generically the answer is NO.
    There is always a chance that some device manufacturer have supplied (if at all) some API to do this. But I am yet to heard about any device in the market to have this. Only what I heard that Nokia is in its way to supply some API like this with there Series 40 3'rd edition phones (ref. http://developers.sun.com/learning/javaoneonline/sessions/2006/TS-4447/index.html?). But they haven't yet announced anything on the release of it.

  • Basic question about accessing card reader from browser

    Hi,
    I am very new to card technology. I have a very basic and general question not directly related to java cards.
    I need to write a web application that allows the user to read a card with a card reader.
    For that I maybe need to write an applet that has to access a native DLL and call methods
    to get the card ID. I already have that DLL (written in Delphi) that reads multi-technology cards.
    Then pass that ID to the application server to retrieve complete information
    about the card holder and display them on the user browser.
    Please could someone guide me on useful resources for that kind of development, for example
    already existing developments, personal experience, design solutions ?
    Thanks.
    Adriano

    Hi Joseph,
    One thing I forgot to tell is that I work with contactless cards and readers.
    Contactless cards use radio frequency. Despite this fact, the problem stays the same. I already have a Delphi DLL that reads contactless cards.
    What I need is a way to retrieve card holder information from a client browser. The card contains a unique ID. My idea is to use an applet that reads that ID on the client-side and then calls a servlet on the server-side to retrieve holder information associated with that ID, i.e. first and last names, and finally displays them on the client browser.
    So the card contains only the unique ID, a database on the server-side contains all other related data.
    Technically the applet has to call the DLL via JNI.
    Do you think that using an applet that way is a good solution, do you see simpler solutions, is there security issues ?
    Thanks.

  • Questions about System Requirements for Adobe Air programs

         Earlier today I asked someone who runs Tweetdeck if they knew what the system requirements were for running Tweetdeck on my desktop (Windows)
    they gave me a link here: http://www.adobe.com/products/air/systemreqs/
    and I found this:
    Windows
    2.33GHz Intel® Pentium® 4, AMD Athlon® 64 2800+, or faster processor
    Microsoft® Windows® XP Home, Professional, or Tablet PC Edition with  Service Pack 3; Windows Server® 2003 or 2008; Windows Vista® Home  Premium, Business, Ultimate, or Enterprise (including 64-bit editions)  with Service Pack 2; or Windows 7
    512MB of RAM (1GB recommended)
    Here are my computer statistics:
    Operating System: Windows Vista™ Home Basic (6.0, Build 6001) Service Pack 1 (6001.vistasp1_ldr.101014-0432)
               Language: English (Regional Setting: English)
    System Manufacturer: Hewlett-Packard
           System Model: Compaq Presario CQ60 Notebook PC
                   BIOS: PhoenixBIOS 4.0 Release 6.1    
              Processor: AMD Athlon Dual-Core QL-65 (2 CPUs), ~2.1GHz
                 Memory: 2814MB RAM
              Page File: 1858MB used, 3993MB available
            Windows Dir: C:\Windows
        DirectX Version: DirectX 10
    DX Setup Parameters: Not found
         DxDiag Version: 6.00.6001.18000 32bit Unicode
    Will me only having a 2.1GHz processor really affect how my performance is with Adobe Air products/apps in general?

    Yes- I've had some lower price pc's using an Adobe AIR app and can tell the difference.
    Computers where performance was a huge issue was Compaq CQ56 & Toshiba Satellite C655D
    These computers are under 2.0 GHz and the app seems to freeze up on occassion or run slower.

  • Question about system catalog tables in oracle 11g database

    We have a customer issue and in order to trobleshoot that we need following information. We would really appreciate any input. I opened SR with support and they asked me to open a discussion here. Please see following.
    what is the meaning of the part# column of the sys.tabpart table and what are the possible values that can be entered into this system catalog table. Our findings are that Oracle may have varied the meaning of this column as the data that we are seeing returned is now > 2gig which does not make sense for a partition number.
    Our stored procedures use this information to retrieve information from the Oracle system tables to process a partitioned Oracle table. Our research has found that the limited description for this table (and columns) can be found in the following Oracle member at the following location:
    dpart.bsq --> ...... oracle database path... \RDMS\ADMIN folder
    The comment within this file notes the following:
    " this value is "partition number (see discussion below)"
    However, there is no additional discussion to describe what the value means and the possible variations. We need this information to move forward with our problem diagnosis and
    to see what kind of updates we need to make for our stored procedures....
    Thanks a lot in advance.
    Avni

    I ran the following....
    list expired archivelog all; - This showed all of the archivelogs that we were not able to validate...
    delete expired archivelog all; This deleted the expired archivelogs that were reported from the previous command...
    I then crosschecked the archivelogs by running the following:
    crosscheck archivelog all;
    Thank you for your help!

  • Question about NTFS Permissions (Read Permissions)

    Hello All,
    I hope somebody can help me. I am wondering about the ACL Atribute "Read Permissions"
    Lets say I created a shre named "Share1"  were "User1" has read Access. I created a Folder named "Folder1" in share. In the NTFS Permssions I specified that User1 can "Traverse Folder and List Folder" . With These
    t atributes set the User is able to Access Folder1. But when I copy a File there - doesnt matter if it is a text document, an exe file, or so, the user is not able to Access the ressource. He gets an Acces Denied Error. Only if I add the Permission "Read
    Permissions" the user is able to open the file.
    I dont get why the user is able to open the Folder but not to execute files with the same permission. Is it possible to open a Folder without "Reading Permession Atribute" but not open a file?
    Why is the Feature available if i cant block users from reading permissions of a specific file?
    Thanks a lot in advance.
    Marco

    Something isn't adding up.  "Read Permissions" should be there by default because just about any basic permission grants the "Read Permissions" advanced permission. See the tables in the following TechNet to see what I mean.
    http://technet.microsoft.com/en-us/library/bb727008.aspx
    Check the Scope for the User1 entry on the Folder1 ACL. The Scope is shown under the "Applies to" section from the screenshot below.  If it doesn't read "This Folder, subfolders, & files" you may want to see if changing to that makes a difference.  

  • Short question about system-id

    Hi everybody,
    how can I get information about the server-id, my web dynpro-application is running on? I need something like sy-sysid in APAP
    Thanks
    Jan

    HI
    String ID = System.getProperty("SAPSYSTEMNAME");
    Try this thread,
    Retrieveing SID programmatically !
    Regards
    Saravanan K

  • Questions about system connections

    Would someone help me clarify something?
    When the ECC QAS system has been copied from the PRD system, the connection between the ECC QAS and the BID system is broken. I was told by Basis that after the copy, the connection has to be restored, and for some reason restoring the connection between the QAS and the BID system, the InfoSources and their transfer rules are completely wipe out from the system.
    We are using the business contents and have not yet did the migration.
    I am not sure why restoring the connection between the ECC and BID system would cause the InfoSources to de deleted. Has anyone come across this problem? Is it something that Basis can prevent from happening? I have not seen this from BW 3.5.

    Hi,
    We tried your suggestions but it is still wiping out the InfoSources. We are aware that the broken connection between the ECC and the BI systems will allways occur when we do a copy. Restoing the connection between the ECC and BI systems is causing the wipe out of the InfoSources.
    We're asking if anyone has come across this problem and was able to fix it,
    Quote of the day:
    "Arrogant idiots go to hell..."

  • Question about the BOOperation READ in the UI Designer

    Hi,
    I just noticed that the BOOperation Read in the UI Designer can use the NodeID, the KeyNavigation and the alternativeKey.
    About the AlternativeKey I have no doubts... but what about the NodeID and the KeyNavigation? Can somebody explain me the differences between these keys?

    Hi Alessandro,
    I created a custom BO:
    businessobject StudioInformationHandler {
    [AlternativeKey] element CustomerUUID : UUID;
    element CustomerID : ID;
    association ToCustomer to Customer;
    association ToStudioInformation to StudioInformation;
    That is an add-on to the customer.
    I made an embedded component for the customer based on this BO. The EC has the CustomerUUID and CustomerID in the inport. In the event handler I try to read the BO instance with BOOperation.
    When debugging my script in After-Modify I expect to have the persistent instance, but I have a new one. I can query my BO and get the persistent instance.
    I think I make a mistake in the BOOperation but I don't know what's wrong. Another suggestion is that data binding in my EC is not as expected.
    I attach a screenshot from my event handler and from the data model.
    The CustomerID is not used at the moment. I tried it with the ID and UUID, but both didn't work for me.
    I think there is something I didn't understand yet.
    kind regards,
    Frank

  • Question about system requirements with upgrade

    I want to upgrade to Leopard which has iLife '08 but as I was reading about iMovie '08, it says that the minimum processor spec is a g5. You can see in my signature, my Powerbook is a g4, will iMovie not run at all (or very well even) or will the installer give me an option not to install the entire iLife'08 suite over what I have right now? Thanks!

    As the Prime Minister has pointed out Leopard doesn't include iLife 08. It is a separate $79 purchase.
    Your G4 (and mine) can't even install iMovie 7 so a purchase would do you no good.
    But, if you bought a new Mac it would come installed with Leopard ($129) and a free version of iLife 08.
    Over $200 in savings right off the bat!

  • Quick question about System Change Number(SCN) in FlashBack Query topic

    i know tht we can get the SCN of the database using flashback concept as follows
    SQL> execute :myvariable:=dbms_flashback.get_system_change_number();
    PL/SQL procedure successfully completed.
    SQL> print myvariable;
    MYVARIABLE
    451578
    for example i made some insertions into a table after this and commit is executed. If i want to rollback this transaction thn i can use
    SQL> execute dbms_flashback.enable_at_system_change_number(:myvariable);
    PL/SQL procedure successfully completed.
    but what if i dont know the SCN of the database before the transaction(insertion in to the table). If i want to rollback the database to state where i don't know the SCN of that particular state, thn how can i do tht.
    i know tht we can do it using timestamp method....but i want to know how we can use this SCN concept in this situation.
    Thanks in advance....

    Maybe you can use [ FLASHBACK_TRANSACTION_QUERY|http://download.oracle.com/docs/cd/B19306_01/server.102/b14237/statviews_4186.htm#i1628130] view .

Maybe you are looking for