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 numbers=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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!