SlideShare a Scribd company logo
1 of 2
Download to read offline
Write a program, Loopy2, that uses a loop to collect an arbitrary number of integers from the
user (until they enter “Q” like in the lecture slides) and stores those integers in an ArrayList.
Then print out the values in the ArrayList.
Solution
Loopy2.java
import java.util.ArrayList;
import java.util.Scanner;
public class Loopy2 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
ArrayList list = new ArrayList();
System.out.println("Enter an arbitrary number of integers: ");
for(;;){
String s = scan.next();
if(s.equalsIgnoreCase("Q")){
break;
}
else{
list.add(Integer.valueOf(s));
}
}
System.out.println("ArrayList values are: ");
for(int i=0;i < list.size(); i++){
System.out.println(list.get(i));
}
}
}
Output:
Enter an arbitrary number of integers:
1
2
3
4
5
Q
ArrayList values are:
1
2
3
4
5

More Related Content

Similar to Write a program, Loopy2, that uses a loop to collect an arbitrary nu.pdf

CountStringCharacters.javaimport java.util.Scanner; public cla.pdf
CountStringCharacters.javaimport java.util.Scanner; public cla.pdfCountStringCharacters.javaimport java.util.Scanner; public cla.pdf
CountStringCharacters.javaimport java.util.Scanner; public cla.pdfpremsrivastva8
 
Java Programs Lab File
Java Programs Lab FileJava Programs Lab File
Java Programs Lab FileKandarp Tiwari
 
UserInputHandlerjava package midterm2023 import javautil.pdf
UserInputHandlerjava package midterm2023 import javautil.pdfUserInputHandlerjava package midterm2023 import javautil.pdf
UserInputHandlerjava package midterm2023 import javautil.pdfadityknits
 
Basic java, java collection Framework and Date Time API
Basic java, java collection Framework and Date Time APIBasic java, java collection Framework and Date Time API
Basic java, java collection Framework and Date Time APIjagriti srivastava
 
Code javascript
Code javascriptCode javascript
Code javascriptRay Ray
 
Lab01.pptx
Lab01.pptxLab01.pptx
Lab01.pptxKimVeeL
 
CountPositiveNumbersInArray.javapackage org.students;import java.pdf
CountPositiveNumbersInArray.javapackage org.students;import java.pdfCountPositiveNumbersInArray.javapackage org.students;import java.pdf
CountPositiveNumbersInArray.javapackage org.students;import java.pdfaparnatiwari291
 
