Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This fragment of code takes user input for a set of numbers. I want to revise it

ID: 3594482 • Letter: T

Question

This fragment of code takes user input for a set of numbers. I want to revise it to include the requirement that any number can only be entered once. Any suggestions on how to do so without having to extensively rewrite everything below? Thanks.  

String numbers;

nt a;

String[] arr; // takes user input for elements and puts them into an array System.out.println("Please enter a set of integers

numbers = scan.nextLine();

arr = numbers.split(" ");

a = (int)Math.sqrt(arr.length);

// fills two dimensional array with user input of numbers

int b = 0;

int[][] matrix = new int[a][a];

for(int row = 0; row < a; row++) {

for(int col = 0; col < a; col++) {

matrix[row][col] = Integer.parseInt(arr[b]);

b++;

}

}

Explanation / Answer

You can simply add a code snippet which simply checks the each and every element enterred and is present in the split array is present in the two dimensional array or not. If the number is found in the two dimensional array then that number will be skipped and the next number in the one dimensional array will be enterred in the two dimensional array.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote