parsing primitive types using java.util.Scanner

Posted by Rich Fluckiger on Stack Overflow See other posts from Stack Overflow or by Rich Fluckiger
Published on 2010-05-29T19:46:19Z Indexed on 2010/05/29 19:52 UTC
Read the original article Hit count: 234

Filed under:

I'm new to java so forgive the noob question.

I have created a swing application that basically has three input strings in JTextFields: loanAmount, interestRate and loanYears and a single submit button with the EventAction.

I'm trying to use the java.util.Scanner to parse the input to primitive types that I can use in calculations.

I'm getting an error in NetBeans indicating that my variables are not recognized? should I not be calling System.in?

private void submitButtonActionPerformed(java.awt.event.ActionEvent evt) {                                             
    Scanner keyInput = new Scanner(System.in);
    while (true)
        try{
            double amount = keyInput.nextDouble(loanAmount.getText());
            double interest = keyInput.nextDouble(interestRate.getText());
            int years = keyInput.nextInt(loanYears.getText());

        } catch (NumberFormatException nfe){

        }
}

© Stack Overflow or respective owner

Related posts about java