开发者

parsing primitive types using java.util.Scanner

开发者 https://www.devze.com 2023-01-01 18:24 出处:网络
I\'m new to java so forgive the noob question. I have created a swing application that basically has three input strings in JTextFields:

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 prim开发者_如何学运维itive 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){

        }
}


Don't worry about asking easier questions, we get them all the time. If it wasn't for easy questions, actually, I'd never get to answer anything.

I can see three problems with your code straight away:

  1. You're catching an exception but then discarding it. There are times when you want to take no action in response to an exception, from a user's perspective, but as a programmer you'll want to know. Try saying nfe.printStackTrace(System.err);

  2. Scanner isn't what you're looking for. Double.valueOf(..) would be more direct.

  3. Now, as for your actual problem: Your three variables go out of scope at the end of the try block. Try declaring them up where you declare KeyInput.

private void submitButtonActionPerformed(java.awt.event.ActionEvent evt) {                                             
    double amount, interest;
    int years;
    while (true) {
        try {
            amount = Double.valueOf(loanAmount.getText());
            interest = Double.valueOf(interestRate.getText());
            years = Integer.valueOf(loanYears.getText());

        } catch (NumberFormatException nfe){
            if (DEBUG) nfe.printStackTrace(System.err);
            return;
        }
    }
    // Your values of amount, interest and years will be available here.
    // Past this last curley brace, however, they will go out of scope.
    // If you want them to stick around for as long as the new object,
    // define them as class fields.
}


No, you shouldn't ;) The Scanner reads data from a Stream and System.in is a stream that reads bytes from the console and not from swing components.

The idea here is to parse the String in the textfield:

private void submitButtonActionPerformed(java.awt.event.ActionEvent evt) {                                             
    try {
       double amount = Double.parseDouble(loanAmount.getText());
       double interest = Double.parseDouble(interestRate.getText());
       int years = Integer.parseInt(loanYears.getText());
     } catch (NumberFormatException nfe){

     }
}


Scanner is for the command line. You need to access the input field and get its value and turn it into an int.

Try it like this:

private void submitButtonActionPerformed(java.awt.event.ActionEvent evt) {                                             
        double amount = Double.valueof(loanAmount.getText());
        double interest = Double.valueof(interestRate.getText());
        int years = Double.valueof(loanYears.getText());
}
0

精彩评论

暂无评论...
验证码 换一张
取 消