User Tools

Site Tools


Sidebar

Programming Reference/Librarys

Question & Answer

Q&A is closed







java:util:scanner

This is an old revision of the document!


Scanner()

Reads input from a Keyboard, or a Device

example Scanner

package scanner;
 
public class Scanner {
 
    public static void main(String[] args) {
 
        /* Scanner for the input from the keyboard */
        java.util.Scanner scan = new java.util.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.nextFloat(); // read currency
        System.out.print("Enter the name of the currency: ");
        currencyName = scan.next();/* read String*/
 
        System.out.print("Please enter the exchange rate (with decimal places): ");
        exchangeRate = scan.nextFloat();
        System.out.print("Please enter the new currency name: ");
        newCurrency = scan.next();
 
        /* 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.1365404573.txt · Last modified: 2013/04/08 09:02 by 94.222.245.67

Impressum Datenschutz