logo

Јава Доубле парсеДоубле() Метод

Метод парсеДоубле класе Јава Доубле враћа нову двоструку вредност која је иницијализована на вредност која одговара дефинисаном стрингу.

Овај метод извршава исто као валуеОф() метод класе Флоат.

Синтакса

 public static double parseDouble(String s) throws NumberFormatException 

Параметерс

с- Ово је стринг који треба рашчланити.

Повратна вредност

Метод парсеДоубле() враћа двоструку вредност која одговара пренетом параметру.

Бацања

Метод парсеДоубле() избацује:

1. НуллПоинтерЕкцептион- ако је прослеђени низ нула.

ипцонфиг за убунту

2. НумберФорматЕкцептион- ако пренесени низ не држи парасабле флоат.

Пример 1

 public class Double_parseDoubleMethodExample1 { public static void main(String[] args) { String val1='1000'; String val2='156'; //here instead of adding it will concatenate two Strings //but will convert the String into Float Double d = Double.parseDouble(val1+val2); System.out.println('1. Sum ='+d); Double f1 = Double.parseDouble(val1)+Double.parseDouble(val2); System.out.println('2. Sum ='+f1); Double f2 = Double.parseDouble(val1)*Double.parseDouble(val2); System.out.println('3. Multiplication ='+f2); Double f3 = Double.parseDouble(val1)/Double.parseDouble(val2); System.out.println('4. Division ='+f3); Double f4 = Double.parseDouble(val1)%Double.parseDouble(val2); System.out.println('5. Reminder ='+f4); } } 
Тестирајте одмах

Излаз:

 1. Sum =1000156.0 2. Sum =1156.0 3. Multiplication =156000.0 4. Division =6.410256410256411 5. Reminder =64.0 

Пример 2

 import java.util.Scanner; public class Double_parseDoubleMethodExample2 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print(&apos;Enter your age:&apos;); String str = scanner.next(); // Double d1 = Double.parseDouble(str); Double d2 = 2018 - d1 ; System.out.println(&apos;Your year of birth is = &apos;+d2); if(d2<1965){ system.out.println('you are an adult, you eligible for the discount in railway tickets.'); } else{ double d3="d2-1965;" system.err.println('sorry! not eligible.try again after '+d3+' years'); < pre> <span> Test it Now </span> <p> <strong>Output:</strong> </p> <pre> Enter your age:09 Sorry! You are not eligible.Try again after 44.0 years Your year of birth is = 2009.0 </pre> <h2>Example 3</h2> <pre> import java.util.Scanner; public class Double_parseDoubleMethodExample3 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print(&apos;Enter any string:&apos;); String str = scanner.next(); Double d1= Double.parseDouble(str); System.out.println(&apos;Double value = &apos;+ d1); } } </pre> <span> Test it Now </span> <p> <strong>Output:</strong> </p> <pre> Enter any string:Himanshu@123 Exception in thread &apos;main&apos; java.lang.NumberFormatException: For input string: &apos;Himanshu@123&apos; at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:2043) at sun.misc.FloatingDecimal.parseDouble(FloatingDecimal.java:110) at java.lang.Double.parseDouble(Double.java:538) </pre> <h2>Example 4</h2> <pre> import java.util.Scanner; public class Double_parseDoubleMethodExample4 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); try { System.out.print(&apos;Enter any string:&apos;); String str = scanner.next(); Double d1 = Double.parseDouble(str); String str1=&apos;null&apos;; Double d2 = Double.parseDouble(str1); System.out.println(&apos;1. Double value = &apos; + d1); System.out.println(&apos;2. Double value = &apos; + d2); }catch (NullPointerException e){ System.out.println(&apos;An exception &apos;+e+&apos; has occured.
Plz enter a valid double integer&apos;); }catch (NumberFormatException ex){ System.out.println(&apos;An exception &apos;+ex+&apos; has occured.
Plz enter a valid double integer&apos;); } } } </pre> <span> Test it Now </span> <p> <strong>Output:</strong> </p> <pre> Enter any string:himanshu@123 An exception java.lang.NumberFormatException: For input string: &apos;himanshu@123&apos; has occured. Plz enter a valid double integer </pre> <br></1965){>

Пример 3

 import java.util.Scanner; public class Double_parseDoubleMethodExample3 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print(&apos;Enter any string:&apos;); String str = scanner.next(); Double d1= Double.parseDouble(str); System.out.println(&apos;Double value = &apos;+ d1); } } 
Тестирајте одмах

Излаз:

 Enter any string:Himanshu@123 Exception in thread &apos;main&apos; java.lang.NumberFormatException: For input string: &apos;Himanshu@123&apos; at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:2043) at sun.misc.FloatingDecimal.parseDouble(FloatingDecimal.java:110) at java.lang.Double.parseDouble(Double.java:538) 

Пример 4

 import java.util.Scanner; public class Double_parseDoubleMethodExample4 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); try { System.out.print(&apos;Enter any string:&apos;); String str = scanner.next(); Double d1 = Double.parseDouble(str); String str1=&apos;null&apos;; Double d2 = Double.parseDouble(str1); System.out.println(&apos;1. Double value = &apos; + d1); System.out.println(&apos;2. Double value = &apos; + d2); }catch (NullPointerException e){ System.out.println(&apos;An exception &apos;+e+&apos; has occured.
Plz enter a valid double integer&apos;); }catch (NumberFormatException ex){ System.out.println(&apos;An exception &apos;+ex+&apos; has occured.
Plz enter a valid double integer&apos;); } } } 
Тестирајте одмах

Излаз:

 Enter any string:himanshu@123 An exception java.lang.NumberFormatException: For input string: &apos;himanshu@123&apos; has occured. Plz enter a valid double integer