Question? that will read a string containing four parts separated by colons such
ID: 3574065 • Letter: Q
Question
Question? that will read a string containing four parts separated by colons such as "Fundamentals of Java:Course Technology 15:4" entered by a user through a keyboard. The first string is a book title, the second string is a publisher, the 3rd string represents a sum ofratings(double), and the 4 string represents a number of reviews (int). The program should compute the average rating and print it out using two digits after the decimal point as: Fundamentals of Java of the publisher Course Technology has a rating of 375 Your program needs to work with any input string of this format ux yz") You can use any of the Scanner class or the split method in the String class to do this problem. You need to use Decimal Format class to format the average ratingExplanation / Answer
import java.text.DecimalFormat;
import java.util.Scanner;
public class Prod {
public static void main(String[] args) {
Scanner scanner = null;
try {
DecimalFormat format = new DecimalFormat("#.00");
scanner = new Scanner(System.in);
System.out.print("Ente the string:");
String line = scanner.nextLine();
String[] lineArr = line.split(":");
String title = lineArr[0];
String publisher = lineArr[1];
int ratingSum = Integer.parseInt(lineArr[2]);
int numberOfReviews = Integer.parseInt(lineArr[3]);
double averageRate = (double) ratingSum / (double) numberOfReviews;
System.out.println("Average Rating: " + format.format(averageRate));
} catch (Exception e) {
// TODO: handle exception
}
}
}
OUTPUT:
Ente the string:Fundemental of java:Course Technology:15:4
Average Rating: 3.75
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.