Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java: Using BufferedReader, Write a user entry method that asks the user to ente

ID: 3886196 • Letter: J

Question

Java:

Using BufferedReader,

Write a user entry method that asks the user to enter an whole number. You should use the appropriate loop to ask the user repeatedly until the user decides to quit. You will need to decide what sentinel value will cause the loop to end.

Store the number in an Integer object.

Append the Integer object to the end of the ArrayList.

When the user quits, print out:

the number of ArrayList slots used, and

a list of the ints contained within the Integers stored in the ArrayList.

Explanation / Answer

import java.io.*;
import java.util.*;
import java.lang.*;
class ttr
{
public static void main(String []args)throws IOException
{
int n,c=0,d;
String ch="";
ArrayList<Integer> arr = new ArrayList<Integer>(); //the arraylist
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
do
{
System.out.println("Enter a number"); //accepting the numbers from the user
n=Integer.parseInt(br.readLine());
arr.add(n); //adding it to the list
c=c+1; //counting the number of index filles
System.out.println("Do you want to enter another number (0=NO/1=YES)");
d=Integer.parseInt(br.readLine());
}while(d!=0);

System.out.println("The numbers are"); //printing the details.
System.out.println(arr);
System.out.println("Number of elements in the list "+c);
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote