413,374 views
14 votes
14 votes
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)

User Prageeth Jayathissa
by
2.4k points

1 Answer

16 votes
16 votes

Answer:

In Java:

import java.util.*;

public class Main{

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

ArrayList<String> Strs = new ArrayList<String>(); ArrayList<Integer> Ints = new ArrayList<Integer>();

String datapoint;

System.out.print("Data point (0 to quit): ");

datapoint = input.nextLine();

while(!"0".equals(datapoint)){

System.out.println("Data point: "+datapoint);

String[] datapointSplit = datapoint.split(", ");

Strs.add(datapointSplit[0]);

Ints.add(Integer.parseInt(datapointSplit[1]));

System.out.print("Data point (0 to quit): ");

datapoint = input.nextLine(); }

System.out.println("Strings: "+Strs);

System.out.println("Integers: "+Ints);

}}

Step-by-step explanation:

In Java:

Declare string and integer array lists

ArrayList<String> Strs = new ArrayList<String>(); ArrayList<Integer> Ints = new ArrayList<Integer>();

Declare datapoint as string

String datapoint;

Prompt the user for data point (An entry of 0 ends the program)

System.out.print("Data point (0 to quit): ");

Get the data point

datapoint = input.nextLine();

This is repeated until an entry of 0 is entered

while(!"0".equals(datapoint)){

This prints the datapoint

System.out.println("Data point: "+datapoint);

This splits the datapoint to 2

String[] datapointSplit = datapoint.split(", ");

This adds the string part to the string array list

Strs.add(datapointSplit[0]);

This adds the integer part to the integer array list

Ints.add(Integer.parseInt(datapointSplit[1]));

Prompt the user for another entry

System.out.print("Data point (0 to quit): ");

datapoint = input.nextLine(); }

Print the string entries

System.out.println("Strings: "+Strs);

Print the integer entries

System.out.println("Integers: "+Ints);

User Maxhallinan
by
2.8k points