Help with arrays

0 Marco Guerrero · October 26, 2015
Hello everyone, I am currently working in this code for one of my assignments. I just started learning java as you will realize. What I'm trying to do is two integer arrays, print them and then prompt for a number to be search using a for each loop. If the number is located then the program will return the index. I have tried several times to create the last loop with no luck.  Any advice will be very appreciate it.
import java.util.Scanner;
import java.util.Arrays;

public class SearchForArray
 public static void main (String[] args)
 //generate random numbers
    Scanner stdIn = new Scanner(;
    int[] number = new int[10];
    for (int i=0; i < number.length; i++){
        number[i] = (int)(Math.random()*20);
 // copy numbers
    int[] sortedNumber = new int[number.length];
    for (int i = 0; i < number.length; i++){
        sortedNumber[i] = number[i];
 //sort numbers second collumn


    System.out.printf("%s\t\t%s", "Unsorted Array","Sorted Array");
    for (int i=0; i < number.length; i++) {
        System.out.printf("\n%d\t\t\t%d", number[i], sortedNumber[i]);

 //search for number (This is where I am stuck at)


 }//end of main

}//end of class


Post a Reply


Oldest  Newest  Rating
0 Gary Whitney · October 26, 2015
If I understand you correctly you are stuck finding if a n int is in your sorted Array.

Does this pseudoCode give you ideas?

int numToFind = 4;
for (i 0; i<sortedNumber.length; i++{
 if (sortedNumber == numToFind){
   //do something
  • 1

Java / Android Development


Very popular language used to create desktop applications, website applets, and Android apps.

Bucky Roberts Administrator