Really need help in understanding how to write out this code properly. It is kin
ID: 3783915 • Letter: R
Question
Really need help in understanding how to write out this code properly. It is kind of long. Thank you in advance for any help received.
5.7 Program: Data visualization (Java)
Sample Code:
import java.util.Scanner;
import java.util.ArrayList;
public class DataVisualizer {
public static void main(String[] args) {
/* Type your code here. */
return;
}
}
Explanation / Answer
HI, Please find my implementation.
Please let me know in case of any issue.
import java.util.Scanner;
import java.util.ArrayList;
public class DataVisualizer {
public static void main(String[] args) {
/* Type your code here. */
Scanner input = new Scanner(System.in);
//1
System.out.print("Enter title for the data: ");
String title = input.nextLine();
System.out.println("You entered: "+title);
//2
System.out.print("Enter e column 1 header: ");
String header1 = input.nextLine();
System.out.println("You entered: "+header1);
System.out.print("Enter e column 2 header: ");
String header2 = input.nextLine();
System.out.println("You entered: "+header2);
//3.
ArrayList<String> strList = new ArrayList<>();
ArrayList<Integer> intList = new ArrayList<>();
while(true){
System.out.print("Enter a data point (-1 to stop input): ");
String point = input.nextLine();
if("-1".equalsIgnoreCase(point.trim()))
break;
int index = point.indexOf(',');
// if there is a comma in input
if(index != -1){
String pointStr = point.substring(0, index).trim();
String str = point.substring(index+1).trim();
// if only one comma is in input
if(str.indexOf(',') == -1){
try{
int x = Integer.parseInt(str);
System.out.println("Data string: "+pointStr);
System.out.println("Data integer: "+x);
// adding in list
strList.add(pointStr);
intList.add(x);
}catch(Exception e){
System.out.println("Comma not followed by an integer");
}
}else{
System.out.println("To many commas in input");
}
}else{
System.out.println("No comma in string");
}
}
System.out.println();
System.out.println(title);
System.out.println(header1+" | "+header2);
for(int i=0; i<intList.size(); i++){
System.out.println(strList.get(i)+" | "+intList.get(i));
}
}
}
/*
Sample run:
Enter title for the data: Number of Novels Authored
You entered: Number of Novels Authored
Enter e column 1 header: Author name
You entered: Author name
Enter e column 2 header: Number of novels
You entered: Number of novels
Enter a data point (-1 to stop input): Jane Austen, 9
Data string: Jane Austen
Data integer: 9
Enter a data point (-1 to stop input): Ernest Hig, 8
Data string: Ernest Hig
Data integer: 8
Enter a data point (-1 to stop input): Alex Bob 5
No comma in string
Enter a data point (-1 to stop input): -1
Number of Novels Authored
Author name | Number of novels
Jane Austen | 9
Ernest Hig | 8
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.