Question: public class DataVisualizer { / / Main method public static void main ( String [ ] args ) { / / Create an object of

public class DataVisualizer
{
// Main method
public static void main(String[] args)
{
// Create an object of Scanner class
Scanner scan = new Scanner(System.in);
// Take the title for data
System.out.println("Enter a title for the data:");
String title = scan.nextLine();
System.out.println("You entered: "+ title);
System.out.println();
// Take the column 1 header
System.out.println("Enter the column 1 header:");
String header1= scan.nextLine();
System.out.println("You entered: "+ header1);
System.out.println();
// Take the column 2 header
System.out.println("Enter the column 2 header:");
String header2= scan.nextLine();
System.out.println("You entered: "+ header2);
System.out.println();
// Create an array list to store names
ArrayList name = new ArrayList();
// Create an array list to store number of novels
ArrayList numNovels = new ArrayList();
// Loop till the user enter -1 to exit
while(true)
{
// Take the data point (-1 to stop input)
System.out.println("Enter a data point (-1 to stop input):");
String data = scan.nextLine();
// If the user enter -1
if(data.equals("-1"))
{
// Exit from loop
break;
}
// If user enter data point
else
{
// Split the data point into array of string
String[] tokens = data.split(",");
// If length of strings is 1
if(tokens.length ==1)
// Print an error message
System.out.println("Error: No comma in string.
");
// If length of strings is greater than 2
else if(tokens.length >2)
// Print an error message
System.out.println("Error: Too many commas in input.
");
// If length of string is 2
else
{
// Try to convert second part into integer
try
{
// Convert the second string in to number
int count = Integer. parseInt(tokens[1]);
// Add name into names list
name.add(tokens[0]);
// Add number of novels
numNovels.add(count);
// Print string & integer
System.out.println("Data string: "+ tokens[0]);
System.out.println("Data integer: "+ tokens[1]);
System.out.println();
}
// If the second string is not integer
catch(NumberFormatException e)
{
// Print an error message
System.out.println("Error: Comma not followed by an integer.
");
}
}
}
}
// Print title, column 1 & column 2 header
System.out.printf("%33s
", title);
System.out.printf("%-20s|%23s
", header1, header2);
System.out.println("--------------------------------------------");
// Loop to print each novels name & number of novels
for(int i =0; i < name.size(); i++)
{
System.out.printf("%-20s|%23s
", name.get(i), numNovels.get(i));
}
System.out.println();
// Loop to print each novels with histigram of number of novels
for(int i =0; i < name.size(); i++)
{
System.out.printf("%20s ", name.get(i));
for(int j =0; j < numNovels.get(i); j++)
{
System.out.printf("*");
}
System.out.println();
}
}
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!