I have the code provided; however, stuck on making the display output all search
ID: 674387 • Letter: I
Question
I have the code provided; however, stuck on making the display output all searched items to reflect. Also, any tips to realign the Unsorted and Sorted Arrays data in their respective table would be great too. Here's what I have:
import java.util.Random;
import java.util.Arrays;
import java.util.Scanner;
public class DCUnit1Ch9 {
public static void main(String[] args) {
Scanner stdIn = new Scanner(System.in);
int[] unsortedArray = new int[10];
int[] sortedArray = new int[10];
Random number = new Random();
for(int i = 0; i < 10; i++)
{
unsortedArray[i] = number.nextInt(20) + 1;
}
for(int i = 0; i < 10; i++)
{
sortedArray[i] = unsortedArray[i];
}
Arrays.sort(sortedArray);
System.out.println("Unsorted Array Sorted Array");
for(int i=0; i < 10; i++)
{
System.out.println(unsortedArray[i] + " "+ sortedArray[i]);
}
System.out.print("Please enter a number to search for: ");
Scanner scan = new Scanner(System.in);
int num = scan.nextInt();
int location1 = -1;
for(int i = 0; i < 10; i++){
if(unsortedArray[i] == num)
{
location1 = i;
}
if(sortedArray[i] == num)
{
location1 = i;
}
}
if(location1 == -1){
System.out.println("Search Value: " + num + " was not found");
}
else{
System.out.println("Search Value: " + num + " is found at location: " + (location1 + 1) +" in the unsorted array");
System.out.println("Search Value: " + num + " is found at location: " + (location1 + 1) +" in the sorted array");
}
}
}
Explanation / Answer
//Look for the comments that show what changes have been made.
import java.util.Random;
import java.util.Arrays;
import java.util.Scanner;
public class DCUnit1Ch9 {
public static void main(String[] args) {
Scanner stdIn = new Scanner(System.in);
int[] unsortedArray = new int[10];
int[] sortedArray = new int[10];
Random number = new Random();
for(int i = 0; i < 10; i++)
{
unsortedArray[i] = number.nextInt(20) + 1;
}
for(int i = 0; i < 10; i++)
{
sortedArray[i] = unsortedArray[i];
}
Arrays.sort(sortedArray);
System.out.println("Unsorted Array Sorted Array");
for(int i=0; i < 10; i++)
{
System.out.println(unsortedArray[i] + " "+ sortedArray[i]);
}
System.out.print("Please enter a number to search for: ");
Scanner scan = new Scanner(System.in);
int num = scan.nextInt();
int location1 = -1;
for(int i = 0; i < 10; i++){
if(unsortedArray[i] == num)
{
location1 = i;
System.out.println("Search Value: " + num + " is found at location: " + (location1 + 1) +" in the unsorted array");
}
}
for(int i = 0; i < 10; i++){ //Added new for loop to check for the number in sortedArray separately so as to print the output for unsortedArray first using previous loop and then for sortedArray using this loop
if(sortedArray[i]==num)
{
location1=i;
System.out.println("Search Value: " + num + " is found at location: " + (location1 + 1) +" in the sorted array");
}
}
if(location1 == -1) //Added this part here to checks if the number is not found in either of the arrays, else part is not required
System.out.println("Search Value: " + num + " was not found");
//Remove the else part that i have turned into comments below and include the output statements in the loops
//else{
//System.out.println("Search Value: " + num + " is found at location: " + (location1 + 1) +" in the unsorted array");
//System.out.println("Search Value: " + num + " is found at location: " + (location1 + 1) +" in the sorted array");
//}
}
}