Program Hangs and Will Not Finish

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sadpony
    New Member
    • Mar 2009
    • 1

    Program Hangs and Will Not Finish

    Ok, so I wrote a program and it is supposed to let the user set the size of an array, input the numbers, find the subscript and value of the highest and lowest values, and then ask the user to input a value which the program will then check for it in the array and tell the user if it is in the array and how many times.

    So, I wrote it all, and I thought that it should be working. It compiles fine, but once I run it, it just hangs up and doesn't do anything. Any help would be so appreciated!!

    Code:
    import java.util.Scanner;
    public class Array  {  
      public static void main(String [] args)  {
        Scanner myScanner = new Scanner(System.in);
        System.out.print("How many numbers will be input?");
        int howMany = myScanner.nextInt();           
        int [] inList = new int [howMany];          
        int maxValue, minValue, n, valueToFind, location, maxLocate, minLocate, occurence;
        char reply;
        
        for (n = 0; n < inList.length; n++)  {
          System.out.println("Enter " + howMany + " numbers");
          inList[n] = myScanner.nextInt();
        }
        maxValue = findMax(inList);
        minValue = findMin(inList);
        maxLocate = locateMax(inList, maxValue);
        minLocate = locateMin(inList, minValue);
        System.out.println("Largest value is :" + maxValue + " and its located at " + maxLocate + " Smallest value is :" + minValue + " and its located at " + minLocate);
        do  {                                          
          System.out.print("Enter value to locate");
          valueToFind = myScanner.nextInt();
          occurence = search(inList, valueToFind);
          if (occurence == -1)  {
            System.out.println("Not found");
          }
          else  {
            System.out.println("This number is found " + occurence + " times");
          }
          System.out.print("Enter 'Y' to continue");
          reply =  myScanner.next().charAt(0);
        } while (reply == 'Y' || reply == 'y');
      }
      public static int findMax(int [] list) {
        int s, maxSoFar;
        maxSoFar = list[0];
        for (s = 1; s < list.length; s++)  {
          if (maxSoFar < list[s]) {
            maxSoFar = list[s];
          }
        }
        return maxSoFar;
      }
      public static int locateMax(int [] list, int max) {
        int locationMax = 0;
        int n = 0, foundAt = -1;
        while (n < list.length && foundAt == -1)  {
            locationMax = n;
        }
        return locationMax;
      }
        public static int locateMin(int [] list, int min) {
        int locationMin = 0;
        int n = 0, foundAt = -1;
        while (n < list.length && foundAt == -1)  {
            locationMin = n;
        }
        return locationMin;
      }
      public static int findMin(int [] list) {
        int s, minSoFar;
        minSoFar = list[0];
        for (s = 1; s < list.length; s++)  {
          if (minSoFar > list[s]) {
            minSoFar = list[s];
          }
        }
        return minSoFar;
      }
        public static int search(int[] list, int valueToFind) {
          int occurence=0;
          for (int i = 0; i < list.length; i++) { 
            if (list[i] == valueToFind)
              occurence++;
      }
            return occurence;
    }
    }
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    1.) You can System.out.prin tln statements to find out which part of the code are getting executed and which part are not.
    2.) You have in your locateMax method
    [code=java]
    int n = 0, foundAt = -1;
    while (n < list.length && foundAt == -1) {
    locationMax = n;
    }
    [/code]
    The loop is controlled by n and foundAt, yet none of them are changed inside the body of the loop. That means you are running into an infinite loop. The same is true for your locateMin method.

    Comment

    Working...