This shows you the differences between two versions of the page.
java:util:scanner [2013/04/08 08:59] 94.222.245.67 created |
java:util:scanner [2024/02/16 01:03] (current) |
||
---|---|---|---|
Line 1: | Line 1: | ||
====== Scanner() ====== | ====== Scanner() ====== | ||
- | Reads input from a Keyboard | + | Reads input from a Keyboard, or a Device |
- | ===== example Scanner ===== | + | CODE_SIGN_IDENTITY |
+ | |||
+ | --verify | ||
+ | |||
+ | --file-list | ||
+ | |||
+ | --display -r- Terminal.app | ||
+ | |||
+ | |||
+ | |||
+ | ===== example Scanner in java ===== | ||
<code java> | <code java> | ||
package scanner; | package scanner; | ||
+ | |||
+ | import java.util.Scanner; | ||
public class Scanner { | public class Scanner { | ||
Line 10: | Line 22: | ||
public static void main(String[] args) { | public static void main(String[] args) { | ||
- | /* Scanner für die Eingabe von der Tastatur einbinden */ | + | /* Create Scanner Object for the input from the keyboard */ |
- | java.util.Scanner scan = new java.util.Scanner(System.in); | + | Scanner scan = new Scanner(System.in); |
double currency, amount, exchangeRate; | double currency, amount, exchangeRate; | ||
Line 17: | Line 29: | ||
System.out.print("Please enter the amount (with decimal places) a of Change done to: "); | System.out.print("Please enter the amount (with decimal places) a of Change done to: "); | ||
- | currency = scan.nextFloat(); // betrag auslesen | + | currency = scan.nextDouble(); // read currency |
System.out.print("Enter the name of the currency: "); | System.out.print("Enter the name of the currency: "); | ||
- | currencyName = scan.next();/* Zeichen auslesen*/ | + | 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): "); | System.out.print("Please enter the exchange rate (with decimal places): "); | ||
- | exchangeRate = scan.nextFloat(); | + | exchangeRate = scan.nextDouble(); |
System.out.print("Please enter the new currency name: "); | System.out.print("Please enter the new currency name: "); | ||
- | newCurrency = scan.next(); | + | newCurrency = scan.nextLine(); |
- | /* Berechnung des neuen Betrags */ | + | /* calculate the new amount */ |
amount = currency * exchangeRate; | amount = currency * exchangeRate; | ||