Stuck in an endless loop?iz

I think my code should work, but it sometimes gets stuck. It's supposed to add numbers of type double and sort them from smalles to largest into an array.
This is my code:      //This method inserts a double into its proper sorted position in a sorted
     //list. Throws exception if the list is full.
     public void sortedAdd(double num){
          if (numItems >= LIST_SIZE){
               System.out.println("List is full, cannot add more.");
          else if (numItems==0){
               items[0] = num;
               numItems++;
          else if (items[numItems-1] <= num){
               items[numItems] = num;
               numItems++;
          else{
               int i=numItems-1;
               while(items>num){
                    items[i+1] = items[i];
                    if (i<=1){
                         items[0] = num;
                         numItems++;
                    else{
                         i--;
               if (i>1){
                    items[i+1] = num;
                    numItems++;
But, for example, when I have an array of size 3 that contain 3 identical numbers, like this:23
23
23and I want to add a smaller number, like say:  5.4, my program gets stuck.
It should be like this:5.4
23
23
23                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       

Ok, so I got this thing to work, jverd, but I don't really understand it. Can you help me?
This is the answer, and in bold is all I changed.
public void sortedAdd(double num){
          if (numItems >= LIST_SIZE){
               System.out.println("List is full, cannot add more.");
          else if (numItems==0){
               items[0] = num;
               numItems++;
          else if (items[numItems-1] <= num){
               items[numItems] = num;
               numItems++;
          else{
               int i=numItems-1;
               while(items>num){
                    items[i+1] = items[i];
                    if (*i<1*){
                         items[0] = num;
                         numItems++;
                    else{
                         i--;
               if (*i>=1*){
                    items[i+1] = num;
                    numItems++;
The only thing I did was move the equal sign from above and place it below. I have to admit, I did this out of desperation and trial and error, so can someone explain to me why this fixes the code?
Thanks again jverd and I'm marking the question as answered.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           

Similar Messages

  • When trying to install Epson R200 driver/utility combo, the installer gets stuck in an endless loop asking for admin password without installing. Any idea how to fix this problem?

    When trying to install Epson R200 driver/utility combo, the installer gets stuck in an endless loop asking for admin password without installing. Any idea how to fix this problem?

    Have you checked on the Epson support site? R200 support is located here.

  • Im stuck in an endless loop to restore my i touch. How do i stop and make it work.

    im stuck in an endless loop to restore my i touch. How do i stop and make it work.

    ive been struggling with the same thing myself. i was told to rename the drive that my ipod was on ie. F to Z and then reboot and try the restore process in itunes again but make sure your ipod is plugged in the entire time. im about to try this myself. goodluck!

  • Time Machine wireless setup failed, now stuck in an endless loop...

    Oh dear!
    I've had my TM up and running, backing up my main iMac and MBP without problems. I've had it configured to "join a wireless network".
    This evening I decided to change this so that the TM would "create a wireless network", with the intention of retiring my existing modem/router.
    Something went wrong somewhere along the way and I'm now faced with a Airport Utility message box reading "Airport Utility was unable to find any Apple wireless devices. Make sure ... then click rescan to try again".
    I don't understand what this means. Which "wireless device" is being referred to here? The TM or the iMac itself? Having tried rescan a couple of times I thought I'd give up, close down the Airport Utility / Time Capsule setup dialog boxes and revert back to my original configuration.
    However, this doesn't seem to be possible. When I reopen the TM part of system preferences and choose "Setup Time Capsule" it seems to jump straight to the "Airport Utility was unable to find any Apple wireless devices..." message. There are buttons for revert and update whilst the "discovering" swirlygig is playing, but these are greyed out. Likewise the manual setup button that appears thereafter. In fact, the only button that is clickable is Rescan, which takes me straight back to "Airport Utility was unable to find any Apple wireless devices...".
    So I am stuck in an endless loop with no apparent way out.
    Thoughts / ideas welcomed! Thanks in advance, Ian.

    Could you be a bit more specific, and drop a few pronouns? What I'm understanding is this:
    1. You have a Time Capsule
    2. Time Machine was functioning perfectly
    3. You wanted to create a wireless network (??? You lost me here. Shouldn't Time Capsule create a network already?)
    4. You cannot set it up through System Preferences. (You're using so many terms that I can't tell what you're trying to say in the "However" paragraph.
    Try using Airport Utility in the Utilities folder, found in the Applications folder.
    \Applications\Utilities

  • Stuck in an endless loop during Leopard installation

    I just purchased an IMac which came with a Leopard upgrade. I was successful in installing the upgrade initially but had problems with the IDVD application. I browsed for possible fixes and found one which recommended erasing Themes 1 through 4 under the IDVD directory and reinstalling ILife. Couldn't find a separate ILife or IDVD applicatin within the apps package included with the Leopard upgrade disc provided. I then thought that it would be easier just to reinstall the Leopard. Hence, the problem. I left it running most of the night and woke up this morning just to see that same gray screen with the apple logo and still looping so I turned it off. I have always been a big Mac fan from back in the 80s when I purchased the first generation Mac, but was forced to switch to PC because of work. Even so, I've always thought that Mac was far superior the Windows for ease of use and lesser trouble. This is the first time since then that I've finally decided to revert back to Mac for these reasons, but as it is turning out, I might be wrong. Need any suggestions on how to resolve this problem. I tried restarting, and it takes me back to the same gray screen in an endless loop. Not noing what the next step should be, I just turn it off. HELP!!!!!

    No problem
    Here is how you re-instal just idvd or other applicatons that came with your mac:
    http://docs.info.apple.com/article.html?artnum=301408#1

  • ITunes Match Stuck In An Endless Loop...

    When I turn on iTunes Match, it progresses to the upload stage, but it repeatedly stops within one to two songs and starts all over again.  It will do this as long as I allow it to go without manually stopping it.  In addition, it keeps dumping and removing song titles from selected albums and separates songs within albums which I have purchased singles from.  Can someone please advise me as to how I can make this program work correctly?  It has been a continuous nightmare since I purchased it!

    I found this to be an "unofficial" ISP problem.  I use WOW/Knology and I read of this happening with Comcast (surprise!  surprise!).  Evidently, the iTunes Match transfer was using too much bandwidth and WOW clamped down on this.
    My iTunes Match was in an endless loop.  Following all the instructions that I found about this in the support area did not resolved the issue.  I did find two answers claiming to resolve this issue.  Both mentioned "port throttling"--that is, limiting the upload speed on a specific port (https--443) or a variety of ports.  I tried the easiest and throttled port 433 to 128K max and reran iTunes Match.  It complete parts 1 and 2 easily.  iTunes Match is working on part 3 currently.  Slowly but surely--has has been working steadily for 16-18 hours now (my iTunes library is ~85GB).  (The transfer listed each file as it is being transferred.  It is interesting to see the transfer penalty of never throwing "anything" away.)
    There are several port-throttling programs for the Mac.  " Entonnoir " was the one recommended and the one I am using.  It is a free download --the publisher does ask for a donation.

  • My ipad mini is stuck in an endless loop?

    i was using my ipad mini perfectly fine until wednesday. i came home and tried to charge it because it had died and it started looping endlessly. it goes to the apple logo and starts to boot, but imediately goes to a white loading type screen. i have tried to connect it to itunes but it connecs and unconnects, ocassionally showing a device internal error screen. it wont do anything, reset, nothing. what should i do?

    FORCE IPAD INTO RECOVERY MODE
    1. Turn off iPad
    2. Turn on computer and launch iTunes (make sure you have the latest version of iTune)
    3. Plug USB cable into computer's USB port
    4. Hold Home button down and plug the other end of cable into docking port.
    DO NOT RELEASE BUTTON until you see picture of iTunes and plug
    5. Release Home button.
    ON COMPUTER
    6. iTunes has detected iPad in recovery mode. You must restore this iPad before it can be used with iTunes.
    7. Select "Restore iPad"...
    Note:
    1. Data will be lost if you do not have backup
    2. You must follow step 1 to step 4 VERY CLOSELY.
    3. Repeat the process if necessary.

  • Stuck in an endless loop

    New to java programming. I have been able to install, configure, and run a JDBC application from scratch. It runs but it keep reading the same records over and over and over. I have searched high and low on the web for answers and my syntax look right according to articles. If anyone can help or see where the problem might be, your help will be greatly appreciated. Simple application, connects to a db2 as400 system to retrieve data and displays it on my dos prompt. this is for testing and makins sure it works before moving it to a domino server agent to run on. here is my code
    import java.sql.DriverManager;
    import java.sql.Connection;
    import java.sql.Statement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    class AS400JDBC {
    public static void main(String[] args) {
    Connection conn=null;
    //Begin standard error handling
    try{
    //Register JDBC driver
    String driver="com.ibm.as400.access.AS400JDBCDriver";
    Class.forName(driver);
    //Open a connection
    System.out.println("Connecting to database...");
    String jdbcUrl="jdbc:as400://111.111.111.111:1111:";
    String user ="user";
    String password = "password";
    conn=DriverManager.getConnection(jdbcUrl,user,password);
    //Execute a query
    Statement stmt= conn.createStatement();
    String sql = "SELECT * FROM libe14ers.iascp025x";
    ResultSet rs = stmt.executeQuery(sql);
    //Extract data from result set
    while(rs.next()){
    System.out.println("Connecting to database.111");
    //Retrieve by column name
    System.out.println("lockid = " + rs.getString(1));
    System.out.println("lock = " + rs.getString(2));
    stmt.close();
    conn.close();
    rs.close();
    catch(Exception e){ 
    System.out.println(e);
    }

    None of this will, in all likelihood, address your actual issue, but:
    Close your resources in the reverse order you opened them. That means ResultSet, Statement and finally Connection
    Speaking of finally, make sure you close your resources within a finally block. That is what they are there for. You need the resources freed regardless of whether the request was successful or not
    Move the System.out.println() out of the loop. You are not connecting every time. You are fetching records every time. It is misleading.
    Do not use SELECT *. Always explicitly label what columns you are selecting. Otherwise, another developer has to go to the actual table, describe it, and see what is coming out.
    Please use the code formatting tags (see button above when posting). It is far too difficult to read otherwise.- Saish

  • I tried to install iOS6 and now i am stuck in an endless loop of restoring my phone.

    Like the title says..... i can't do anything but endlessly click restore phone..... HELP!

    Same thing is happening to me...

  • Computer is stuck in an endless loop of "this computer is scheduled for sleep in 10 minutes" it's 2 in the morning and I am afraid

    My macbook pro (2014, mavericks, I would tell you what specific version but my computer is sick) started acting strangely, the brightness button failed to work and things were loading slowly before I got the first prompt that my computer would be going to sleep in ten minutes. Now the window will not go away. If I cancel the prompt, it comes right on back with a fresh count down. I can't type or do anything or access my library because that stupid window is trying to take over my life. I've restarted my computer multiple times, put it to sleep, let the timer run out, reboot it in safe mode, and none of it has worked. I'm panicking. Please I am so tired I just want to go to sleep without all this fear in my heart. Is this because daylight savings? I hear apple can't seem to wrap its head around daylight savings ?? And that a year or two ago there were folks with a similar issue but their OS allowed a lil way to the library I don't have. Can anyone help me. I am tired

    I would first do a SMC and PRAM reset.  See this link:
    http://www.makeuseof.com/tag/reset-macs-smc-pram/

  • I am trying to vget help and support but stuck in an endless loop, being brought back to original support screen. I set yp a support account but you won't allow me to log into it. likewise unable to submit feedback' another endless loop

    i already did!!!!!!!!!!!!!!!!!!!

    Make sure that you do not block cookies.
    * https://support.mozilla.com/kb/Cookies
    * https://support.mozilla.com/kb/Enabling+and+disabling+cookies
    Clear the cache and the cookies from sites that cause problems.
    "Clear the Cache":
    * Tools > Options > Advanced > Network > Offline Storage (Cache): "Clear Now"
    "Remove Cookies" from sites causing problems:
    * Tools > Options > Privacy > Cookies: "Show Cookies"

  • My iphone4 is stuck on an endless loop displaying the apple logo after restore setting and "erase all my personnal data" (forgot the word), what to do?

    I am from Lebanon so there aren't any apple store nearby and can't download 666,6MB with my poor internet especially since on itunes you can't hold the download or at least when you update,
    I just bought the Iphone yesterday from someone and been searching on the net for more than 3 hours
    please help

    I would first do a SMC and PRAM reset.  See this link:
    http://www.makeuseof.com/tag/reset-macs-smc-pram/

  • My wifi is connected but im stuck in an endless loop of resolving hosts.

    All of a sudden the wifi on my MBP has stopped working. I have full signal strength but all browsers are unable to resolve hosts and eventually just time out. I tried clearing out my dns and using OpenDNS but both didnt seem to work. Network diagnostics also failed to resolve the issue. Any assistance provided would be greatly appreciated, thanks very much

    I would first do a SMC and PRAM reset.  See this link:
    http://www.makeuseof.com/tag/reset-macs-smc-pram/

  • Firefox has broken Gmail, and now stuck in an endless loop

    Firefox is telling me Gmail is untrusted. Furthermore I click "I Understand the risks' to add an exception and it tells me the Certificate is valid, so I can't add an exception, then it sends me back to the "site is untrusted" page.
    The only thing that's changed is that my laptop just came back from repair (I kept the HD). Everything else is fine.
    I've tried advice in other articles about clearing cache, removing cookies.sqlite and all that to no avail.
    Another thing, possibly related, my ISP has been doing some major repairs on 'International Connections', and the Mozilla also site came up with a Certificate warning as well. It also came up on my phone. Yet, strangely, Gmail works fine on my phone.

    Check the Date and Time in the Clock on your computer: (double) click the Clock icon on the Windows Taskbar
    * '''Date and Time Properties''' -> go to '''Internet Time''' -> select '''Automatically synchronize with an Internet time server
    * under '''Server''' select '''time.windows.come''' & click '''Update Now''' button
    * click OK
    Check and tell if its working.

  • My HP desktop stuck in an endless loop.

    I just recentley recovered my computer to original factory form, and when i tried strting it up again everything went fine until i came to the login screen and came up with:                                                  
                                                                                                                             Install windows
                                                                             the computer restarted unexpectedly or encountered an enexpected error. Windows 
                                                                             installation cannot proceed. To install Windows, click "OK" to restart the computer,
                                                                              and then restart the installation.
    So i click "OK" and it restarts and repeats the same exact thing. coming up with the same message.
    Im running Windows vista 32 bit on a HP m8000 v5.15

    Try doing another Recovery by starting from the beginning. Are you doing the install from Recovery Disks or F11 at power on?
    ******Clicking the Thumbs-Up button is a way to say -Thanks!.******
    **Click Accept as Solution on a Reply that solves your issue to help others**

Maybe you are looking for