The nākamaisDouble() ir Java Scanner klases metode, ko izmanto, lai skenētu nākamo ievades marķieri kā dubultā. Ja tulkojums ir veiksmīgs, skeneris palaiž garām atbilstošo ievadi.
Sintakse
Tālāk ir sniegta deklarācija nākamaisDouble() metode:
public double nextDouble()
Parametrs
Šī metode nepieņem nevienu parametru.
saraksta virkne java
Atgriežas
The nākamaisDouble() metode atgriež divreiz skenēto no ievades.
Izņēmumi
InputMimatchException - Tas izliks šo izņēmumu, ja nākamais marķieris neatbilst Float regulārajai izteiksmei vai ir ārpus diapazona.
NoSuchElementException - Ja ievade ir izsmelta, tiks parādīts šis izņēmums.
IllegalStateException - Šis izņēmums tiks parādīts, ja izsaukšana tiks veikta pēc skenera aizvēršanas.
Saderības versija
Java 1.5 un jaunāka versija
gimp dzēšanas fons
1. piemērs
import java.util.*; public class ScannerNextDoubleExample1 { public static void main(String args[]){ int amount; double balance; //Insert amount and balance from console Scanner input = new Scanner (System.in); System.out.println('Enter the amount: '); amount = input.nextInt(); System.out.println('Enter the Total Balance: '); balance = input.nextDouble(); //reduce amount+fee from balance balance=balance-(amount + 0.50); //print new balance System.out.print('Left Balance is: '+balance); input.close(); } }
Izvade:
Enter the amount: 213734 Enter the Total Balance: 5684566.856 Left Balance is: 5470832.356
2. piemērs
import java.util.*; public class ScannerNextDoubleExample2 { public static void main(String args[]){ String str = 'Hello World! 12 + 13.0 = 15 '; Float f = 2.123f; str = str + f; //Create a new scanner with string Object Scanner scanner = new Scanner(str); //Use US locale to be able to identify doubles in the string scanner.useLocale(Locale.US); //Find the next double token and print it while (scanner.hasNext()) { //Check if the next is a double, print found if (scanner.hasNextDouble()) { System.out.println('Found Double:' + scanner.nextDouble()); } //If double is not found, print 'Not Found' System.out.println('Not Found Double:' + scanner.next()); } scanner.close(); } }
Izvade:
Not Found Double:Hello Not Found Double:World! Found Double:12.0 Not Found Double:+ Found Double:13.0 Not Found Double:= Found Double:15.0 Not Found Double:2.123
3. piemērs
import java.util.*; public class ScannerNextDoubleExample3 { public static void main(String args[]){ Scanner scan = new Scanner(System.in); System.out.print('Enter value: '); if(scan.hasNextDouble()) { double y = scan.nextDouble(); System.out.println('Your entered Double value: ' +y); } else if (scan.hasNext()) { System.out.println('Please Entered the Double Value.'); System.out.println('You entered: ' + scan.next()); } scan.close() } }
Izvade:
Enter value: 375437.565 Your entered Double value: 375437.565
4. piemērs
import java.util.*; public class ScannerNextDoubleExample4 { public static void main(String args[]){ double value; Scanner scan = new Scanner(System.in); System.out.print('Enter the numeric value : '); value = scan.nextDouble(); System.out.println('Double value : ' + value +' Twice value : ' + 2.0*value ); scan.close(); } }
Izvade:
Enter the numeric value : 12345 Double value : 12345.0 Twice value : 24690.0