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(System.in);
    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

    Arrays.sort(sortedNumber);

    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

Replies

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
   break;
 }
}
  • 1

Java / Android Development

106,979 followers
About

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

Links
Moderators
Bucky Roberts Administrator