Program: Data visualization (Java): Please help me fix the issues with my curren
ID: 3784100 • Letter: P
Question
Program: Data visualization (Java): Please help me fix the issues with my current code to match up with the expected Input:
Current Code
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.println("Enter a title for the data: ");
String title = input.nextLine();
System.out.println("You entered: "+title);
//2
System.out.println("Enter the column 1 header: ");
String header1 = input.nextLine();
System.out.println("You entered: "+header1);
System.out.println("Enter the 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.println("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));
}
return;
}
}
My Input VS Expected Input
Thank you in advance for your help in solving where I am missing steps.
Explanation / Answer
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.println("Enter a title for the data: ");
String title = input.nextLine();
System.out.println("You entered: "+title);
//2
System.out.println("Enter the column 1 header: ");
String header1 = input.nextLine();
System.out.println("You entered: "+header1);
System.out.println("Enter the 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.println("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));
}
return;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.