A program that generates random numbers that are between 1 a
A program that generates random numbers that are between 1 and 100, then searches for the values.
/****************************************************************************************************************************
Declare an array list to hold random numbers. ? Get input for how many random numbers to use. ? Using a “for loop”, add random numbers to the array list between 1 and 100. ? Get input for value to be searched. ? Using a “for-each loop” and a sequential search, search through the array list for the search term entered. 90% o Output “Unsorted list” and: o For each match found, output # found at location: # (see example) ? Use java collections to sort the array list. ? Using a “for-each loop” and a sequential search, search through the array list for the search term entered. o Output “Sorted list” and: o For each match found, output # found at location: # (see example) ? Only print the headers and values if the search item is found. Print the total value of all random numbers added together after the search has complete.
***********************************************************************************************************************/
Output sample
/******************************************************************************************
The code I came up with is below but is majorly flawed am sure that it is not right and running into plenty errors along the way.
import java.util.Scanner;
import java.util.Random;
import java.util.ArrayList;
public class ArrayListWork
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
Random rand = new Random();
ArrayList<Integer> array1 = new ArrayList<>(); // Array to contain unsorted list
int ele1,temp,sum = 0; // value holders
int search; // search value input
System.out.print(\"How many random numbers between 1 and 100? \"); // asking how many random numbers
search = scan.nextInt();
for (int i = 0;i < search; i++) // loop to match the search input
{
array1.add(rand.nextInt(100));
}
System.out.print(\"What random number are you searching for? \"); //asking for value to search for
ele1 = scan.nextInt();
System.out.print(\"Unsorted List\"); // print unsorted list
for (int j : array1) // for each
{
sum += array1.get(search); // searching for search value in unsorted list
if (j == ele1)
{
System.out.print(ele1 + \" found at location: \" +temp);
} // close if
ele1++;
} //close for each
Collections.sort(array1); // sorting the array1
System.out.print(\"Sorted list\");
for (int k : array1) // for each with sorted
{
if (k == ele1)
{
System.out.print(ele1 + \" found at location: \"+temp);
}// close
ele1++;
} // close
sum += array1;
System.out.print(\"Total of all numbers \" +sum);
if (ele1 != i) // if search value is not found in array list, outputs not printed
{
System.out.print(\"Total of all numbers \" +sum);
}// close if loop
} // end main
}// end class
Solution
import java.util.*;
public class Main2
{
public static void main(String args[])
{
ArrayList<Integer> a=new ArrayList<Integer>();
Scanner in=new Scanner(System.in);
System.out.println(\"Enter the number of random numbers to be generated between 1 and 100\");
int n=in.nextInt();
for(int i=0;i<n;i++)
{
int r = (int )(Math.random() * 100 + 1);
a.add(r);
}
System.out.println(\"Enter the number to be searched for ?\");
int search=in.nextInt();
System.out.println(\"UNSORTED LIST ELEMENTS ARE :\");
System.out.println(a);
int flag=0;
for (int i:a)
{
if (search == i)
{
flag=1;
int pos=a.indexOf(i);
System.out.println(\"ELEMENT FOUND AT POSITION \"+pos);
break;
}
}
if (flag==0)
{
System.out.println(\"ELEMENT NOT FOUND\");
}
Collections.sort(a);
System.out.println(\"SORTED LIST ELEMENTS ARE :\" +a);
int flag2=0;
for (int i:a)
{
if (search == i)
{
flag2=1;
int pos=a.indexOf(i);
System.out.println(\"ELEMENT FOUND AT POSITION \"+pos);
break;
}
}
if (flag2==0)
{
System.out.println(\"ELEMENT NOT FOUND\");
}
}
}


