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!!
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; } }
Comment