Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

import java.util.Scanner; import java.util.ArrayList; import java.io . ByteArrayOutputStream; import java.io . PrintStream; public class DataVisualizer { private final String title; private final String authorNameColumn; private

import java.util.Scanner;
import java.util.ArrayList;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
public class DataVisualizer {
private final String title;
private final String authorNameColumn;
private final String numNovelsColumn;
private final ArrayList dataPoints;
public DataVisualizer(String title, String authorNameColumn, String numNovelsColumn){
this.title = title;
this.authorNameColumn = authorNameColumn;
this.numNovelsColumn = numNovelsColumn;
this.dataPoints = new ArrayList>();
}
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a title for the data");
String title = scanner.nextLine();
System.out.println("You entered: "+ title +"
");
System.out.println("Enter the column 1 header:");
String c1Header = scanner.nextLine();
System.out.println("You entered: "+ c1Header +"
");
System.out.println("Enter the column 2 header");
String c2Header = scanner.nextLine();
System.out.println("You entered: "+ c2Header +"
");
DataVisualizer dv = new DataVisualizer(title, c1Header, c2Header);
boolean done = false;
do {
System.out.println("Enter a data point (Enter 1 to stop input):");
String input = scanner.nextLine();
if (input.equals("1")){
done = true;
} else {
try {
if (!input.contains(",")){
throw new Exception("Error: No comma in string.
");
}
if (input.lastIndexOf(".") input.indexOf(",")){
throw new Exception("Error: Too many commas in input.
");
}
String[] dataArray = input.split(",");
String author = dataArray[0].trim();
int novelQty = Integer.parseInt(dataArray[1].trim());
System.out.println("Data string:: "+ author);
System.out.println("Data interger: "+ novelQty"
");
);
dv.dataPoints.add(new DataPoint(author, novelQty));
} catch (NumberFormatException nfe){
System.out.println("Error: Comma not followed by an integer.
");
} catch (Exception e){
System.out.println(e.getMessage());
}
}
} while (!done);
System.out.println(dv);
}
private void printTable(){
System.out.printf("
%-33s%
" title);
System.out.printf("-20s%-23s
", authorNameColumn, numNovelsColumn);
System.out.println("----------------------");
for (DataPoint d : dataPoints){
System.out.printf("%-20s|%-23d
", d.author, d.novelQty);
}
}
private void printHistogram(){
for (DataPoint d : dataPoints){
System.out.printf("%20s ", d.author);
for (int i =0; i d.novelQty; i++){
System.out.print("*");
}
System.out.println();
}
}
@Override
public String toString(){
ByteArrayOutputStream os = new ByteArrayOutputStream();
PrintStream standardOut = System.out;
PrintStream stringOut = new PrintStream(os);
System.setOut(stringOut);
printTable();
System.out.println();
printHistogram();
System.out.flush();
System.setOut(standardOut);
return os.toString();
}
private static class DataPoint {
private final String author;
private final int novelQty;
public DataPoint(String author, int novelQty){
this.author = author;
this.novelQty = novelQty;
}
}
} can anyone help me fix my eroors
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

SQL Instant Reference

Authors: Gruber, Martin Gruber

2nd Edition

0782125395, 9780782125399

More Books

Students also viewed these Databases questions

Question

1. Make sure praise is tied directly to appropriate behavior.

Answered: 1 week ago

Question

Recognize and discuss the causes of culture shock

Answered: 1 week ago