import java.util.;public class Program{public static void.pdf
import java.util.;public class Program{public static void.pdfimport java.util.;public class Program{public static void.pdf
import java.util.;public class Program{public static void.pdfoptokunal1
 

Similar to Write a program, Loopy2, that uses a loop to collect an arbitrary nu.pdf (15)

3.Lesson Plan - Input.pdf.pdf
3.Lesson Plan - Input.pdf.pdf3.Lesson Plan - Input.pdf.pdf
3.Lesson Plan - Input.pdf.pdf
 
CountStringCharacters.javaimport java.util.Scanner; public cla.pdf
CountStringCharacters.javaimport java.util.Scanner; public cla.pdfCountStringCharacters.javaimport java.util.Scanner; public cla.pdf
CountStringCharacters.javaimport java.util.Scanner; public cla.pdf
 
Java Programs Lab File
Java Programs Lab FileJava Programs Lab File
Java Programs Lab File
 
UserInputHandlerjava package midterm2023 import javautil.pdf
UserInputHandlerjava package midterm2023 import javautil.pdfUserInputHandlerjava package midterm2023 import javautil.pdf
UserInputHandlerjava package midterm2023 import javautil.pdf
 
Basic java, java collection Framework and Date Time API
Basic java, java collection Framework and Date Time APIBasic java, java collection Framework and Date Time API
Basic java, java collection Framework and Date Time API
 
Code javascript
Code javascriptCode javascript
Code javascript
 
Lab01.pptx
Lab01.pptxLab01.pptx
Lab01.pptx
 
CountPositiveNumbersInArray.javapackage org.students;import java.pdf
CountPositiveNumbersInArray.javapackage org.students;import java.pdfCountPositiveNumbersInArray.javapackage org.students;import java.pdf
CountPositiveNumbersInArray.javapackage org.students;import java.pdf
 
Java final lab
Java final labJava final lab
Java final lab
 
Computer programming 2 chapter 1-lesson 2
Computer programming 2  chapter 1-lesson 2Computer programming 2  chapter 1-lesson 2
Computer programming 2 chapter 1-lesson 2
 
Java programs
Java programsJava programs
Java programs
 
import java.util.;public class Program{public static void.pdf
import java.util.;public class Program{public static void.pdfimport java.util.;public class Program{public static void.pdf
import java.util.;public class Program{public static void.pdf
 
Java file
Java fileJava file
Java file
 
Java file
Java fileJava file
Java file
 
Java -lec-5
Java -lec-5Java -lec-5
Java -lec-5
 

More from sivakumar19810

You have a business customer who has a large number of graphics file.pdf
You have a business customer who has a large number of graphics file.pdfYou have a business customer who has a large number of graphics file.pdf
You have a business customer who has a large number of graphics file.pdfsivakumar19810
 
Why is biasing a transistor importantSolutionThe process of f.pdf
Why is biasing a transistor importantSolutionThe process of f.pdfWhy is biasing a transistor importantSolutionThe process of f.pdf
Why is biasing a transistor importantSolutionThe process of f.pdfsivakumar19810
 
Which command is used to add interfaces to VLAN 5A. switchport mo.pdf
Which command is used to add interfaces to VLAN 5A. switchport mo.pdfWhich command is used to add interfaces to VLAN 5A. switchport mo.pdf
Which command is used to add interfaces to VLAN 5A. switchport mo.pdfsivakumar19810
 
What is the difference between bio-accessibility and adsorption.pdf
What is the difference between bio-accessibility and adsorption.pdfWhat is the difference between bio-accessibility and adsorption.pdf
What is the difference between bio-accessibility and adsorption.pdfsivakumar19810
 
What is a biodiversity hot spot Do some research, and identify such.pdf
What is a biodiversity hot spot Do some research, and identify such.pdfWhat is a biodiversity hot spot Do some research, and identify such.pdf
What is a biodiversity hot spot Do some research, and identify such.pdfsivakumar19810
 
TrueFalse In testing for the existence of a linear relationship be.pdf
TrueFalse In testing for the existence of a linear relationship be.pdfTrueFalse In testing for the existence of a linear relationship be.pdf
TrueFalse In testing for the existence of a linear relationship be.pdfsivakumar19810
 
TransactionsOn June 1 of the current year, Chad Wilson established.pdf
TransactionsOn June 1 of the current year, Chad Wilson established.pdfTransactionsOn June 1 of the current year, Chad Wilson established.pdf
TransactionsOn June 1 of the current year, Chad Wilson established.pdfsivakumar19810
 
There is the DNA stored Where is RNA made How does it get to wher.pdf
There is the DNA stored  Where is RNA made How does it get to wher.pdfThere is the DNA stored  Where is RNA made How does it get to wher.pdf
There is the DNA stored Where is RNA made How does it get to wher.pdfsivakumar19810
 

More from sivakumar19810 (8)

You have a business customer who has a large number of graphics file.pdf
You have a business customer who has a large number of graphics file.pdfYou have a business customer who has a large number of graphics file.pdf
You have a business customer who has a large number of graphics file.pdf
 
Why is biasing a transistor importantSolutionThe process of f.pdf
Why is biasing a transistor importantSolutionThe process of f.pdfWhy is biasing a transistor importantSolutionThe process of f.pdf
Why is biasing a transistor importantSolutionThe process of f.pdf
 
Which command is used to add interfaces to VLAN 5A. switchport mo.pdf
Which command is used to add interfaces to VLAN 5A. switchport mo.pdfWhich command is used to add interfaces to VLAN 5A. switchport mo.pdf
Which command is used to add interfaces to VLAN 5A. switchport mo.pdf
 
What is the difference between bio-accessibility and adsorption.pdf
What is the difference between bio-accessibility and adsorption.pdfWhat is the difference between bio-accessibility and adsorption.pdf
What is the difference between bio-accessibility and adsorption.pdf
 
What is a biodiversity hot spot Do some research, and identify such.pdf
What is a biodiversity hot spot Do some research, and identify such.pdfWhat is a biodiversity hot spot Do some research, and identify such.pdf
What is a biodiversity hot spot Do some research, and identify such.pdf
 
TrueFalse In testing for the existence of a linear relationship be.pdf
TrueFalse In testing for the existence of a linear relationship be.pdfTrueFalse In testing for the existence of a linear relationship be.pdf
TrueFalse In testing for the existence of a linear relationship be.pdf
 
TransactionsOn June 1 of the current year, Chad Wilson established.pdf
TransactionsOn June 1 of the current year, Chad Wilson established.pdfTransactionsOn June 1 of the current year, Chad Wilson established.pdf
TransactionsOn June 1 of the current year, Chad Wilson established.pdf
 
There is the DNA stored Where is RNA made How does it get to wher.pdf
There is the DNA stored  Where is RNA made How does it get to wher.pdfThere is the DNA stored  Where is RNA made How does it get to wher.pdf
There is the DNA stored Where is RNA made How does it get to wher.pdf
 

Recently uploaded

Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Pooja Bhuva
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxmarlenawright1
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17Celine George
 
Simple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdfSimple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdfstareducators107
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxPooja Bhuva
 
Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111GangaMaiya1
 
Introduction to TechSoup’s Digital Marketing Services and Use Cases
Introduction to TechSoup’s Digital Marketing  Services and Use CasesIntroduction to TechSoup’s Digital Marketing  Services and Use Cases
Introduction to TechSoup’s Digital Marketing Services and Use CasesTechSoup
 
dusjagr & nano talk on open tools for agriculture research and learning
dusjagr & nano talk on open tools for agriculture research and learningdusjagr & nano talk on open tools for agriculture research and learning
dusjagr & nano talk on open tools for agriculture research and learningMarc Dusseiller Dusjagr
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxannathomasp01
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17Celine George
 
How to Add a Tool Tip to a Field in Odoo 17
How to Add a Tool Tip to a Field in Odoo 17How to Add a Tool Tip to a Field in Odoo 17
How to Add a Tool Tip to a Field in Odoo 17Celine George
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdf
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdfUGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdf
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdfNirmal Dwivedi
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxJisc
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - Englishneillewis46
 
What is 3 Way Matching Process in Odoo 17.pptx
What is 3 Way Matching Process in Odoo 17.pptxWhat is 3 Way Matching Process in Odoo 17.pptx
What is 3 Way Matching Process in Odoo 17.pptxCeline George
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxJisc
 

Recently uploaded (20)

OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17
 
Simple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdfSimple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdf
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111
 
VAMOS CUIDAR DO NOSSO PLANETA! .
VAMOS CUIDAR DO NOSSO PLANETA!                    .VAMOS CUIDAR DO NOSSO PLANETA!                    .
VAMOS CUIDAR DO NOSSO PLANETA! .
 
Introduction to TechSoup’s Digital Marketing Services and Use Cases
Introduction to TechSoup’s Digital Marketing  Services and Use CasesIntroduction to TechSoup’s Digital Marketing  Services and Use Cases
Introduction to TechSoup’s Digital Marketing Services and Use Cases
 
dusjagr & nano talk on open tools for agriculture research and learning
dusjagr & nano talk on open tools for agriculture research and learningdusjagr & nano talk on open tools for agriculture research and learning
dusjagr & nano talk on open tools for agriculture research and learning
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
How to Add a Tool Tip to a Field in Odoo 17
How to Add a Tool Tip to a Field in Odoo 17How to Add a Tool Tip to a Field in Odoo 17
How to Add a Tool Tip to a Field in Odoo 17
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdf
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdfUGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdf
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdf
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
What is 3 Way Matching Process in Odoo 17.pptx
What is 3 Way Matching Process in Odoo 17.pptxWhat is 3 Way Matching Process in Odoo 17.pptx
What is 3 Way Matching Process in Odoo 17.pptx
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
Our Environment Class 10 Science Notes pdf
Our Environment Class 10 Science Notes pdfOur Environment Class 10 Science Notes pdf
Our Environment Class 10 Science Notes pdf
 

Write a program, Loopy2, that uses a loop to collect an arbitrary nu.pdf

  • 1. Write a program, Loopy2, that uses a loop to collect an arbitrary number of integers from the user (until they enter “Q” like in the lecture slides) and stores those integers in an ArrayList. Then print out the values in the ArrayList. Solution Loopy2.java import java.util.ArrayList; import java.util.Scanner; public class Loopy2 { public static void main(String[] args) { Scanner scan = new Scanner(System.in); ArrayList list = new ArrayList(); System.out.println("Enter an arbitrary number of integers: "); for(;;){ String s = scan.next(); if(s.equalsIgnoreCase("Q")){ break; } else{ list.add(Integer.valueOf(s)); } } System.out.println("ArrayList values are: "); for(int i=0;i < list.size(); i++){ System.out.println(list.get(i)); } } } Output: Enter an arbitrary number of integers: 1 2 3