User Tools

Site Tools


Sidebar

Programming Reference/Librarys

Question & Answer

Q&A is closed







java:util:scanner

Scanner()

Reads input from a Keyboard, or a Device

CODE_SIGN_IDENTITY

–verify

–file-list

–display -r- Terminal.app

example Scanner in java

package scanner;
 
import java.util.Scanner;
 
public class Scanner {
 
    public static void main(String[] args) {
 
        /* Create Scanner Object for the input from the keyboard */
        Scanner scan = new Scanner(System.in); 
 
        double currency, amount, exchangeRate;
        String currencyName, newCurrency;
 
        System.out.print("Please enter the amount (with decimal places) a of Change done to: ");
        currency = scan.nextDouble(); // read currency
        System.out.print("Enter the name of the currency: ");
        currencyName = scan.nextLine();/* read String*/
 
        scan.nextLine(); // clear the keyboard buffer ... trow a \n away
 
        System.out.print("Please enter the exchange rate (with decimal places): ");
        exchangeRate = scan.nextDouble();
        System.out.print("Please enter the new currency name: ");
        newCurrency = scan.nextLine();
 
        /* calculate the new amount */
        amount = currency * exchangeRate;
 
        System.out.println(
                java.lang.Math.round(currency)+" \""
                +currencyName+" \" are "
                +java.lang.Math.round(amount)+" \""
                +newCurrency+"\"");
 
        scan.close(); // close the object 
    }
}

output of scanner

  Please enter the amount (with decimal places) a of Change done to: 42,0
  Enter the name of the currency: Euro
  Please enter the exchange rate (with decimal places): 1,2993
  Please enter the new currency name: Doller
  42 "Euro " are 55 "Doller"
advertising
java/util/scanner.txt · Last modified: 2013/05/27 18:35 by 75.105.50.252

Impressum Datenschutz