Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program in JAVA: Where the user is asked to enter his/her first name, la

ID: 3889895 • Letter: W

Question

Write a program in JAVA:

Where the user is asked to enter his/her first name, last name, and the amount of money they want to withdraw separated by spaces.

Use string manipulation functions.

Example:

Hey Jack Snow! You would like to withdraw $700.00. Thanks!

Can only use string manipulation methods (indexOf, substring) as well as parseFloat() and nextLine() methods

I tried using parseFloat() , but for some reason it would give me a result like 120.0 instead of 120.00.

Like such:

float m = Float.parseFloat(input.substring(input.lastIndexOf(" "), input.length()));

How can you change parseFloat to print 2 decimal places instead of 1 in order to print a correct dollar amount from the user input?

Explanation / Answer

Demo.java

import java.util.Scanner;

public class Demo {

public static void main(String[] args) {

//Declaring variables

String fname,lname,str;

float f;

/*

* Creating an Scanner class object which is used to get the inputs

* entered by the user

*/

Scanner sc = new Scanner(System.in);

System.out.print("Enter firstname,lastname and withdraw separated by spaces :");

str=sc.nextLine();

//Parsing the string

fname=str.substring(0, str.indexOf(' '));

lname=str.substring(str.indexOf(' ')+1, str.lastIndexOf(' '));

f=Float.parseFloat(str.substring(str.lastIndexOf(' ')+1,str.length()));

//Displaying the output

System.out.printf("Hey %s %s! You would like to withdraw $%.2f. Thanks!",fname,lname,f);

}

}

__________________

Output:

Enter firstname,lastname and withdraw separated by spaces :Jack Snow 700
Hey Jack Snow! You would like to withdraw $700.00. Thanks!

_____________Could you rate me well.Plz .Thank You

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote