Question

Program: Data visualization

5.10 LAB*: Program: Data visualization

(1) Prompt the user for a title for data. Output the title. (1 pt)

Ex:

Enter a title for the data:
Number of Novels Authored
You entered: Number of Novels Authored


(2) Prompt the user for the headers of two columns of a table. Output the column headers. (1 pt)

Ex:

Enter the column 1 header:
Author name
You entered: Author name

Enter the column 2 header:
Number of novels
You entered: Number of novels


(3) Prompt the user for data points. Data points must be in this format: string, int. Store the information before the comma into a string variable and the information after the comma into an integer. The user will enter -1 when they have finished entering data points. Output the data points. Store the string components of the data points in an ArrayList of strings. Store the integer components of the data points in a second ArrayList of integers. (4 pts)

Ex:

Enter a data point (-1 to stop input):
Jane Austen, 6
Data string: Jane Austen
Data integer: 6


(4) Perform error checking for the data point entries. If any of the following errors occurs, output the appropriate error message and prompt again for a valid data point.

  • If entry has no comma

    • Output: Error: No comma in string. (1 pt)

  • If entry has more than one comma

    • Output: Error: Too many commas in input. (1 pt)

  • If entry after the comma is not an integer

    • Output: Error: Comma not followed by an integer. (2 pts)


Ex:

Enter a data point (-1 to stop input):
Ernest Hemingway 9
Error: No comma in string.

Enter a data point (-1 to stop input):
Ernest, Hemingway, 9
Error: Too many commas in input.

Enter a data point (-1 to stop input):
Ernest Hemingway, nine
Error: Comma not followed by an integer.

Enter a data point (-1 to stop input):
Ernest Hemingway, 9
Data string: Ernest Hemingway
Data integer: 9


(5) Output the information in a formatted table. The title is right justified with a minimum of 33 characters. Column 1 is left justified with a minimum of 20 characters. Column 2 is right justified with a minimum of 23 characters. (3 pts)

Ex:

        Number of Novels Authored
Author name         |       Number of novels
--------------------------------------------
Jane Austen         |                      6
Charles Dickens     |                     20
Ernest Hemingway    |                      9
Jack Kerouac        |                     22
F. Scott Fitzgerald |                      8
Mary Shelley        |                      7
Charlotte Bronte    |                      5
Mark Twain          |                     11
Agatha Christie     |                     73
Ian Flemming        |                     14
J.K. Rowling        |                     14
Stephen King        |                     54
Oscar Wilde         |                      1


(6) Output the information as a formatted histogram. Each name is right justified with a minimum of 20 characters. (4 pts)

Ex:

         Jane Austen ******
     Charles Dickens ********************
    Ernest Hemingway *********
        Jack Kerouac **********************
 F. Scott Fitzgerald ********
        Mary Shelley *******
    Charlotte Bronte *****
          Mark Twain ***********
     Agatha Christie *************************************************************************
        Ian Flemming **************
        J.K. Rowling **************
        Stephen King ******************************************************
         Oscar Wilde *


2 0
Add a comment Improve this question Transcribed image text
✔ Recommended Answer
Answer #1
import java.util.Scanner;
import java.util.ArrayList;
import java.util.List;

public class DataVisualizer {
   
   public static void main(String[] args) {
      
      Scanner scnr = new Scanner(System.in);
      ArrayList authorName = new ArrayList();
      ArrayList numBooks = new ArrayList();
      
      String title;
      String col1;
      String col2;
      String dataPoints;
      int commaLocation;
      String author;
      String numNovels;
      int bookStars;
      
      System.out.println("Enter a title for the data:");
      title = scnr.nextLine();
      System.out.println("You entered: " + title);
      System.out.println();
      
      System.out.println("Enter the column 1 header:");
      col1 = scnr.nextLine();
      System.out.println("You entered: "+ col1);
      System.out.println();
      
      System.out.println("Enter the column 2 header:");
      col2 = scnr.nextLine();
      System.out.println("You entered: " + col2);
      System.out.println();
      
      do {
         
         System.out.println("Enter a data point (-1 to stop input):");
         dataPoints = scnr.nextLine();
         
         if (dataPoints.equals("-1")) {
            
            break;
            
         }
         
         else if (!dataPoints.contains(",")) {
            
            System.out.println("Error: No comma in string.");
            System.out.println();
            continue;
            
         }
         
         else if (!dataPoints.matches(".*\\d.*")) {
            
            System.out.println("Error: Comma not followed by an integer.");
            System.out.println();
            continue;
            
         }
         
         else if (dataPoints.contains(",")) {
            
            int count = 0;
            int location;
            
            for (int m = 0; m < dataPoints.length(); m++) {
               
               if(dataPoints.charAt(m) == ',') {
                  
                  count++;
                  
               }
               
            }
            
            if (count > 1) {
               
               System.out.println("Error: Too many commas in input.");
               System.out.println();
               continue;
               
            }
            
            else if (count == 1) {
               
               commaLocation = dataPoints.indexOf(',');
               author = dataPoints.substring(0, commaLocation);
               numNovels = dataPoints.substring(commaLocation + 1, dataPoints.length());
               
               author = author.trim().replaceAll(" +", " ");
               numNovels = numNovels.trim().replaceAll(" +", " ");
               
               System.out.println("Data string: " + author);
               System.out.println("Data integer: " + numNovels);
               System.out.println();
               
               authorName.add(author);
               numBooks.add(numNovels);
               
               continue;
               
            }
            
         }
         
         else {
            
            commaLocation = dataPoints.indexOf(',');
            author = dataPoints.substring(0, commaLocation);
            numNovels = dataPoints.substring(commaLocation + 1, dataPoints.length());
            
            author = author.trim().replaceAll(" +", " ");
            numNovels = numNovels.trim().replaceAll(" +", " ");
            
            System.out.println("Data string: " + author);
            System.out.println("Data integer: " + numNovels);
            System.out.println();
            
            authorName.add(author);
            numBooks.add(numNovels);
            
         }
         
      } while (!dataPoints.equals("-1"));
      
      System.out.println();
      System.out.printf("%33s\n", title);
      System.out.printf("%-20s|%23s\n", col1, col2);
      System.out.println("--------------------------------------------");
      
      for (int i = 0; i < authorName.size(); i++) {
         
         System.out.printf("%-20s|%23s\n", authorName.get(i), numBooks.get(i));
         
      }
      
      System.out.println();
      
      for (int j = 0; j < authorName.size(); j++) {
         
         System.out.printf("%20s ", authorName.get(j));
         
         bookStars = Integer.parseInt(numBooks.get(j));

         for (int k = 0; k < bookStars; k++) {
            
            System.out.print("*");
            
         }
         
         System.out.println();
         
      }
      
   }
   
}


Add a comment
Know the answer?
Add Answer to:
Program: Data visualization
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Similar Homework Help Questions
  • computer science . the programming language is python

    11.9 LAB*: Program: Data visualization(1) Prompt the user for a title for data. Output the title. (1 pt)Ex:Enter a title for the data: Number of Novels Authored You entered: Number of Novels Authored(2) Prompt the user for the headers of two columns of a table. Output the column headers. (1 pt)Ex:Enter the column 1 header: Author name You entered: Author name Enter the column 2 header: Number of novels You entered: Number of novels(3) Prompt the user for data points. Data points must be in this format: string, int. Store the information before the comma into a string variable and the information after the comma into an integer. The user will enter -1 when they...

  • Need help with this homework, and follow the bolded text required 7.10 LAB: Data Visualization (1) Write a function, get_data_headers(), to prompt the user for a title, and column headers for a table....

    Need help with this homework, and follow the bolded text required 7.10 LAB: Data Visualization (1) Write a function, get_data_headers(), to prompt the user for a title, and column headers for a table. Return a list of three strings, and print the title, and column headers. (2 pt) Ex: Enter a title for the data: Number of Novels Authored You entered: Number of Novels Authored Ex: Enter the column 1 header: Author name You entered: Author name Enter the column...

  • In this assignment you are going to handle some basic input operations including validation and manipulation,...

    In this assignment you are going to handle some basic input operations including validation and manipulation, and then some output operations to take some data and format it in a way that's presentable (i.e. readable to human eyes). Functions that you will need to use: getline(istream&, string&) This function allows you to get input for strings, including spaces. It reads characters up to a newline character (for user input, this would be when the "enter" key is pressed). The first...

  • **C programming Language 3) Prompt the user for data points. Data points must be in this...

    **C programming Language 3) Prompt the user for data points. Data points must be in this format: string, int. Store the information before the comma into a string variable and the information after the comma into an integer. The user will enter -1 when they have finished entering data points. Output the data points. Store the string components of the data points in an array of strings. Store the integer components of the data points in an array of integers....

  • Please help modify my C program to be able to answer these questions, it seems the...

    Please help modify my C program to be able to answer these questions, it seems the spacing and some functions arn't working as planeed. Please do NOT copy and paste other work as the answer, I need my source code to be modified. Source code: #include <stdio.h> #include <string.h> #include <ctype.h> #include <stdlib.h> int main(void) { char title[50]; char col1[50]; char col2[50]; int point[50]; char names[50][50]; printf("Enter a title for the data:\n"); fgets (title, 50, stdin); printf("You entered: %s\n", title);...

  • *In Python please***** This program will display some statistics, a table and a histogram of a...

    *In Python please***** This program will display some statistics, a table and a histogram of a set of cities and the population of each city. You will ask the user for all of the information. Using what you learned about incremental development, consider the following approach to create your program: Prompt the user for information about the table. First, ask for the title of this data set by prompting the user for a title for data, and then output the...

  • Python 9.13 LAB: Warm up: Parsing strings (1) Prompt the user for a string that contains two strings separated by a comm...

    Python 9.13 LAB: Warm up: Parsing strings (1) Prompt the user for a string that contains two strings separated by a comma. (1 pt) Examples of strings that can be accepted: Jill, Allen Jill , Allen Jill,Allen Ex: Enter input string: Jill, Allen (2) Report an error if the input string does not contain a comma. Continue to prompt until a valid string is entered. Note: If the input contains a comma, then assume that the input also contains two...

  • Warm up: Parsing strings

    5.9 LAB: Warm up: Parsing strings(1) Prompt the user for a string that contains two strings separated by a comma. (1 pt)Examples of strings that can be accepted:Jill, AllenJill , AllenJill,AllenEx:Enter input string: Jill, Allen(2) Report an error if the input string does not contain a comma. Continue to prompt until a valid string is entered. Note: If the input contains a comma, then assume that the input also contains two strings. (2 pts)Ex:Enter input string: Jill Allen Error: No comma in string. Enter input string: Jill, Allen(3) Extract the two words from the input string...

  • 6.6 Warm up: Parsing strings (Python 3) (1) Prompt the user for a string that contains...

    6.6 Warm up: Parsing strings (Python 3) (1) Prompt the user for a string that contains two strings separated by a comma. (1 pt) Examples of strings that can be accepted: Jill, Allen Jill , Allen Jill,Allen Ex: Enter input string: Jill, Allen (2) Report an error if the input string does not contain a comma. Continue to prompt until a valid string is entered. Note: If the input contains a comma, then assume that the input also contains two...

  • Create a program (Lab9_Act1_Write.py) that will read in data from the keyboard and store it in...

    Create a program (Lab9_Act1_Write.py) that will read in data from the keyboard and store it in a file. Your program should prompt the user for the name of the file to use and then request values be entered until the user is done. This data should be written to two files: a text file, user_file_name.txt and a comma separated value file, user_file_name.csv. To help you with decision making, we suggest that you ask the user to input data representing students’...

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