Get user input

Hi everyone ! I know how to get a user input if it's a string, but how do I take integer input ?

Get the string, use
Integer.parseInt()
to convert it to an int. If parseInt() throws a NumberFormatException, the user put in non-numeric data.

Similar Messages

  • How to get User input in JTextField?

    How to get User input in JTextField? Can u anyone give me some code samples? thanks

    read the API!!!

  • How to get user input to keep in array in the form of int[]?

    I really want to know how to get user input to keep in an array. Or if it's impossible, can i use the value in "int" and transfer it to an array?

    What I understand is that you want to set an input from the user in an array of int.
    Here is how it work:
    1. Create a stream and a buffer to get and store the informations entered by the user:
    BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
    2. Set this input in a String:
    String input = stdin.readLine();
    3. Set this string in an int:
    int userInput = Integer.parseInt(input);
    4. Then you can put this int in the array.
    Warning this code throws IOExceptions and NumberFormatException ( when you try to set letters as int ). But you can catch them easily.

  • Problems with getting user input

    Hi All,
    I want to get user input from a command line and i am NOT using a GUI.
    The user can still see their password when they are typing it in.
    Is there anyway to show '******' when they type in their password??
    This is what I have so far..
    public static void getPassword() {
    System.out.print("Enter password-->");
    DataInputStream input = new DataInputStream(System.in);
    try{  
    password = input.readLine();
    catch (IOException e){
    System.out.println("ERROR " + e);
    Any help would be greatly appreciated.
    Thanks
    Kal.

    Kal,
    I'm pretty sure you're going to need some kind of GUI (either a Java window or an HTML page) to get the desired effect. There might be a way to set the echo character in the command line, but I'm not sure.
    - Sheepy

  • Getting user input in design view

    I need to get the user inputs in the design view. NOT in the runtime...
    (Say in the design view of NetBeans)
    how to do this?
    Say;
    I have UserControl with 2 textboxes.
    textbox1 for Name and textbox2 for Age.
    when i drag this usercontrol to the form(in form design view) i need to get the 2 inputs(Name and Age) and write them to a file...Is this possible

    User input in design view can only be entered via the properties of the input interface. Else it is impossible.

  • Getting user input in Textfield

    I have a textfield called "time" in a JPanel, on a JFrame.
    When i try getting the user input,
    int t = Integer.parseInt(time.getText());
    I have a nullpointer exception error.
    my code is something like this.
    JTextField Time = new JTextField(2);
    Time.addActionListener(this);
    int t = Integer.parseInt(Time.getText());Is there anything that I miss out?
    The above textfield is located in a JPanel, which is located in another JPanel for layout purposes, is that the reason that it cant work?

    Is the explanation for the above the same as your last post?
    No. The last post (reply 10) was about a way to avoid having to declare a variable, JButton in particular, in class scope.
    I did try to explain the answer to you question in reply 11 in reply 9. The comments in the code point out what is happening with the scope to explain why one way works and the other does not.
    I'll try again. Let's start with a couple of definitions. A declaration is this
        JTextField Text;where JTextField is the type and Text is the name of the variable. We have declared a variable named Text of type JTextField. Now java knows that any time it encounters the variable Time that it is a JTextField.
    Next is instantiation
        Text = new JTextField(2);Here we have instantiated the variable Time and it now points to a real JTextField. You can do both declaration and instantiation at the same time like this
        JTextField Text = new JTextField(2);So on to the issue of scope. Consider your first code block from above
    public class MyClient1 extends JFrame implements ActionListener
        JLabel Length;
        JTextField Time;
        JButton Clear, Play;
        public MyClient1()
            JButton Play = new JButton(" Play ");
            Play.addActionListener(this);
            Play.setBounds(410,0,90,20);
            JButton Clear = new JButton(" Clear ");
            Clear.addActionListener(this);
            Clear.setBounds(310,0,90,20);
            JTextField Time = new JTextField(2); //the JTextField is present and it can't work
            Time.addActionListener(this);
            Time.setBounds(80,0,40,20);
            Time.setText("0");
        }You have declared and instantiated the variable Text inside the constructor. This variable is therefore private to this constructor, ie the method whose body is inside the curley braces {...}. No one outside these curley braces can ever know that there exists something called Time. So the code in your actionPerformed method does not have access to Time and when you try to use the variable it will throw a NullPointerException, ie, java is saying it doesn't exist — it is null. You can test this by adding a line like this System.out.println("Test = " + Test); in the actionPerformed method.
    The declaration in class scope, ie, this line
        JTextField Time;placed above the constructor is still null because it has not been instantiated.
    In the second code block you did instantiate this member variable declaration inside the constructor. They are the same thing, the declaration and the instantiation are referring to the same object. Success! But in the first code block the member variable declaration and the declaration–and–instantiation statement inside the constructor are not even related, they are not the same.
    The java tutorial explains this in Scope. They use the word visibility in a more specialized way than I have above.

  • Exe running from java fails to get user input .Help me...

    I have an DOS based exe.It just asks for input for a number from the user and prints the number it gtes.
    When I try to execute this program from Java with the following code
    import java.lang.Runtime;
    import java.lang.Process;
    import java.io.*;
    public class runtimetest     {
         public static void main(String[] args)     throws Exception {
              Runtime rt = Runtime.getRuntime();
              Process p = rt.exec("myExe");
    nothing happens.Thatis It doesnot ask for the input .and I am just unable to know whether its running or not.
    What could be wrong?
    What is the other means to tun the DOS based exe that accepts user inputs , from Java.
    Plz help me as I am in urgent need.
    Aathi

    try this
    Runtime r = Runtime.getRuntime();
              Process p = null;
              try
                   System.out.println("Exe will be called");
                   p = r.exec("cmd.exe /c start c:/ur exe path ");
                   p.waitFor();
              catch (Exception e)
                   e.printStackTrace();
              }

  • Get user input during playback

    Hi,
    Can OpenScript take user inputs during playback
    Thanks

    Use JoptionPane. Try with the following code:
    String input = JOptionPane.showInputDialog("Enter your input")
    info(input);Regards,
    Deepu M

  • Get user input and find file and read it.....argh

    Ok, so I have to write a program that asks a user for the name of the file they want opened and then it is to read it and then display the max score and the minimum score on the file. also to display the average score on the file. Along with that it is supposed to display how many As, Bs, Cs, Ds, and Fs were on the file IE: a: 1 b: 3 c: 0 d: 4 f: 1. etc.
    Also, the next part is to have the program open the input file and then write all the information to an output file. I was able to do this and have the output file show the peoples names and their score along wtih their grade adn then the whole score average at the end of the output file....
    My question is how do I do the first part by asking the user for the name of file and then for the program to find the file and then display the max, min, average and how many letter grades the class got?

    this is what i have so far and the code before didnt work with JCreator, came up with lots of errors......
    What do i have to do to get the max value and min value to display along with the amount of As, Bs, Cs, Ds, and Fs????
    import java.io.*;
    class extest
    public static void main(String [] args) throws IOException
    // First, open the input file
    TextReader filein = new TextReader(new FileInputStream("test.txt"));
    // Read our number
    int x;
    x = filein.readInt();
    String [] first=new String[x];
    String [] last=new String[x];
    int [] score=new int[x];
    // load data from the input file test1.txt
    for (int i=0; i<x; i++)
    first=filein.readWord();
    last[i]=filein.readWord();
    score[i]=filein.readInt();
         WriteTestFile(first, last, score, x);
    public static void WriteTestFile(String f[], String last[], int score[], int x)
    double average=0;
    // Next, open the output file
    try
    PrintWriter fileout = new PrintWriter(
    new FileWriter("grade.txt") );
    // OK, now write to the file
    fileout.println(x);
    for (int i=0; i<x; i++)
    average=average+score[i];
    if (score[i]>90)
    fileout.println(f[i]+" "+last[i]+" " + score[i] +": "+"A");
    else if (score[i]>80)
    fileout.println(f[i]+" "+last[i]+ " " + score[i] + ": "+"B");
    else if (score[i]>70)
    fileout.println(f[i]+" "+last[i]+ " " +score[i] + ": "+"C");
    else if (score[i]>60)
    fileout.println(f[i]+" "+last[i]+ " " +score[i] + ": "+"D");
    else
    fileout.println(f[i]+" "+last[i]+" " +score[i] + ": "+"F");
    fileout.println("The average score of the class is "+average/x);
    // we're done with the output file
    fileout.close();
    catch (IOException e)
    throw new RuntimeException(e.toString());
    System.out.println("***** Welcome to the Grade Program! *****");
         System.out.println("");
         System.out.println("Please enter the name of the file you want to open: ");
         TextReader input = new TextReader(System.in);
         String fileName = input.readLine();
    System.out.println("The average of the scores is: "+average/x);
    System.out.println("The maximum score is: ");
    System.out.println("The minimum score is: ");
    System.out.println("Number of scores by letter grade:");
    System.out.println("A: ");
    System.out.println("B: ");
    System.out.println("C: ");
    System.out.println("D: ");
    System.out.println("F: ");

  • [b]How can I get User Input ..? [/b]

    hi,
    My question is how can I get get the user to "input a string" and then store it in a variable.
    I tried the foll but it didn't work:
    String EmpName;
    System.in.read(EmpName);
    Thanx...

    BufferedReader buff = new BufferedReader(new InputStreamReader (System.in));
    String userInput = buff.readLine();

  • How to use Pl/sql block to edit check user input

    Hi,
    Please advise on PL/SQL Block code that could be used to Check User input from within a Loop and proceed conditionally based upon User Supplied compliant Input. Thanks in advance.

    Hi,
    yakub21 wrote:
    You could use the ACCEPT to get user input and then assign the input to a variable that could then be verified.
    I believe that anything is possible because we don't yet have proof that it is not!
    I do have code that can accept user input. Is it PL/SQL code? Sybrand was clearly talking about PL/SQL:
    sybrand_b wrote:
    Pl/sql is for server side code, it is not a front end tool, and it is incapable of the functionality you describe.If you do have PL/SQL code that accepts user input, please post an example. A lot of people, including me, would be very interested.
    Pass the user-input value to a variable and then assign that value to another variable from within a Declare of a PL/SQL Block.
    The opportunity here is to figure a way to loop with user input until desired input is entered by the user before proceeding with the code. I'm using PL/SQL Block because I don't want the code to persist. I just want to run it as part of database configuration procedure. ThanksIt sounds like you're talking about SQL*Plus, which is a very poor tool for looping or branching.
    It's possible, but it's not pretty. The following thread shows one way of looping in SQL*Plus:
    Re: How to give the different values to runtime parameters in a loop?

  • Validate user input String

    I dunno why I cant get this to work,
    Im getting user input from JOptionPanes, and need to create an error message each time that nothing is entered, I tried creating a method to validate this, but couldnt get it too work, do I need to use the OnClick event handler???
    This is my code, thanks
    billsName = JOptionPane.showInputDialog("Please enter the Bill Name:");
    tomName = JOptionPane.showInputDialog("Please enter the Tom:");
    sandyNameLoc = JOptionPane.showInputDialog("Please enter the Sandy:");

    this is the way that I had the validation, but nothing happens, when I tyoe nothing in, it just goes to teh next input box
    fName = JOptionPane.showInputDialog("Please enter your First Name:");
    if(fName ==null)
    JOptionPane.showMessageDialog(null, "Eror", "Eror", JOptionPane.ERROR_MESSAGE);
    }

  • User Inputs via TextFeild

    In JavaFX mobile arena.....Can we use TextFeilds similar to J2ME to get user inputs?
    If there is any way to do it please let me know my friends.....
    I have tried with javafx.ext.swing.SwingTextField but it is not supported for mobile package......
    Please help....

    Use javafx.scene.control.TextBox

  • MVC 5 Database Help (calculations and input based on user input)

    Hello all! I am learning MVC 5 database interactions. I would like to fill in certain database columns based on a calculation done on columns the user inputs. I am not having trouble getting user inputs into the database. I am wondering
    about how to take 2 user input database values, make a calculation using them, and put the resulting number in its own column in the database.
    So: after the user inputs price and lot price, I want the application to figure out the margin (price / lot price) and then input margin into the database. Here is what I have, but the calculation doesn't seem to input the result into my database.
    Where am I going wrong? (margin is already a column in the database)
    Thanks in advance!
    publicActionResultCreate([Bind(Include
    = "ID,Name,Price,Location,Color,LotPrice")]
    Automobile automobile)
    if(ModelState.IsValid)
                    db.Automobile.Add(automobile);          
    automobile.Margin = automobile.Price / automobile.LotPrice;
                    db.SaveChanges();        
    returnRedirectToAction("Index");
    returnView(automobile);

    Hi Ruben,
    Please post your question to the MVC forum below for a better assistance on your issue:
    http://forums.asp.net/1146.aspx
    Regards,
    Fouad Roumieh

  • Powershell : Issues with user input collection from Multiple InPutBox Form

    I am having issues with getting user input to pass from a form that a user fills out into variables that I can then use in other methods and commands. (ex; SQL Query, SQL Data Add, ... )
    I have attached the Powershell script I am using in it's designed form but I am having issues getting the DataCollection function to grab the content of the InputBox and send it to a variable for later use.
    Note: I'm running this at this time from the ISE so I can actually see what is going on.
    Any help would be appreciated.
    DAS
    [System.Reflection.Assembly]::LoadWithPartialName( “System.Windows.Forms”)
    [System.Reflection.Assembly]::LoadWithPartialName( “Microsoft.VisualBasic”)
    $FormDBA = New-Object System.Windows.Forms.Form
    $FormDBA.Size = New-Object System.Drawing.Size(300,500)
    $FormDBA.Text = "MIS Data"
    $FormDBA.StartPosition = [System.Windows.Forms.FormStartPosition]::CenterScreen;
    function DataCapture
    $SubSID = $InBoxSID.text
    ECHO $SubSID
    $InBoxTxtSID = New-Object System.Windows.Forms.Label
    $InBoxTxtSID.Location = New-Object System.Drawing.Size(25,15)
    $InBoxTxtSID.Text = "Sticker ID : "
    $InBoxSID = New-Object System.Windows.Forms.TextBox
    $InBoxSID.Location = New-Object System.Drawing.Size(130,10)
    $InBoxSID.Size = New-Object System.Drawing.Size(120,20)
    $InBoxTxtUSR = New-Object System.Windows.Forms.Label
    $InBoxTxtUSR.Location = New-Object System.Drawing.Size(25,55)
    $InBoxTxtUSR.Text = "User Name ; "
    $InBoxUSR = New-Object System.Windows.Forms.TextBox
    $InBoxUSR.Location = New-Object System.Drawing.Size(130,50)
    $InBoxUSR.Size = New-Object System.Drawing.Size(120,20)
    $InBoxTxtPCN = New-Object System.Windows.Forms.Label
    $InBoxTxtPCN.Location = New-Object System.Drawing.Size(25,95)
    $InBoxTxtPCN.Text = "PC Name : "
    $InBoxPCN = New-Object System.Windows.Forms.TextBox
    $InBoxPCN.Location = New-Object System.Drawing.Size(130,90)
    $InBoxPCN.Size = New-Object System.Drawing.Size(120,20)
    $InBoxTxtDPT = New-Object System.Windows.Forms.Label
    $InBoxTxtDPT.Location = New-Object System.Drawing.Size(25,135)
    $InBoxTxtDPT.Text = "Department : "
    $InBoxDPT = New-Object System.Windows.Forms.TextBox
    $InBoxDPT.Location = New-Object System.Drawing.Size(130,130)
    $InBoxDPT.Size = New-Object System.Drawing.Size(120,20)
    $InBoxTxtPCT = New-Object System.Windows.Forms.Label
    $InBoxTxtPCT.Location = New-Object System.Drawing.Size(25,175)
    $InBoxTxtPCT.Text = "PC Type : "
    $InBoxPCT = New-Object System.Windows.Forms.TextBox
    $InBoxPCT.Location = New-Object System.Drawing.Size(130,170)
    $InBoxPCT.Size = New-Object System.Drawing.Size(120,20)
    $InBoxTxtMAK = New-Object System.Windows.Forms.Label
    $InBoxTxtMAK.Location = New-Object System.Drawing.Size(25,215)
    $InBoxTxtMAK.Text = "Make : "
    $InBoxMAK = New-Object System.Windows.Forms.TextBox
    $InBoxMAK.Location = New-Object System.Drawing.Size(130,210)
    $InBoxMAK.Size = New-Object System.Drawing.Size(120,20)
    $InBoxTxtMOD = New-Object System.Windows.Forms.Label
    $InBoxTxtMOD.Location = New-Object System.Drawing.Size(25,255)
    $InBoxTxtMOD.Text = "Model : "
    $InBoxMOD = New-Object System.Windows.Forms.TextBox
    $InBoxMOD.Location = New-Object System.Drawing.Size(130,250)
    $InBoxMOD.Size = New-Object System.Drawing.Size(120,20)
    $InBoxTxtOPS = New-Object System.Windows.Forms.Label
    $InBoxTxtOPS.Location = New-Object System.Drawing.Size(25,295)
    $InBoxTxtOPS.Text = "O.S. : "
    $InBoxOPS = New-Object System.Windows.Forms.TextBox
    $InBoxOPS.Location = New-Object System.Drawing.Size(130,290)
    $InBoxOPS.Size = New-Object System.Drawing.Size(120,20)
    $InBoxTxtDIS = New-Object System.Windows.Forms.Label
    $InBoxTxtDIS.Location = New-Object System.Drawing.Size(25,335)
    $InBoxTxtDIS.Text = "Disposed : "
    $InBoxDIS = New-Object System.Windows.Forms.TextBox
    $InBoxDIS.Location = New-Object System.Drawing.Size(130,330)
    $InBoxDIS.Size = New-Object System.Drawing.Size(120,20)
    $button = New-Object System.Windows.Forms.Button
    $button.Location = New-Object System.Drawing.Size(150,400)
    $button.Width = 100
    $button.Text = “Ok”
    $button.Add_Click({DataCapture})
    $FormDBA.Controls.Add($button)
    $FormDBA.Controls.Add($InBoxTxtSID)
    $FormDBA.Controls.Add($InBoxTxtUSR)
    $FormDBA.Controls.Add($InBoxTxtPCN)
    $FormDBA.Controls.Add($InBoxTxtDPT)
    $FormDBA.Controls.Add($InBoxTxtPCT)
    $FormDBA.Controls.Add($InBoxTxtMAK)
    $FormDBA.Controls.Add($InBoxTxtMOD)
    $FormDBA.Controls.Add($InBoxTxtOPS)
    $FormDBA.Controls.Add($InBoxTxtDIS)
    $FormDBA.Controls.Add($InBoxSID)
    $FormDBA.Controls.Add($InBoxUSR)
    $FormDBA.Controls.Add($InBoxPCN)
    $FormDBA.Controls.Add($InBoxDPT)
    $FormDBA.Controls.Add($InBoxPCT)
    $FormDBA.Controls.Add($InBoxMAK)
    $FormDBA.Controls.Add($InBoxMOD)
    $FormDBA.Controls.Add($InBoxOPS)
    $FormDBA.Controls.Add($InBoxDIS)
    $FormDBA.ShowDialog()

    Change this:
    $button = New-Object System.Windows.Forms.Button
    $button.Location = New-Object System.Drawing.Size(150,400)
    $button.Width = 100
    $button.Text = “Ok”
    $button.DialogResult='Ok'  #<<<<<-------
    #$button.Add_Click({DataCapture})
    Remove function andrun like this:
    if('Ok' -eq $FormDBA.ShowDialog()){
        $FormDBA.Controls|%{$_.Text}
    With names you can get values by control name.
    ¯\_(ツ)_/¯
    This suggestion works for the purpose I needed. 
    If I could, I would attach the file instead of pasting the script so you can see what all I am using this to do.
    But in short, we have a main form we use to pull records from a database and call up an application at the click of a button for remote assistance.  However, we seem to now need the ability to have this application to edit and add new records into said
    database.  That's where this second form came in and also when I hit my issue with the information capture.
    At this time I'm getting this to format the information collected so that I can start using using it with SQL commands.

Maybe you are looking for

  • Unable to Bring Up System in Installer Mode

    My son came home from college with an old IBook G4 with OS X he received as a "throwing away" gift. We don't have the prior owner's userid or password or OS disks. I've picked up a set of OS X10.2 Jaguar disks off of Ebay. When I attempt to start the

  • LinkedButton and lf_Employee

    Hi all, I create a linkButton and a edit text. I'm link the button to lF_Employee and to the Edit Text. The link doesn't work, i can't open the form associate at the name of the edit text. If i insert the name or the empID, nothing happends. The butt

  • Include Core Java Package While using Javadoc

    Hi there: I have developed a java package which import the JavaSQL package. I would like to generate the technical document via Javadoc and include the JavaSQL APIs. Any idea? Thanks, Joseph

  • Field selection for mvmt type 543/acct 895000 differs for vendor goods mvmt

    Hi Pals,           While i post for goods receipt for subcontracting items, it is showing this error " Field selection for movement type 543 / acct 895000 differs for vendor goods movement " Kindly help me to resolve this plz. Thanks&Regards, Prasath

  • Problems with deleting a boilerplate with no contents

    Hi, I'm using Reports Release 9.2.0.5.0 . I'm trying to delete a boilerplate with no contents, but when I press delete, nothing happens. Furthermore, I can't find the boilerplate on the canvas (because it has no contents) so I can not select it there