How to ask for an array and how to save the values

I'm supposed to be learning the differences between a linear search and a binary search, and the assignment is to have a user input an array and search through the array for a given number using both searches. My problem is that I know how to ask them how long they want their array to be, but I don't know how to call the getArray() method to actually ask for the contents of the array.
My code is as follows:
import java.util.Scanner;
import java.util.ArrayList;
public class Main
    private static Scanner input = new Scanner(System.in);
    public static void main (String args[])
        //creates ArrayList
        int List[];
        System.out.println("How long would you like the array to be?");
        int arrayLength = input.nextInt();
        //Initializes array list
        List = new int [arrayLength];
        System.out.println("Please enter the first value of the array");
    public static void getArray(int List[], int arrayLength)
        for(int i=0; i < arrayLength; i++) {
             System.out.println("Enter the next value for array");
             List[i] = input.nextInt();
     public static void printArray(int List[])
         for(int i=0; i < List.length; i++)
             System.out.print(List[i] + " ");
public class search
    public static int binarySearch(int anArray[], int first, int last, int value)
        int index;
        if(first > last) {
            index = -1;
        else {
            int mid = (first + last)/2;
            if(value == anArray[mid]) {
                index = mid; //value found at anArray[mid]
            else if(value < anArray[mid]) {
                //point X
                index = binarySearch(anArray, first, mid-1, value);
            else {
                //point Y
                index = binarySearch(anArray, mid+1, last, value);
            } //end if
        } //end if
        return index;
    //Iterative linear search
    public int linearSearch(int a[], int valueToFind)
        //valueToFind is the number that will be found
        //The function returns the position of the value if found
        //The function returns -1 if valueToFind was not found
        for (int i=0; i<a.length; i++) {
            if (valueToFind == a) {
return i;
return -1;

I made the changes. Two more questions.
1.) Just for curiosity, how would I have referenced those methods (called them)?
2.) How do I call the searches?
import java.util.Scanner;
import java.util.ArrayList;
public class Main
    private static Scanner input = new Scanner(System.in);
    public static void main (String args[])
        //creates ArrayList
        int List[];
        System.out.println("How many values would you like the array to have?");
        int arrayLength = input.nextInt();
        //Initializes array list
        List = new int [arrayLength];
        //Collects the array information
        for(int i=0; i < arrayLength; i++) {
             System.out.println("Enter a value for array");
             List[i] = input.nextInt(); 
        //Prints the array
        System.out.print("Array: ");
        for(int i=0; i < List.length; i++)
             System.out.print(List[i] + " ");
        //Asks for the value to be searched for
        System.out.println("What value would you like to search for?");
        int temp = input.nextInt();
        System.out.println(search.binarySearch()); //not working
}

Similar Messages

  • I bought a used macbook pro from a friend. Software updates are available, but when I try to update, the computer asks for apple id and will only accept the former owner´s. How can I connect my macbook to my apple id?

    I bought a used macbook pro from a friend. Software updates are available, but when I try to update, the computer asks for apple id and will only accept the former owner´s. How can I connect my macbook to my apple id?

    The first thing you should do after acquiring a used computer is to erase the internal drive and install a clean copy of OS X. How you do that depends on the model. Look it up on this page to see what version was originally installed.
    If the machine shipped with OS X 10.4 or 10.5, you need a boxed and shrink-wrapped retail Snow Leopard installation disc, which you can get from the Apple Store or a reputable reseller — not from eBay or anything of the kind.
    If the machine shipped with OS X 10.6, you need the gray installation discs that came with it. If you don't have the discs, order replacements from Apple. A retail disc, or the gray discs from another model, will not work.
    To boot from an optical disc, insert it, then reboot and hold down the C key at the startup chime. Release the key when you see the gray Apple logo on the screen.
    If the machine shipped with OS X 10.7 or later, it should boot into Internet Recovery mode when you hold down the key combination option-command-R at the startup chime. Release the keys when you see a spinning globe.
    Once booted from the disc or in Internet Recovery, launch Disk Utility and select the icon of the internal drive — not any of the volume icons nested beneath it. In the Partition tab, select the default options: a GUID partition table with one data volume in Mac OS Extended (Journaled) format. This operation will permanently remove all existing data on the drive, which is what you should do.
    After partitioning, quit Disk Utility and run the OS X Installer. When the installation is done, the system will automatically reboot into the Setup Assistant, which will prompt you to transfer the data from another Mac, its backups, or from a Windows computer. If you have any data to transfer, this is usually the best time to do it.
    You should then run Software Update and install all available system updates from Apple. If you want to upgrade to a major version of OS X newer than 10.6, buy it from the Mac App Store. Note that you can't keep an upgraded version that was installed by the previous owner. He or she can't legally transfer it to you, and without the Apple ID you won't be able to update it in Software Update or reinstall, if that becomes necessary. The same goes for any App Store products that the previous owner installed — you have to repurchase them.

  • My iPhone3GS running iOS5 has recently been reluctant to upgrade Apps. The App store asks for my password and then marks all the Apps as being updated. Often only one or none actually update. Usually I get a message saying "Unable to download application"

    My iPhone3GS running iOS5 has recently been reluctant to upgrade Apps. The App store asks for my password and then marks all the Apps as being updated. Often only one or none actually update. Usually I get a message saying "Unable to download application" and "<app> could not be downloaded at this time - Done or Retry".
    It's not an iTunes login / password issue because sometimes one or two of the Apps will download and the others stall with the blank progress bar. The internet connection is working fine in all other respects.
    Sometimes, many restarts later the downloads will complete. Is this a known bug? Is Apple working on it? Any fixes?

    My iPhone3GS running iOS5 has recently been reluctant to upgrade Apps. The App store asks for my password and then marks all the Apps as being updated. Often only one or none actually update. Usually I get a message saying "Unable to download application" and "<app> could not be downloaded at this time - Done or Retry".
    It's not an iTunes login / password issue because sometimes one or two of the Apps will download and the others stall with the blank progress bar. The internet connection is working fine in all other respects.
    Sometimes, many restarts later the downloads will complete. Is this a known bug? Is Apple working on it? Any fixes?

  • I am unable to connect to my WIFI.It keeps asking for a password and when i enter the password it says that is unable to connect or i have entered an incorrect password

    I am not able to connect to my Wifi. It keeps asking for a password and when i enter the password it either says Unable To Connect or Incorrect Password.
    This occurred only after installing a new Wifi router from Qtel (service provider). any assisyance is greatly appreciated.

    You should call Qtel technical support and ask for help.
    Where did you get the password that you are trying? What does it look like? (Length, type of characters)
    Do you know what type of security is being used? WEP, WPA, WPA2

  • Trying to restore my ipad 2 during the restore process it is asking for a passcode and i dont know the passcode

    My IPad 2 is locked with a passcode when I try to restore the ipad  halfway through the restore process it is asking for a passcode I do not know the passcode and the IPad was never synced to a computer. Please help

    Instructions can be found here.
    http://support.apple.com/kb/ht1212
    Make sure that you pay attention to the section at the very of the article that talks about having never synced your iPad with iTunes. You may need have to restore the iPad that way.

  • Firefox asks for user name and password even though the proxy auto detection

    After upgrading Firefox to ver. 6.01 and next to 6.02 Firefox asks for user name and password of proxy server even though proxy settings are set to Automatic detect proxy settings.

    Sorry can't do anything more than offer support. We have been having this problem for months and support is clueless. Do you also have the migrating homepage issue? We can have 3 or 4 different homepage set-ups throughout the day. Between Verizon and FIOS, there isn't an employee in the corporation who cares about customer service. Since the failed email migrations dating back to Spring of 2011, we have had this login situation constantly. Our normal login count for a day is 12. We figure they are regulating traffic by having customers constantly login.

  • Photoshop cs3 extended asking for serial number and activation after having the program for over 5 years now

    for some reason the program out of the blue is asking for a serial number and activation.  I can't find the case anymore.  does anyone have a way I can contact adobe directly to see if they can help me solve this problem. 
    Everywhere I look points me back to the forum or troubleshooting menus.
    thanks

    It may think you have a different computer if you've changed some of the hardware recently.
    Did you purchase the software as a download or register it with Adobe?  If so, your serial number may be available from them directly.  Click first the red A logo at the top of this page, then drill down through [Manage Account] to see the products they have on file for you.
    If you've physically lost your serial number and never registered it with Adobe, it's like losing a wad of cash on the street.  You're responsible for holding onto your valuables and no one's going to step up and replace it for you.
    The Creative Cloud Photographer's Bundle subscription package is still available for $9.99 a month, as far as I know.
    -Noel

  • Waveburner and Soundtrack Pro :ask for a serial and XS key all the time

    Dears all,
    i have installed the Logic package on a new Mac. All seems fine but all the time i start Waveburner or Soundtrack pro i get a serial number and xs key dialog box. Can someone help me ?
    Thanks in advance.
    The system is a iMac 27 3.4 os 10.8.3.
    Best
    -Anton

    Problem solved in this way :
    Launch Waveburner -> type or copy the Logic 9 serial -> at the second dialog box ( insert xs key or type the serial ) type or paste the Logic 8 serial number.
    It work and also Sountrack is running flawless without serial ask.
    Cheers.
    -A

  • I just got a new ipad air, and it's tell me to connect to a network...I've chosen my network at home...it's asking for a password, and I've put the password in, but there isn't a button that allows me to hit enter and accept the password.

    I just got a new ipad air...it's telling me to connect to a network...I've chosen my home network, which requires a password...I put the password in, but there is no button to press to accept the password.  Can someone help me get past this roadblock?

    You need 8 characters or more to connect.

  • HT2731 How do I enrol in iTunes u? It asks for a code and I don't no what that means and my iPod does this wierd thing!....

    Hey I need help in enrollment on iTunes u I already did my appleID but then it asks for a code and idk what that is. Btw I need iTunes u to catch up in school cuz some things I'm bad at and I tried diff. Things in school and it didn't work so I'm trying this out, school has just begun... It would b totally awesome if some1 could get back to me on this plz email me at [email protected]..........and I also have troubles with my iPod touch 4g, when I'm searching things on the google app my iPods screen turns black for 5 seconds then goes to the home screen and it does this every time I'm in the google app and sometimes it does this when I'm playing a game or looking on iTunes plz help me!!!! P.s I won't b on here very much so if you have an answer for me email me :) at [email protected]      :) :)

    I am using MAC OSx 10.5.8 (9L31a) as my operation system.  The TV says "No signal.  Check connected device's power, cable connection and source selection."  Seems to me that that goes in hand with what the Samsung tech said about the Mac needing to be forces to use the external display.  But how do I do that?

  • Help! Bought a used iMac (2008) and I'm trying to install quicktime but its asking me for a password and has Apple in the "Name" spot.  How do I retrieve or reset the password?

    Can anyone help...
    I just bought a used iMac (2008) from an ebay seller and I'm trying to install quicktime but its asking me for a password and has Apple in the "Name" spot. 
    How do I retrieve or reset the password?
    It came with the upgrade of Mountain Lion but I cannot find quicktime in the applications.  When I try to install anything this Installer Password thing comes up... so I'm stuck.

    ShirlFromNC
    I think you're slightly misunderstanding something, so maybe I'll try to chime in, even though everyone has said it in various ways.
    Before OSX 10.7 (aka Lion) came out, the upgrades to OSX belonged to the Mac and not the user. Probably there was a technical legality in the user agreement that you couldn't do that, but everyone did. I could sell a Mac with Snow Leopard (or anything before that) by just erasing the hard drive, and reinstalling OSX. Then ship it to the buyer. Now, I was pretty honest as a seller, and including not only the original installation discs but also any upgrade. I would only sell a Mac if I was replacing it, so I always was getting the brand new version.
    With OSX 10.7, upgrading was done online, with no disks. So the upgrades were tied to an Apple ID rather than the machine. Recently, I sold a MacBook Pro (to upgrade to a 27" iMac), which came with Lion. I had promised the buyer that it would come with Mountain Lion, but I didn't know that the upgrade was tied to my Apple ID. I made good with the buyer by sending along a $20 Apple App Store card so that she could upgrade to Lion.
    So what everyone is saying here is that you need the original Installation discs or Snow Leopard disk to reinstall and wipe out the Mountain Lion that is on your Mac. It doesn't belong to you, it belongs to the seller, and without his Apple ID and password, you cannot use it. Once you do a reinstall of Snow Leopard, then you can upgrade to Lion and then to Mountain Lion. I do not believe you can upgrade directly to Mountain Lion from Snow Leopard, but I'm not sure.
    If the seller claimed that you were getting Mountain Lion, then he was lying to you (or being more civil, maybe he just didn't know like I did). If you bought the used machine from Amazon Sellers or from eBay, then you have recourse through the feedback system or through PayPal. If you bought it any other way, you're on your own.

  • The audo files do not play from websites instead ask for download. This has occured from the time I have updated Firefox 4 to Firefox 5. Why? And how can i play audio files?

    The audo files do not play from websites instead ask for download. This has occured from the time I have updated Firefox 4 to Firefox 5. Why? And how can i play audio files from websites?

    You have a <b>general.useragent.override</b> pref that identifies you as Firefox/3.5.7
    *Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.7) Gecko/20091221 Firefox/3.5.7 (.NET CLR 3.5.30729)
    See:
    *https://support.mozilla.com/kb/Websites+or+add-ons+incorrectly+report+incompatible+browser
    *http://kb.mozillazine.org/Resetting_your_useragent_string_to_its_compiled-in_default

  • I'm trying to open the photoshop and it's asking for a serial number  how can i gent a serial number for photoshop, i buy it here

    i'm trying to open the photoshop and it's asking for a serial number  how can i gent a serial number for photoshop, i buy the photoshop here

    What version of Photoshop are you using?
    Did you purchase it outright, or do you have a subscription?
    Gene

  • I brought an IPhone 5 from a second hand source and I tried to activate it but it asked for his Apple ID how can I get around that

    I brought an IPhone 5 from a second hand source and I tried to activate it but it asked for his Apple ID how can I get around that?

    MarkeyMarcc wrote:
    I brought an IPhone 5 from a second hand source and I tried to activate it but it asked for his Apple ID how can I get around that?
    Read this: Find My iPhone Activation Lock: Removing a device from a previous ...

  • I own Photoshops Elements 10, Installed on my new Mac, mavericks, and it now asks for a reinstall. How do I reinstall?

    I own Photoshops Elements 10, Installed on my new Mac, mavericks, and it now asks for a reinstall. How do I reinstall?

    First of all, what is asking for a reinstall? Did you move PSE from another computer?

Maybe you are looking for

  • HP LaserJet Pro 200 color M251nw can't be detected on wired network

    New LaserJet Pro 200 color M251nw connects to main computer and functions.  Networked computer does not recognize the this new computer.  Operating system is Windows XP. IBM Printer 12 was the printer that was replaced.  I used the same USB port that

  • Itunes problems with windows 7 64 bit

    Hi, i have searched and searched for a solution to my on going problems with itunes on my windows 7 64bit computer. it started with my backups not being saved when i sync, then any music i add seems to play strangely i.e skips and plays the start of

  • Unicode character not displaying correctly

    when i am stroring Unicode Character in Oracle 8 DB .The Stored Character are not displayed correctly . Through sql client seen as '�' .Altough through JSp it displays correct value 集 (in japanese) My client wants to see through sql also same value .

  • Toshiba 44 NHM84 DLP TV - Can't turn on it

    I have a Toshiba 44 nhm84 DLP tv. It is about 4 years old. Within weeks of buying the TV It would not turn on. After turning the power on, the green light on the TV would blink for about 40 seconds and then the red light would go on indicating the TV

  • Clear cache of 2nd level tabs

    Hello, I have a question related to clearing the cache in a two level tab application structure. If you look at the examples application of Denes Kubicek, you see that he uses a two level tab structure. The horizontal bar below the header is level 1