Question
Fix the following code so that the program runs. The comments (in red) tell you what the code should be doing. import java.util.ArrayList; import java.util.Scanner;
Fix the following code so that the program runs. The comments (in red) tell you what the code
should be doing.
import java.util.ArrayList;
import java.util.Scanner;
public class ArrFix {
/*This method should return an integer entered by the user*/
public static void getUserInput()
{
Scanner in=new Scanner(System.in);
System.out.println("Enter a number or -1 to quit.");
boolean done=true;
int n=0;
/*This while loop should run until the user enters a number-once a
number is entered, the loop should terminate*/
while(done)
{
try{
n=in.nextInt();
}
catch(Exception e)
{
System.out.println("That is not a number. Please enter an number.");
in.nextLine();
}
}
return done;
}
/*This method should take an ArrayList of integers and print out
all elements*/
public static printOutList(a)
{
System.out.println("Currently in the list:");
for(int i=0;i { /*Each element is printed out*/ System.out.printf("%d ", a); } System.out.println(" "); } public static void main(String [] args){ /*Declare an ArrayList of integers*/ ArrayList int n; do{ n=getUserInput(); if(n==-1) { System.out.println("Bye!"); return; } /*Check if the ArrayList already has the number entered by the user*/ else if(numbers.) { System.out.println("Already have this number."); } /*Add the number given by the user to the ArrayList */ else { numbers+n; } printOutList(numbers); } while(n!=-1); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started