Searching element in aray - java

I'm trying to create a program that asks the user the number of elements inside an array and prints its original list reverse list and assending. In, addition I'm also trying to find an element inside an array and print found and the index number if it is inside the array. So far here is my code:
public static void main(String[] args){
int n, y, z, temp=0;
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of elements you want to store here: ");
n = sc.nextInt();
int[] a = new int[10];
System.out.println("Enter elements here: ");
for(int i=0;i<n;i++){
a[i]=sc.nextInt();
}
System.out.print("\nOriginal list: ");
for(int i=0;i<n;i++){
System.out.print(a[i]+" ");
}
System.out.print("\nReverse list: ");
for(int i=n-1;i>=0;i--){
System.out.print(a[i]+" ");
}
// it seems to copy the assending list. It should print the integers the way user entered it
System.out.print("\nOriginal list: ");
for(int i=0;i<n;i++){
System.out.print(a[i]+" ");
}
for(y=0;y<n;y++){
for(z=y+1;z<n;z++){
if(a[y]>a[z]){
temp=a[y];
a[y]=a[z];
a[z]=temp;
}
}
}
{System.out.print("\nAscending Order list: ");
for(y=0;y<n;y++){
System.out.print(""+a[y]+" ");
}}
System.out.print("\nEnter the element you want to search: ");
s = sc.nextInt();
for(y=0;y<n;y++)
{
if(a[y]==s)
{
System.out.println("Element "+s+" is in "+y+" index");
f=1;
}
}
if(f==0)
{
System.out.println("Element "+s+" is not found");
}
}
}
My problem is the last original list.

First of all, you are taking an array not a list. Here you were not taking the user-defined array size. Rather you were taking an array of 10 elemets. Then you were swapping the elements of the original array. This caused the array elements to be rearranged. So, the original array got changed while you were swapping elements. In this case, you should take a copy of the array. And finding index numbers is pretty easy. Just loop over the elements and you are good to go. Hope this helps:
public static void main(String[] args)
{
int n;
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of elements you want to store here: ");
n = sc.nextInt();
int[] a = new int[n]; //Number of array member should be declared here
System.out.println("Enter array elements here: ");
for(int i=0;i<n;i++)
{
a[i]=sc.nextInt();
}
System.out.print("\nOriginal array: ");
for(int i=0;i<n;i++)
{
System.out.print(a[i]+" ");
}
System.out.print("\nReverse array: ");
for(int i=n-1; i>=0; i--)
{
System.out.print(a[i]+" ");
}
System.out.print("\nOriginal array: ");
for(int i=0; i<n; i++)
{
System.out.print(a[i]+" ");
}
//This is where you should make a copy of the original array. You are swapping variable. That's mean array is also getting swapped
System.out.print("\nAscending Order array: ");
int[] tempArray = a;
Arrays.sort(tempArray);
for (int j : tempArray)
{
System.out.print(j + " ");
}
System.out.println();
System.out.print("Enter the number you want to find: ");
int toFind = sc.nextInt();
boolean found = false;
for(int i=0; i<n; i++)
{
if(a[i] == toFind)
{
found = true;
}
}
if(found)
System.out.println(toFind + " is found.");
else
System.out.println(toFind + " is not found.");
}

Related

How to display the value of an array by the index number?

The user enters 10 numbers. After that, the program asks the user to enter the index number they want to retrieve like the example below.
How do I ask the user to input an index number and print the array in that specific index number?
This is my code so far
public class ArrayElement {
public static void main(String[] args) {
int [] Array = new int[10];
int index;
Scanner input = new Scanner(System.in);
System.out.println("Enter 10 elements:");
for (int i = 0; i<10; i++){
Array[i] = input.nextInt();
}
System.out.print("Enter an index you want to retrieve: ");
index = input.nextInt();
}
}
public static void main(String[] args) {
int [] Array = new int[10];
int index;
Scanner input = new Scanner(System.in);
System.out.println("Enter 10 elements:");
for (int i = 0; i<10; i++){
Array[i] = input.nextInt();
}
System.out.print("Enter an index you want to retrieve: ");
index = input.nextInt();
System.out.print("Element at index "+index+" is "+Array[index]);
}
Output : Element at index 6 is 42
you can get the element of a particular index of an array as follows
int element = Array[index];

Q: Sorting of Numeric array in java

