Can't take User input with DataInputStream!!!

public class DataInputStrm
     public static void main( String[] args ) {
          DataInputStream dIpStream = new DataInputStream( System.in );
             try {
               System.out.println( "Enter an integer" );
               int anInteger = dIpStream.readInt();
               // does not work
               System.out.println( "You have entered " + anInteger );
          catch( java.io.IOException e ) {
               e.printStackTrace();
     } // end of main
} // end of class DataInputStrm/** Problems:
1. If an one digit integer is entered( 2, 3 etc ), it is waiting for another!
     Does not happen for an integer with more than one digit.
2. Displays garbage when the integer value is output.
Please help immediately!

DataInputStream/DataOutputStream provide a way of reading and
writing complex objects in a way that works on all sorts of different
computers.
What you probably want is a Scanner. These things are designed
to read relatively simple data only (numbers, strings, lines, boolean
values etc) from just about any source (anything Readable).
Here's your example, but using a Scanner:import java.util.Scanner;
public class ScanEg
    public static void main( String[] args ) {
        Scanner in = new Scanner(System.in);
        System.out.println( "Enter an integer" );
        int anInteger = in.nextInt();
            // but what if it's not an integer...
        System.out.println( "You have entered " + anInteger );
}Works fine, but you might want to consult the documentation to
find out what happens if the user doesn't enter a number, or if
there's an i/o error. The API documentation is here:
http://java.sun.com/j2se/1.5.0/docs/api/java/util/Scanner.html
Scanner is discussed in the tutorial here:
http://java.sun.com/docs/books/tutorial/essential/io/scanning.html

Similar Messages

  • How can i read user input value to my User exist

    Hi Guru's,
    I am facing one problem in Variables in BPS.
    I am calculating days from Month/year .I have one variable it is for Days,Second variable it is for Month/Year.
    First variable is user exist (for calculating the days),Second varible is user defined variable(this is a Input to the first variable).
    When i am giving the Month/Year(02/2008)variable i am getting the 29 days from the first variable.again i am changing the value of Month/Yera(03/2008) i am not getting the desired value.
    my doubt is my user exist not able to read current value of variable(month/year).how can i pass my value to user exist because this value is user input value based on this value i am calculating the days and dynamically displaying the layout.
    Here is the my sample code..
    seq = '0000'.
    ind = 0.
       i_area = 'ZTEST1'.
       area_var = 'ZVar2'.
    PERFORM instantiate_object USING    i_area
                                        area_var
                                 CHANGING lsr_var.
    PERFORM get_current_value_of_variable
                            USING lsr_var
                            CHANGING lto_value.
    READ TABLE lto_value INTO lso_value index 1  .
    i_month = lso_value-low.
    iv_month = i_month+4(2).
    iv_year = i_month(4).
    concatenate iv_year iv_month '01' into iv_date.
    begindate = iv_date.
    below bracket code calculating the leap year
    ( IF iv_date+4(2) = lc_feb.
        lv_hlp_date_year = iv_date+0(4).
        lv_hlp_rest      = lv_hlp_date_year MOD 4.
        IF lv_hlp_rest = 0.
          EV_DAYS = lc_days_29.
          lv_hlp_rest = lv_hlp_date_year MOD 100.
          IF lv_hlp_rest = 0.
            lv_hlp_rest = lv_hlp_date_year MOD 400.
            IF lv_hlp_rest NE 0.
              EV_DAYS = lc_days_28.
            ENDIF.
          ENDIF.
        ELSE.
          EV_DAYS = lc_days_28.
        ENDIF.)
      ELSE.
    below bracket code calculating the days
    (   CASE iv_date+4(2).
          WHEN lc_jan. EV_DAYS = lc_days_31.
          WHEN lc_mar. EV_DAYS = lc_days_31.
          WHEN lc_may. EV_DAYS = lc_days_31.
          WHEN lc_jul. EV_DAYS = lc_days_31.
          WHEN lc_aug. EV_DAYS = lc_days_31.
          WHEN lc_oct. EV_DAYS = lc_days_31.
          WHEN lc_dec. EV_DAYS = lc_days_31.
          WHEN lc_apr. EV_DAYS = lc_days_30.
          WHEN lc_jun. EV_DAYS = lc_days_30.
          WHEN lc_sep. EV_DAYS = lc_days_30.
          WHEN lc_nov. EV_DAYS = lc_days_30.
          WHEN OTHERS.   CLEAR EV_DAYS.
        ENDCASE.)
      ENDIF.
    data: st_date(2) type c.
    st_date = '01'.
    ind = 0.
    ind = ind + 1.
    here i am passing the low value and high value.
    yto_charsel-chanm = '0CALDAY'.
    yto_charsel-seqno = 1.
    yto_charsel-sign  = 'I'.
    yto_charsel-opt   = 'EQ'.
    yto_charsel-LOW = st_date.
    yto_charsel-chanm = '0CALDAY'.
    yto_charsel-seqno = 1.
    yto_charsel-sign  = 'I'.
    yto_charsel-opt   = 'BT'.
    yto_charsel-high = ev_days.
    INSERT yto_charsel INTO sto_charsel INDEX ind.
    ETO_CHARSEL = sto_charsel.
    lto_value = sto_charsel.
    How can i pass user input value to read this exist ,some where again i have to write code or else??
    This is very urgent can you help me..

    Hi,
    Instead of two perform statements, use single perform.
    PERFORM get_value USING i_area
                              i_variable
                         CHANGING lw_varsel.
    Take the values from lw_varsel-low.
    FORM statement for this perform is as follows.
    DATA: li_varsel TYPE STANDARD TABLE OF upc_ys_api_varsel,
            lv_varsel TYPE REF TO cl_sem_variable.
      FORM get_value USING p_area TYPE upc_y_area
                           p_variable TYPE upc_y_variable
                     CHANGING
                           p_lw_varsel TYPE upc_ys_api_varsel.
        CALL METHOD cl_sem_variable=>get_instance
          EXPORTING
            i_area       = p_area
            i_variable   = p_variable
             I_CREATE     =
          RECEIVING
            rr_variable  = lv_varsel.
           EXCEPTIONS
             NOT_EXISTING = 1
             others       = 2
        IF sy-subrc <> 0.
          EXIT.
        ENDIF.
        REFRESH li_varsel.
    ****Getting the Value*********
        CALL METHOD lv_varsel->get_value
          EXPORTING
            i_user     = sy-uname
            i_restrict = 'X'
          RECEIVING
            rto_value  = li_varsel.
        CLEAR : p_lw_varsel.
        READ TABLE li_varsel INTO p_lw_varsel INDEX 1.
        IF sy-subrc <> 0.
          EXIT.
        ENDIF.
      ENDFORM.                    "get_value
    Try this code.
    Bindu

  • Can I take a picture with my Mac?

    Can I take a selfie with the camera on my Mac?

    Yes. Look in the Applications folder for Photobooth. (It may already be on the dock.)
    Double click to open it. When you are ready to take a shot, click the red camera icon below the picture.

  • Can not take a screenshot with Maverick and track pad.  can not use apple chat to talk with a tech.  there are other issues also like I used to be able to go back a page with the delete button.  No Maverick PDF to instruct on how to use Maverick

    Can not take a screenshot with Maverick and track pad.  can not use apple chat to talk with a tech.  there are other issues also like I used to be able to go back a page with the delete button.  No Maverick PDF to instruct on how to use Maverick
    THE use of the apple support is a mess also.
    I want to be able to use my MAC fully and easily like I used to with Snow Leopard. 
    What is wrong with this maverick OS?

    The only people who can possibly assist you with this is Apple Customer Relations, call your local Apple contact number and ask for Customer Relations then explain your situation clearly and politely (be firm but don't rant).
    You might want to investiage what the local laws are regarding defective goods and 'fit for use' definitions on warranties etc. Consumer Protection can be a useful tool to use or bargain with if needed ...

  • How Can I Take Print Screen With in java Application

    Hello Every One,
    Hope U all r fine.
    How can i take print screen with in java code,
    Please Help me out

    http://search.java.sun.com/search/java/index.jsp?qp=&nh=10&qt=%2Btitle%3A%22print+screen%22&col=javaforums

  • How i can show the user input in alv list on the top

    hi all,
                   please tell me how i can show the user input in alv list on the top.
                         and  also tell me how i  can hide the toolbar in alv.
    regards
    vikas saini

    Hi,
    Use the Below Code.
    form top_of_page.                                          
      data : it_header type slis_t_listheader,
             is_header type slis_listheader.
      is_header-typ  = c_h.
      is_header-key  = space.
      is_header-info = 'Pending Order Information Report'(018).
      append is_header to it_header.
      call function 'REUSE_ALV_COMMENTARY_WRITE'
        exporting
          it_list_commentary = it_header.
    endform.                    "top_of_page
    and finally pass it to .
    call function 'REUSE_ALV_GRID_DISPLAY'
          exporting
            i_callback_program     = sy-repid
            i_callback_top_of_page = 'TOP_OF_PAGE'
    call function 'REUSE_ALV_GRID_DISPLAY'
          exporting
            i_callback_program     = sy-repid
            i_callback_top_of_page = 'TOP_OF_PAGE'
    Regards.
    Arbind

  • How to control user input with a button?

    How do I control a user input from a dialog box by activating it with a button. I am saying "press zero", then when the user does press zero, it should zero the scale.

    The user has only one button they can press.  You should enable the cancel so that the dialog can output an F.  
    What you have works, but the zero will not occur until the case is activated.  If the insides of the case do not work, that is another story.  Put a pop up in the active case to convince yourself that the case is being entered.
    This, what you have, always returns T.
    Mark Ramsdale

  • User input with while

    Hello all!
    I'm kinda new to Java and especially to these forums. I know it's impolite to go into a community and ask for something straight away; but I really can't find a solution to my problem:
    I'm trying to make a small hangman game with console user input. The code:
    package mystupidjavaapp;
    import java.util.*;
    import java.awt.*;
    public class MyStupidJavaApp
        public static void main(String[] args)
         Scanner console = new Scanner (System.in);
         System.out.println("Let's play hangman are you ready? ");
         String s = console.nextLine();
         DrawingPanel panel = new DrawingPanel(500, 400);
         Graphics g = panel.getGraphics();
         panel.setBackground(Color.white);
         drawingPanel(g);
         userInput(console, g, s);
        public static void drawingPanel( Graphics g)
            g.setColor(Color.black);
            g.drawLine(50, 50, 50, 70);
            g.drawLine(50, 50, 100, 50);
            g.drawLine(100, 50, 100, 150);
            g.drawLine(75, 150, 125, 150);
            g.drawString("Hello and welcome to a small sample of a hangman game!", 120, 50);       
        public static void userInput(Scanner console, Graphics g, String s)
           int roll=0;
           String word = "";
           while (!s.equals("yes"))
              console.next();
              System.out.println("Well if you can't say a simple \"yes\"  then we can't go on. ");
              s = console.nextLine();
             System.out.println("Nice! ");
             Random r = new Random();
             roll = r.nextInt(4)+1;
             if (roll == 1)
                 word = "pu";
             else if (roll == 2)
                 word = "copac";
             else if (roll == 3)
                 word = "bani";
             else
                 word = "casa";
          if (roll !=0)
              g.drawLine(150, 130, 200, 130);
           for (int i=0; i<=word.length()-1; i++)
              g.drawLine(150+i*70, 130, 200+i*70, 130);
          int tries=0, position=0, guess=0;
          while (tries <= 6 && guess<=word.length())
           System.out.println("Ok please input a letter(one letter at a time please): ");
           String s1 = console.next();
           char c = s1.charAt(0);
           for (int i=0; i<word.length(); i++)
                 if (word.charAt(i) == c)
                     position = i;
                     System.out.println("Congratulations you got a letter right! ");
                     guess++;
                     if (position==1)
                        {g.setFont(new Font("SansSerif", Font.PLAIN, 40));
                         g.drawString(s1.substring(0,1), 170, 125);}
                     else if (position!=0)
                        {g.setFont(new Font("SansSerif", Font.PLAIN, 40));
                         g.drawString(s1.substring(0,1), 170+70*position, 125);}
                 else
                     tries++;
    }My main problem is with the "while (!s.equals("yes"))" part, where if I enter the loop it will never exit it. What to do? Also, I tried watching for variables but I dunno how to do that to be honest :(. (In Netbeans, I tried going in the top "Debug" menu and there I selected "New Watch" and I chose the variable I wanted. Sadly as I run my code, in the watch window my variable is unchanged; moreover it doesn't even receive a value)
    Suggestions/ help anyone :S?

    Hello!
    Thanks for the reply and sorry for my late reply. I wanted to start out by watching the "tries" variable but I have no idea what I'm doing. i tried setting break points throughout my code but nothing seems to happen/change. I need a step by step tutorial on how to watch a variable "evolve" through my code. I know this is probably a nuisance but until I find a good tutorial on it, I dunno how to do it :(.
    **QUICK EDIT**
    I finally got it to work! The main problem started because I couldn't get all the letters to show up on the right spot or sometimes I couldn't get the letters to show up at all. But after staring at the code for awhile I decide to ditch the "if" that was generating the letters. Here's how it looks now and it works flawlessly! (In case some1 is curious.)
    package mystupidjavaapp;
    import java.util.*;
    import java.awt.*;
    public class MyStupidJavaApp
        public static void main(String[] args)
         Scanner console = new Scanner (System.in);
         System.out.println("Let's play hangman are you ready? ");
         String s = console.nextLine();
         DrawingPanel panel = new DrawingPanel(500, 400);
         Graphics g = panel.getGraphics();
         panel.setBackground(Color.white);
         drawingPanel(g);
         userInput(console, g, s);
        public static void drawingPanel( Graphics g)
            g.setColor(Color.black);
            g.drawLine(50, 50, 50, 70);
            g.drawLine(50, 50, 100, 50);
            g.drawLine(100, 50, 100, 150);
            g.drawLine(75, 150, 125, 150);
            g.drawString("Hello and welcome to a small sample of a hangman game!", 120, 50);       
        public static void userInput(Scanner console, Graphics g, String s)
           int roll=0;
           String word = "";
           while (!s.equals("yes"))
              console.next();
              System.out.println("Well if you can't say a simple \"yes\"  then we can't go on. ");
              s = console.nextLine();
             System.out.println("Nice! ");
             Random r = new Random();
             roll = r.nextInt(4)+1;
             if (roll == 1)
                 word = "pu";
             else if (roll == 2)
                 word = "copac";
             else if (roll == 3)
                 word = "bani";
             else
                 word = "casa";
              g.drawLine(150, 130, 200, 130);
           for (int i=0; i<=word.length()-1; i++)
              g.drawLine(150+i*70, 130, 200+i*70, 130);
          int tries=0, position=0, guess=0;
          while (tries <= 6 && guess<=word.length())
           System.out.println("Ok please input a letter(one letter at a time please): ");
           String s1 = console.next();
           char c = s1.charAt(0);
           for (int i=0; i<word.length(); i++)
                 if (word.charAt(i) == c)
                     position = i;
                     System.out.println("Congratulations you got a letter right! ");
                     guess++;
                     g.setFont(new Font("SansSerif", Font.PLAIN, 40));
                     g.drawString(s1.substring(0,1), 170+70*position, 125);
                 else
                     tries++;
              if (tries == 1)
                  g.drawOval(40, 70, 30, 30);
              else if (tries == 2)
                  g.drawLine(50, 100, 50, 140);
              else if (tries == 3)
                  g.drawLine(50, 110, 20, 120);
              else if (tries == 4)
                  g.drawLine(50, 110, 80, 120);
              else if (tries == 5)
                  g.drawLine(50, 130, 20, 140);
              else if (tries == 6)
                  g.drawLine(50, 130, 80, 140);
    }Sadly I couldn't manage to understand how to use breakpoints :|.
    Edited by: 890334 on Jan 23, 2012 1:53 PM

  • User Input With SQL?

    Hi
    I need to create a query that uses user input as part of the query.
    Example:
    SELECT * FROM item
    WHERE designer = 'Gucci';
    The 'Gucci' part of the query needs to be user input every time the query is ran... Can you help?
    I've tryed the following, but it does not work....
    SELECT * FROM Item
    WHERE Designer like '&& Designer';
    All help appreciated, thanks.

    Hello,
    Try this and if you don't want to specify single quote then at the prompt user have to enter like 'Gucci'. And with the following statement user just have to enter Gucci at the prompt
    SELECT * FROM Item
    WHERE Designer like ('&Designer');Regards

  • How can I deny user input file name (JFileChooser )

    in a common JFileChooser.
    when user open it, how can it deny user to input in filename field?

    just try this
    JFileChooser chooser = new JFileChooser();
    int option = chooser.showOpenDialog(this);
    if( option == JFileChooser.APPROVE_OPTION) {   }
    change the if little bit to suit yr purpose
    or if not try
    getAccessibleContext() this method may deal the situation
    although i have not tried it......
    i m also trying .........
    the onw who gets it earlier notifies other...
    is it fine
    rgds

  • How to take user input and place it in a variable

    All I want to know is how to copy user input from the message pop up and store in a local variable?
    Thanks.

    Hi
    Just take a look at thread's example
    http://forums.ni.com/t5/NI-TestStand/TestStand-Message-Popup/m-p/1792424/highlight/true#M35397
    The trick is done by Message-Popup PostExpression: Locals.strMyResponse = Step.Result.Response
    Hope this helps
    Juergen
    =s=i=g=n=a=t=u=r=e= Click on the Star and see what happens :-) =s=i=g=n=a=t=u=r=e=

  • Can I take my AX with me when I travel?

    Can I take my Airport Express with me and use my WiFi in another location?  I'm going to be visiting with family over the next week or two and am not sure they all have WiFi in their homes.  Would my AX be usable?

    Yes, but minimally they need to have Ethernet and the ability for you to connect an Ethernet cable from the AEX to their router. If they are no using an Airport Extreme Base Station, then the AEX can only connect to their router using Join a wireless Network (Extend will not work without the AEBS.) This is known as Bridge Mode.

  • Can't delete user account with open shopping cart

    Hi all,
    Is it okay to delete user account with open shopping cart? I can't delete user by users_gen, it says existing partner document. I then applied notes 1148837,report PARTNER_SET_DELETE. And this report doesn't really work. I ran it and it found zero occurrnences. Is anybody using this report?
    So is it the only way to delete user from SRM is by su01 and leave some informations on Busines partner (bp)
    Thanks,
    Kev

    Hi. You can easily lock the user and change the validity dates using SU01.
    Then, I would write a program that looks through all locked users and checks their valdity dates. Table USR02 holds all this data.
    Then, if the user is locked and the validity end date was 3 or 6 or whatever months ago (make this a selection parameter probably), then you know you want to delete this user.
    So, go into table HRP1001 where SCLAS = US and SOBID = user name and OTYPE = CP and get field OBJID. This is the central person (CP) number.
    Then, go into table HRP1001 again, this time where SCLAS = BP  and OTYPE = CP and OBJID = the CP number and get field SOBID. This is the business partner (BP) number.
    Now, run function BAPI_USER_DELETE to delete the SU01 user fully, then function HR_CENTRALPERSON_DELETE to delete the central person, then function BUP_BUPA_DELETE to delete the business partner.
    And that should delete everything.
    Regards,
    Dave.

  • I can't take a picture with my playbook

    The camera doesn't work anymore. When I try to take a pic or record a video, I have a message error. 

    Rule of thumb with apparent hardware faults is if a restart does not fix it, reinstall OS using de-brick usb method..
    If it still does not work it's a hardware fault, and depending on the warranty status and your country consumer law take appropriate steps with your supplier or RIM

  • Can I take panoramic photos with my iPad?

    I thought I could take panoramic photos with the camera in my iPad but when I open the camera and click on "options" I don't get the option to take panoramic photos.  I read that it was possible on iPad 2 and assumed that it would be an option on the newer iPad with retinal display.  I looked in Settings under cameras and couldn't see anything there. 

    iOS 6: Which software features does my iPhone, iPad, or iPod touch support?
    http://support.apple.com/kb/HT5457
    Look at this app review. http://appadvice.com/appguides/show/best-panoramic-photo-apps
     Cheers, Tom

Maybe you are looking for

  • A new Bug Found

    Hi Mr. Steve, I am using your example from your blog "Optional Required Fields" and i have noticed the following behaviour. You have a table with 4 columns, Id, RowType, ValueA and ValueB. Id and RowType is mandatory and ValueA and ValueB are not (th

  • What's happened to calendar overnight?

    The list view only shows one or two events per day. I need to see events across the whole day and the months ahead.

  • ARD Over the net

    Hi All Just purchased ARD and i need to know is it possiblt to log on to another mac and take control etc, over the interent. EG. I have a mac at home and i have another mac at the other end of the country, i have put the IP address in of the mac i w

  • Display queue in th emessage mapping

    Hi SapAll. i just want to know about the proper reason on why the pi developers do the display queue in the message mapping programs of every interface that they develope.if so how we can do the proper display queue,as it is displaying in irregular f

  • Filtering cost center members in HFR point of view

    Is it possible to setup a Hyperion Financial Report to filter members of a Cost Center dimension so that users only see in the POV member selection the members they have been provisioned access to? I have a cost center dimension of over 1000 cost cen