Question: Variable currentWeights is declared as a HashSet. Then, integer numWeights is read from input representing the number of integers to be read next. For each

Variable currentWeights is declared as a HashSet. Then, integer numWeights is read from input representing the number of integers to be read next. For each integer read into variable weight:
Add weight to currentWeights.
If weight is successfully added, output "Weight registered: ", followed by weight.
Otherwise, output "Duplicate entered: ", followed by weight, and assign duplicateEntered with true.
End each output with a newline.
Ex: If the input is:
454155494
then the output is:
Weight registered: 54 Weight registered: 15 Duplicate entered: 54
import java.util.HashSet;
import java.util.Scanner;
public class Weights {
public static void main(String[] args){
Scanner scnr = new Scanner(System.in);
HashSet currentWeights = new HashSet();
int numWeights;
int weight;
boolean duplicateEntered;
numWeights = scnr.nextInt();
duplicateEntered = false;
while (numWeights >0){
if (!duplicateEntered){
weight = scnr.nextInt();
/* Your code goes here */
}
--numWeights;
}
}
}

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!