Improve java.util.ArrayList; Improve java.util.Scanner; public class question5A
ID: 3841469 • Letter: I
Question
Improve java.util.ArrayList; Improve java.util.Scanner; public class question5A {Public static void main (String [args]); ArrayList list = new ArrayList ; Scanner in = new Scanner (System.in); System.out.print ("Do you want to enter a new string, if yes enter 1 else enter 0:}; while(in.next []); {list.add(in.next []); System.out.print.{"Do you want to enter a new string, if yes enter 1 else enter 0: "}:} System.out.print{"The array contains; " + list.size []' elements."); for(String s: list) {if (I = = 0) System.out.print {"|" + s}; I ++;}Explanation / Answer
The corrrections in the code are written in red color and made bold
import java.util.ArrayList;
import java.util.Scanner;
public class Question5A
{
public static void main (String [] args)
{
ArrayList<Integer> list = new ArrayList<Integer>();
Scanner in = new Scanner(System.in);
System.out.println("Do you want to enter a new string, if yes enter 1 else enter 0:");
int n = in.nextInt();
if(n == 1)
{
list.add(n);
System.out.println("Do you want to enter a new string, if yes enter 1 else enter 0: ");
n = in.nextInt();
}
System.out.println("The array contains:" +list.size()+ "element.");
int i=0;
for(int s: list)
{
if(i==0)
System.out.print(s);
else
System.out.print(" | " +s);
i++;
}
}
}
import java.util.ArrayList;
import java.util.Scanner;
public class Question5A
{
public static void main (String [] args)
{
ArrayList<Integer> list = new ArrayList<Integer>();
Scanner in = new Scanner(System.in);
System.out.println("Do you want to enter a new string, if yes enter 1 else enter 0:");
int n = in.nextInt();
if(n == 1)
{
list.add(n);
System.out.println("Do you want to enter a new string, if yes enter 1 else enter 0: ");
n = in.nextInt();
}
System.out.println("The array contains:" +list.size()+ "element.");
int i=0;
for(int s: list)
{
if(i==0)
System.out.print(s);
else
System.out.print(" | " +s);
i++;
}
}
}