Question

[JAVA] Suppose we need to write code that receives String input from a user, and we...

[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.

0 0
Add a comment Improve this question Transcribed image text
Answer #1


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

Add a comment
Know the answer?
Add Answer to:
[JAVA] Suppose we need to write code that receives String input from a user, and we...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • IT Java code In Lab 8, we are going to re-write Lab 3 and add code...

    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...

    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...

    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...

    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.

  • Java code please. Please help * 95: Write a public class named TollInts with the following....

    Java code please. Please help * 95: Write a public class named TollInts with the following. * -A public constructor that takes 2 doubles as inputs. * -A public method named compute that takes no parameters and returns the the tangent of the * first constructor input subtracted by the the square root of the second constructor input * as a double. You will need to store the constructor input in instance variables to be able * to access them...

  • need java code for this question Question 2 (15 marks) (a) Does the following class successfully...

    need java code for this question Question 2 (15 marks) (a) Does the following class successfully compile? Explain your answer. public class MyClass public static void main(String arge) if(Integer.parseInt(args[0]) < 0) throw new RuntimeException(); C { 1 } If the class does compile, describe what will happen when we run it with command: java MyClass -10 (5 marks) (b) Write a complete definition of the method with the heading given below: public static double calculate insurance Premium double carValue, int...

  • Write a Java method that takes a String as its input and prints on the first...

    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).

  • I need java code for the following problem. Lab 7: Methods 1. Write a Java program called Numbers that calls the following methods and displays the returned value: Write a method called cubelt that a...

    I need java code for the following problem. Lab 7: Methods 1. Write a Java program called Numbers that calls the following methods and displays the returned value: Write a method called cubelt that accepts one integer parameter and returns the value raised to the third power as an integer. o Write a method called randominRange that accepts two integer parameters representing a range. The method returns a random integer in the specified range inclusive. 2. o Write a method...

  • in java, finish the method 17: Chapter 8: Handling a number as different types (Method Overloading)...

    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...

  • Write a Java program which takes a string as a user input. Create 2 functions. The...

    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....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT