· 4 years ago · Mar 26, 2021, 10:14 PM
1import java.util.Scanner;
2import java.util.ArrayList;
3
4public class DataVisualization {
5
6 public static String promptAndPrint(Scanner scnr, String prompt){
7 String input = "";
8 System.out.println(prompt);
9 input = scnr.nextLine();
10 System.out.println("You entered: " + input);
11 System.out.println("");
12 return input;
13 }
14
15 public static void addDataToArray(ArrayList<String> dataString, ArrayList<Integer> dataInteger, String userInput, Scanner in){
16 String input = userInput.substring(0, userInput.indexOf(','));
17 int inputInt = in.nextInt();
18 dataString.add(input);
19 dataInteger.add(inputInt);
20 System.out.println("Data string: " + input);
21 System.out.println("Data integer: " + inputInt);
22 }
23
24 public static void outputToTable(String userTitle, String columnHeader1, String columnHeader2, ArrayList<String> dataString, ArrayList<Integer> dataInteger){
25 System.out.println("");
26 System.out.printf("%33s\n", userTitle);
27 System.out.printf("%-20s|%23s\n", columnHeader1, columnHeader2);
28 for (int i = 0; i < 44; i++) {
29 System.out.print("-");
30 }
31 System.out.println("");
32
33 for (int i = 0; i < dataString.size(); i++) {
34 System.out.printf("%-20s|%23d\n", dataString.get(i), dataInteger.get(i));
35 }
36 System.out.println("");
37 }
38
39 public static void outputHistogram(ArrayList<String> dataString, ArrayList<Integer> dataInteger){
40 for (int i = 0; i < dataString.size(); i++) {
41 System.out.printf("%20s ", dataString.get(i));
42 for (int j = 0; j < dataInteger.get(i); j++) {
43 System.out.print("*");
44 }
45 System.out.println("");
46 }
47 }
48 public static void main(String[] args) {
49 Scanner scnr = new Scanner(System.in);
50
51 String userTitle = ""; // Data Title
52 String columnHeader1 = ""; // Column 1 Title
53 String columnHeader2 = ""; // Column 2 Title
54 ArrayList<String> dataString = new ArrayList<String>(); // Column 1 Data
55 ArrayList<Integer> dataInteger = new ArrayList<Integer>(); // Column 2 Values
56 String userInput = ""; // User input for data
57 boolean quit = false; // Flag to indicate finish input (-1 input)
58
59 userTitle = promptAndPrint(scnr, "Enter a title for the data:");
60 columnHeader1 = promptAndPrint(scnr, "Enter the column 1 header:");
61 columnHeader2 = promptAndPrint(scnr, "Enter the column 2 header:");
62
63 // TODO: Turn this into a method?
64 while (!quit) {
65
66 System.out.println("\nEnter a data point (-1 to stop input):");
67 userInput = scnr.nextLine();
68 if (userInput.equals("-1")){
69 quit = true;
70 }
71
72 // if more than 1 comma exists in userInput
73 else if (userInput.indexOf(',') != -1 && userInput.indexOf(',', userInput.indexOf(',')+1) != -1){ // checks if there is a comma after the first comma
74 System.out.println("Error: Too many commas in input.");
75 }
76
77 // if there is only 1 comma
78 else if (userInput.indexOf(',') != -1) {
79 // create new Scanner object that takes userInput from comma onwards (i.e. integer)
80 Scanner in = new Scanner(userInput.substring(userInput.indexOf(',') + 1));
81
82 // if there is integer then add all information into ArrayList
83 if (in.hasNextInt()) {
84 addDataToArray(dataString, dataInteger, userInput, in);
85 }
86
87 else {
88 System.out.println("Error: Comma not followed by an integer.");
89 }
90 }
91
92 else if (userInput.indexOf(',') == -1) {
93 System.out.println("Error: No comma in string.");
94 }
95 }
96
97 // Output information into formatted table
98 outputToTable(userTitle, columnHeader1, columnHeader2, dataString, dataInteger);
99
100 // Output information as a formatted histogram
101 outputHistogram(dataString, dataInteger);
102
103 }
104}
105