I am trying to sort a numeric array in ascending and descending order. I am beginner so using the following link Sort an array in Java . I am trying to get input from user as array's elements.
public class SortingofString {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
int Array1[];
// String Array2[];
System.out.println("How many numaric elements: ");
int n = input.nextInt();
int[] array1 = new int[n];
int number=input.nextInt();
for(int i=0; i<n; i++){
System.out.println("Enter number:");
array1[i] = number;
System.out.println("Original numeric array : "+Arrays.toString(Array1));
Arrays.sort(Array1);
System.out.println("Sorted numeric array : "+Arrays.toString(Array1));
}
}
}
The error occurs when i pass my array_name Array1 in first toString function.System.out.println("Original numeric array : "+Arrays.toString(Array1));
Error says Initialize variable Array1 . How can i resolve this error?
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
//int Array1[];
System.out.println("How many numaric elements: ");
int n = input.nextInt();
int arr[] = new int[n];// solve 1st problem
for (int i = 0; i < n; i++) {
System.out.println("Enter number: " +(i+1));
int number = input.nextInt();
arr[i]=number;//init array by user input data
}
System.out.println("Original numeric array : " + Arrays.toString(arr));
Arrays.sort(arr);
System.out.println("Sorted numeric array : " + Arrays.toString(arr));
}
you also need 2 import
import java.util.Arrays;
import java.util.Scanner;
Move (and rename) Array1 behind reading n:
System.out.println("How many numaric elements: ");
int n = input.nextInt();
int[] array1 = new int[n];
And maybe use the entered number:
int number = input.nextInt();
array1[i] = number;

In Java how to calculate a series of numbers to find the product, that is passed to a method that uses a variable-length argument list

This program is supposed to take a series of numbers, that is put into an array by the user. Not sure on how to do the calculation correctly when using the elements from the array inside of the method that I had made.
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
//****Sentinel****
int s;
//****Do...While Statement****
do{
//****User Input****
System.out.println("Welcome! Please enter a 1 to continue, or 2 to exit.");
s = sc.nextInt();
System.out.println();
switch(s){
case 1:
//****Array****
ArrayList<Integer> nums;
nums = new ArrayList<>();
//****User Input****
System.out.println("Please enter 4 integers: ");
System.out.println("*************************************************");
for(int i = 0; i < 4; i++){
nums.add(sc.nextInt());//****User adds to Array****
}
System.out.println("*************************************************");
//****Call Method****
Product();
break;
}
}while(s != 2);//****end Do...While****
}
//****Product Method****
public static void Product(int... nums){
//****variables****
int result;
int sum = 0;
//****iterate through array****
for(int n : nums){
sum += n;
}
//****Multiply****
int product = (sum * nums.length);
result = product;
//****User Output****
System.out.println("The product of the numbers is: " + result);
System.out.println();
System.out.println("*************************************************");
}
The only thing I get for output is a 0.
You are not passing the array to your method.
//****Call Method****
Product();
You should be passing the array there.

insertion sort logic error in java

I am trying to implement the insertion sort and I only get the first element correct in the sorted newArray. All others become 0's.
I use the occupancy variable to keep track of the number of elements in the newArray.
package nisarg;
import java.util.Scanner;
public class InsertionSort {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
int size,i,j,k=0;
int occupancy = 1;
System.out.println("How many elements? ");
size = input.nextInt();
int[] array = new int[size];
int[] newArray = new int[size];
for(i=0;i<size;i++){
System.out.println("Enter element at " +(i+1) );
array[i] = input.nextInt();
//System.out.println(array[i]);
}
newArray[0] = array[0];
for(i=1;i<size;i++){
for(j=0;j<occupancy;j++){
if(array[i] <= newArray[j]){
for(k=occupancy-1;k>j;k--){
newArray[k] = newArray[k-1];
}
newArray[k] = array[i];
occupancy++;
System.out.println(occupancy +" occupancy");
break;
}
else if(j==occupancy-1){
newArray[occupancy-1] = array[i];
occupancy++;
}
}
}
for(i=0;i<size;i++){
System.out.println(newArray[i]);
}
}
}

Trying to print the frequency of integers in an array

I'm trying to print out the frequency of each integer in an array
import java.util.*;
public class NumFrequency {
public static void main(String [] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter the amount of numbers your going to input, up to 50");
int num = input.nextInt();
int array[] = new int[num];
System.out.println("Enter the "+ num + " numbers now.");
for (int i=0 ; i<array.length; i++) {
array[i] = input.nextInt();
}
System.out.println("array created");
printArray(array);
}
public static void printArray(int arr[]){
int n = arr.length;
for (int i=0; i<n; i++) {
System.out.print(arr[i]+" ");
}
}
private static int[] intFreqArray = new int[51];
public static void FreqOfInt(int[] array, int num) {
for (int eachInt : array) {
intFreqArray[eachInt]++;
}
for (int m = 0; m<intFreqArray.length; m++) {
if (intFreqArray[m] > 1) {
System.out.println(m+ " occurs " + intFreqArray[m] + " times.");
}
}
}
}
It'll print out the array created by the user but nothing after that I'm lost as to why it wont print out the last part.
You need to call FreqOfInt before you print.
Note that we normally use lower case letters for the names of Java methods.
In main, the last method call is to printArray, but you never call FreqOfInt. That's why that output doesn't show up.
Call it after calling printArray.

Categories