[JAVA]
Suppose we need to write code that receives String input from a user, and we expect the String input to contain a double value (inside quotes). We want to convert the String to a double using the static method Double.parseDouble(String s) from the Double class. The parseDouble method throws a NumberFormatException if the String s is not a parsable double. Write a method printSum that takes two String parameters (that hopefully have parsable doubles in them), and either prints the sum or prints that the inputs were invalid by handling the exception in a try/catch block and responding accordingly.
Note : Could you please check the output .If you required any
changes Just intimate.I will modify it.Thank You.
________________
ParseString.java
import java.util.Scanner;
public class ParseString {
public static void main(String[] args) {
//Declaring variables
String str1, str2;
double result;
/*
* Creating an Scanner class object which is used to get the
inputs
* entered by the user
*/
Scanner sc = new Scanner(System.in);
//Getting the strings entered by the user
System.out.print("Enter string#1:");
str1 = sc.next();
System.out.print("Enter string#2:");
str2 = sc.next();
//calling the method by passing the user entered inputs as
arguments
try {
result = printSum(str1, str2);
//displaying the sum of two double values
System.out.println("The Sum is :" + result);
} catch (NumberFormatException nfe) {
System.out.println("Exception :" + nfe);
}
}
/* this method will find the double values in the string
* and add those two double values and return it
*/
private static double printSum(String str1, String str2) {
int i, j;
double d1, d2, res = 0.0;
i = (str1.indexOf("'"));
j = str1.indexOf("'", i + 1);
d1 = Double.parseDouble(str1.substring(i + 1, j));
i = (str2.indexOf("'"));
j = str2.indexOf("'", i + 1);
d2 = Double.parseDouble(str2.substring(i + 1, j));
res = d1 + d2;
return res;
}
}
__________________
Output:
Enter string#1:beauti'56.67'ful
Enter string#2:celeb'78.89'rations
The Sum is :135.56
_________________Thank You
[JAVA] Suppose we need to write code that receives String input from a user, and we...
IT Java code In Lab 8, we are going to re-write Lab 3 and add code to validate user input. The Body Mass Index (BMI) is a calculation used to categorize whether a person’s weight is at a healthy level for a given height. The formula is as follows: bmi = kilograms / (meters2) where kilograms = person’s weight in kilograms, meters = person’s height in meters BMI is then categorized as follows: Classification BMI Range Underweight Less...
Java Question, I need a program that asks a user to input a string && then asks the user to type in an index value(integer). You will use the charAt( ) method from the string class to find and output the character referenced by that index. Allow the user to repeat these actions by placing this in a loop until the user gives you an empty string. Now realize that If we call the charAt method with a bad value...
Write a program that reads a string from the keyboard and tests whether it contains a valid time. Display the time as described below if it is valid, otherwise display a message as described below. The input date should have the format hh:mm:ss (where hh = hour, mm = minutes and ss = seconds in a 24 hour clock, for example 23:47:55). Here are the input errors (exceptions) that your program should detect and deal with: Receive the input from...
Using Java write a program that takes a string input from the user and then outputs the first character, then the first two, then the first three, etc until it prints the entire word.
Write two Java classes. A Numbers class has five fields -- maximum, minimum, sum, count, and average. An input() method will prompt for and input a sequence of double inputs, negative to quit, calculating and updating the five fields as the doubles are input. A getMax() method returns the maximum of the doubles. A getMin() method returns the minimum of the doubles. A getSum() method returns the sum of the doubles. A getCount() method returns the number of doubles that...
Write a Java method that takes a String as its input and prints on the first line the odd characters (1st, 3rd, etc) and on the second line the even characters (2nd, 4th, etc).
in
java, finish the method
17: Chapter 8: Handling a number as different types (Method Overloading) J o pulli UCILISIS lule zyBooks catalog Help/FAQ 31.47 Chapter 8: Handling a number as different types (Method Overloading) Write an overloaded method called divide_by_two() that can handle a number between 1-5 as an int, double, or String. • The method should be able to handle the String version with upper or lower case letters in any location in the String. ACTIVITY 31.47.1: Chapter...
Question: Write a Java program for Evaluating Postfix Expression 1. Input a postfix expression from user. 2. Evaluate expression using double stack made of your own linked list. 3. Show the result of expression or error if incorrect. Evaluating Postfix Expression Input an expression: 2 10 + 9 6 - / Evaluating Postfix Expression Input an expression: 20 10 + 9 6 - 1 Evaluating Postfix Expression Input an expression: 2 10 + 9 - / Result = 4.0 Result...
Write a Java program which takes a string as a user input. Create 2 functions. The first function expects a string as argument and returns void. It converts all upper case characters to lower case and converts all lower case characters to upper case. It then prints out the converted string to a console. The second function also expects a string as argument and returns void. It will find first charactor that is repeated exactly 2 times in the string....
I need to create a code for this prompt: In this project we will build a generic UserInput class for getting keyboard input from the user. Implementation: The class UserInput is a 'Methods only' class, and all the methods should be declared static. Look at the TestScanner.java program at the bottom of this page that inputs a string, int and double. It shows you how to use Scanner class to get input from the keyboard. Write FOUR simple methods, one...