Input from keyboard

Hey everyone, I'm new to java and am having some problems.
I'm trying to make a simple program that reads a numeric value
entered from the keyboard, and then adds or subtracts it from
another variable for the final output. Ive gotten input to work with
chars, but I need int. Any help would be much appreciated.

See if this helps
// The "Input" class.
import java.io.*;
public class Input
    public static void main (String [] args)
        try
            System.out.print ("Enter Int: ");
            BufferedReader input = new BufferedReader (
                    new InputStreamReader (System.in));
            String userInput = input.readLine ();
            int intValue = Integer.parseInt (userInput);
            System.out.println (intValue);
        catch (NumberFormatException ex)
            System.err.println (ex.getMessage ());
        catch (IOException io)
            System.err.println (io.getMessage ());
    } // main method
} // Input class

Similar Messages

  • Stuck at password screen and doesn't take iny input from keyboard or mouse

    Laptop started and some msg appeared the automatically shut down... Again automatically started but stuck on password screen which neither take input from kayboard or mouse.. Pls help

    Ditto for about a week. Happens maybe every 24-48 hours.
    IIRC, I applied some routine MS Updates prior.
    T510, Win7 Professional, Nvidia GPU.

  • How do you store input from keyboard into a string array

    I am trying to learn java and one of the programs I am trying to write needs to be able to accept a machine hostname at the keyboard and stuff it into a string array element. I am sure I will be using something along the lines of:
    BufferedReader in = new BufferedReader(new InputStreamReader(
                             System.in));
                   String str = "";
                   System.out.print("Enter a FQDN to look up: ");
                   str = in.readLine();
    but how do I get the input stuffed into hostname[ ].
    Any hints or assistance will be appreciated.
    Michael

    Well part of. I need to be able to take a random number of hostnames (ie. mblack.mkblack.com, fred.mblack.com, joe.mblack.com, ...) and after the user presses the enter key between each entry, the inputted information is stored in an array element. for example with the three shown above the array would look like this after the user finished.
    hostname {"mblack.mblack.com","fred.mblack.com","joe.mblack.com"};
    the algorithm would be
    Prompt for hostname
    get user input and press enter
    store hostname into array element
    prompt for next hostname or enter with no input to complete entry and execute lookup.class methods.
    I have the program written and working fine if I use a static array where I put the hostnames in the list, but cannot figure out how to get the information from the keyboard to the array element.
    Thanks for the help though, the response is very much appreciated.
    Michael

  • How to get input from keyboard scanner into an array

    This is probably a very basic question but I'm very new to java..
    My task is to reverse a string of five digits which have been entered using keyboard scanner.
    E.g. - Entered number - 45896
    Output - 69854
    I used the StringBuffer(inputString).reverse() command for this, but i need a more basic method to do this.
    I thought of defining an array of 5
    int[] array = new int [5];
    and then using,
    Scanner scan = new Scanner(System.in);
    to enter the numbers. But I can't figure out how to get the five input numbers into the array.
    If I can do this I can print the array in reverse order to get my result.
    Any other simple method is also welcome.

    Hey thanks for the quick reply,
    But how can I assign the whole five digit number into the array at once without asking to enter numbers separately?
    E.g. - if entered number is 65789
    Assign digits into positions,
    anArray [0] = 6;
    anArray [1] = 5;
    anArray [2] = 7;
    anArray [3] = 8;
    anArray [4] = 9;
    I'm really sorry but I am very new to the whole subject.

  • Making the program react to input from keyboard

    Hello,-
    I am trying to write a program that reacts to user input: when one hits the "y" key on keyboard the program should print out a statement or perform some other action. The following piece of code compiles, but ignores the if-part. Does anyone have any tips?
    Thanks, Vahagn
    import java.io.*;
    public class InputPrinter {
        public static void main(String[] arg) throws IOException {
         InputStreamReader isr = new InputStreamReader(System.in);
         BufferedReader br = new BufferedReader(isr);
         String line = br.readLine();
         //works perfectly
         //doesn't work:
         if (line == "y")
             System.out.println("Hello World!");
    }

    Thanks, that was exactly what I needed:
    import java.io.*;
    public class InputPrinter {
        public static void main(String[] arg) throws IOException {
         InputStreamReader isr = new InputStreamReader(System.in);
         BufferedReader br = new BufferedReader(isr);
         String line = br.readLine();
         //works perfectly
         //doesn't work: if (line == "y")
         if (line.equals("y"))
             System.out.println("Hello World!");
         else
             System.out.println(4+4);
    }In the long run I AM interested in a program that processes input as it happens, without printing the input itself and without hitting the ENTER key. It was nice though to have sorted the above problem out.
    Regards to all, Vahagn

  • Using a textfield for input from keyboard to cause events

    Ok I know very little about java and even less about handling events... I am reciving conflicting information from different sources like webpages and the JDK docs but I so far have this code which compiles but doesn't work. I want it to draw an oval if the user inputs an a.
    public boolean Action(Graphics g, Event evt, Object arg){
              boolean a = true;
              if (evt.target instanceof TextField){
                   if (evt.target.equals(movement)) {
                        direction = direction();
                        System.out.println(direction);
                        clear();
                        if(direction == "a"){
                             g.setColor(Color.decode(randomHex.Colour()));
                             g.fillOval(10,10,10,10);     
                        }//end if direction
                   }//end if target
                   else {
                        a = false;
                   }//end else
              }//end event target textarea
              return(a);
         }//end action
    This of course is only my action method. I look forward to posts that'll help me out. Thx a lot guys.

    Assuming that "direction" is a String variable, don't do this:if (direction == "a")Instead, do this:if (direction.equals("a"))Your code tests whether the direction variable and the "a" constant refer to the same String object, which they won't. The revised code tests whether the direction variable contains the same characters as the constant "a".

  • MBP won't accept input from keyboard seemingly at random

    Recently (the past couple of months) I have been having this weird problem with my 2008 MBP. I will try to input something using the keyboard (type in a URL in the browser, type in my name and password to give my kid extra time on parental controls, type a reply on a message board), and I can't type anything. I hear that little bong, bong at each keypress, like I am doing something wrong, and nothing appears. I will have to close and reopen Firefox (usually works), or just restart my computer.
    Has anyone else had this kind of thing happen? It happens so randomly I haven't been able to guess what might be causing it. Thanks.

    This is a bit of a wild guess.  Try a PRAM reset.  It can do no harm.
    http://support.apple.com/kb/HT1379
    Ciao.

  • Help: How can I read an integer from keyboard

    Hi,
    I am new to java, and I know how to declare integer variable,
    but how can read an integer input from keyboard and assign it
    to the variable.
    Can anyone help me on this.
    Thanks
    Studentfj

    the easiest way is to
    import javax.swing.*
    and use the methods and functions in there
    otherwise from command line i think as far as i remember it is System.in

  • How can I read something from keyboard using i/o operations?

    I wrote the following code in java using *"oracle Jdeveloper 11g release 2"* platform and i think it's correct, but I have a little problem when I try to execute him, it asks me to introduce a number, and this it's correct, but I don't know how to introduce that number. If i want to write the same code in c or c++ using Visual Studio it's very easy when i want to introduce a number i must run the code and then a console window will appear and then i introduce the number.
    Could someone to give me a link at a tutorial about how to use *"oracle Jdeveloper 11g release 2"* when I have to use i/o operations or something useful.
    package instante;
    import java.io.*;
    public class Read_from_keyboard {
    public static void main(String[] args) {
    BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in), 1);
    double number_read = 0.0;
    String line = "";
    try {
    System.out.flush();
    System.out.println("Give a number: ");
    line = keyboard.readLine();
    Double tmpDouble = Double.valueOf(line);
    number_read = tmpDouble.doubleValue();
    System.out.println("The number is: " + number_read);
    } catch (IOException e) {
    System.out.println(" Input from keyboard " + e.toString());
    System.exit(1);
    Thank you!
    Edited by: Iosif on Nov 9, 2012 5:06 AM

    Its a command line application. Perhaps JDeveloper has support built in to be able to do that in the IDE, but generally you'd just want to run a command line application on the command line / shell. How to do that is described in any Java book, in the Oracle Java tutorials and in just about a couple hundred/thousand articles you can find through Google.
    Other than that, the JDeveloper forum is here:
    JDeveloper and ADF

  • How to read input from handheld scanner on a keyboard port

    Hi everybody,
    i have an application that should read input from a scanner.
    If i use a jTextField to input scanned data it works fine.
    the problem is that i don't want to use any text component to input data into it.
    And if i set the jtextField not visible then the focus sustem doesnt work.
    I tried to read a buffered input stream from System.in but it doesnt work.
    If the scanner was installed on a serial port i could use javax.comm to monitor it.
    But unfortunately i must use the keyboard wedge thing...
    Do you have any ideas?
    Thank you in advance

    BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()))
    null

  • Keyboard input from numbers 1-0 not recognized

    On my ibook G4 which I recently started using again, I've noticed that now all input from the "number bar", that is, digits one through zero, is not recognized, that is, it's as if I weren't pressing it at all. I've gone through system preferences, reset everything to default, and I've even done three separate PMU resets, but to no avail. Curiously, all the F keys function. I have access to some number input with the "number lock" feature, but any number that resides on the actual number bar like seven eight or nine cannot be entered. I have no problem with ~, -, +, or delete, either. Any help to point me in the right direction would be greatly appreciated.

    Hey Danger and Welcome to Apple Discussions,
    It sounds like the horizontal trace that links the numbers together has developed an open. I know of no way to repair this and the solution would be a replacement keyboard. Verify the problem isn't software related by plugging in a USB keyboard and checking function.
    Richard

  • Inputting from the keyboard

    hi everyone, can anyone help me on how to read what the user inputs from the keyboard when running a program in command-line(ms-dos prompt)interface.Does it have something to do with System.in any help would be appreciated.
    (Example)
    System.out.println("Please enter your name ");
    String name = //capture the name in string
    System.out.println("Enter your choice");
    int choice = //capture what the choice is
    System.out.println("your choice is "+ choice);
    }

    Modified version of example code in working test file.
    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.io.IOException;
    //Class
    public class KeyBoardInput
      //data members declared static
      static BufferedReader buff = new BufferedReader(
      new InputStreamReader ( System.in) );
      static String buffin; 
      //main method
      public static void main ( String[] args )
        int test = readInt("enter a number");
        System.out.println("you have entered " + test );
      //reads strings and converts to integers
      public static int readInt( String prompt )
            try
         System.out.println(prompt);
            buffin = buff.readLine();
            buff.close();
         catch (IOException IoE) {}
            return Integer.parseInt ( buffin );
      //needs work
      public static String readString( String prompt )
         //?? need to work on this one
    }

  • Can anyone explain to me why sometimes my ipad2 locks up and won't respond to any input from the keyboard.

    My iPad 2 locks up sometimes and won't respond to any input from the keyboard. Have to reboot in order to fix the problem.

    You have 3 choices:
    1. Do a reset (Hold Sleep/Wake and Home buttons about 10 secs or more till Apple logo appears, ignore the Slide to Power Off that appears)
    Note: You will not lose any data.
    2. Restore from backup using iTunes
    3. Restore as new without the backup

  • When I try to view account under the cellular data feature it wont give me an onscreen keyboard and it wont take input from my connected bluetooth keyboard

    When I go into "Settings", "Cellular Data" "View Account" when I'm prompted for username and password no on screen keyboard pops up and it will not take  input from my bluetooth keyboard.  It wont show on screen keyboard even if I have my bluetooth keyboard turned off before I go into settings....

    I get this, but the onscreen keyboard returns when I turn off bluetooth, also I found that if you only select the plan first and press next, when the form returns with errors the fields work again.

  • I have to step-input an existing score and tried it with the step-input-window in combination with midi-input from an usb-keyboard.

    I have to step-input an existing score and tried it with the step-input-window in combination with midi-input from an usb-keyboard.

    Tsk tsk, Nigel, don't you see: this is a story - with a cliffhanger... he said he tried... but he never clarifies if he succeeded or not... but still, he has to do it. It's stronger than him, bigger than him, there's no escaping the step-input and midi-input via usb-keyboard scoring... it's fate I tell you! Fate!

Maybe you are looking for