Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Introduction to Wireless and Mobile Systems

Authors: Dharma P. Agrawal, Qing An Zeng

4th edition

1305087135, 978-1305087132, 9781305259621, 1305259629, 9781305537910 , 978-130508713

More Books

Students also viewed these Programming questions

Question

never there is a

Answered: 1 week ago

Question

1. Which is the most abundant gas presented in the atmosphere?

Answered: 1 week ago