473,385 Members | 2,014 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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!!

Expand|Select|Wrap|Line Numbers
  1. import java.util.Scanner;
  2. public class Array  {  
  3.   public static void main(String [] args)  {
  4.     Scanner myScanner = new Scanner(System.in);
  5.     System.out.print("How many numbers will be input?");
  6.     int howMany = myScanner.nextInt();           
  7.     int [] inList = new int [howMany];          
  8.     int maxValue, minValue, n, valueToFind, location, maxLocate, minLocate, occurence;
  9.     char reply;
  10.  
  11.     for (n = 0; n < inList.length; n++)  {
  12.       System.out.println("Enter " + howMany + " numbers");
  13.       inList[n] = myScanner.nextInt();
  14.     }
  15.     maxValue = findMax(inList);
  16.     minValue = findMin(inList);
  17.     maxLocate = locateMax(inList, maxValue);
  18.     minLocate = locateMin(inList, minValue);
  19.     System.out.println("Largest value is :" + maxValue + " and its located at " + maxLocate + " Smallest value is :" + minValue + " and its located at " + minLocate);
  20.     do  {                                          
  21.       System.out.print("Enter value to locate");
  22.       valueToFind = myScanner.nextInt();
  23.       occurence = search(inList, valueToFind);
  24.       if (occurence == -1)  {
  25.         System.out.println("Not found");
  26.       }
  27.       else  {
  28.         System.out.println("This number is found " + occurence + " times");
  29.       }
  30.       System.out.print("Enter 'Y' to continue");
  31.       reply =  myScanner.next().charAt(0);
  32.     } while (reply == 'Y' || reply == 'y');
  33.   }
  34.   public static int findMax(int [] list) {
  35.     int s, maxSoFar;
  36.     maxSoFar = list[0];
  37.     for (s = 1; s < list.length; s++)  {
  38.       if (maxSoFar < list[s]) {
  39.         maxSoFar = list[s];
  40.       }
  41.     }
  42.     return maxSoFar;
  43.   }
  44.   public static int locateMax(int [] list, int max) {
  45.     int locationMax = 0;
  46.     int n = 0, foundAt = -1;
  47.     while (n < list.length && foundAt == -1)  {
  48.         locationMax = n;
  49.     }
  50.     return locationMax;
  51.   }
  52.     public static int locateMin(int [] list, int min) {
  53.     int locationMin = 0;
  54.     int n = 0, foundAt = -1;
  55.     while (n < list.length && foundAt == -1)  {
  56.         locationMin = n;
  57.     }
  58.     return locationMin;
  59.   }
  60.   public static int findMin(int [] list) {
  61.     int s, minSoFar;
  62.     minSoFar = list[0];
  63.     for (s = 1; s < list.length; s++)  {
  64.       if (minSoFar > list[s]) {
  65.         minSoFar = list[s];
  66.       }
  67.     }
  68.     return minSoFar;
  69.   }
  70.     public static int search(int[] list, int valueToFind) {
  71.       int occurence=0;
  72.       for (int i = 0; i < list.length; i++) { 
  73.         if (list[i] == valueToFind)
  74.           occurence++;
  75.   }
  76.         return occurence;
  77. }
  78. }
  79.  
Mar 12 '09 #1
1 1693
r035198x
13,262 8TB
1.) You can System.out.println statements to find out which part of the code are getting executed and which part are not.
2.) You have in your locateMax method
Expand|Select|Wrap|Line Numbers
  1. int n = 0, foundAt = -1;
  2. while (n < list.length && foundAt == -1) {
  3.     locationMax = n;
  4. }
  5.  
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.
Mar 12 '09 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: anuradha.k.r | last post by:
hi, i am writing a socket program in python,both client side and server side.I've written the client side which is working perfectly fine(checked it against server program written in C).but as for...
10
by: feel52 | last post by:
Below you'll find the code i'm working on. It's in a button click routine and hangs after 3 or 4 sometimes 5 loops done, probably in sock.receive(....). Some code was found here( on google i mean)...
2
by: Asad Khan | last post by:
I call the following method from my main form method: uploadThread = new Thread(new ThreadStart (this.doFTPUpload)); uploadThread.Name = "FTP Upload"; uploadThread.Start(); bool...
6
by: Alexander Widera | last post by:
hello, if i start a program (an exe-file) with Process.Start(...) I don't have the required permissions that the programm needs (i could start the programm but the program needs special rights)....
1
by: ninjawolfen | last post by:
Hi, I'm doing an application in VB 6 SP6 in XP Prof. SP2, this app used to work fine, until i used a ms-winsock control to connect to a fingerprint terminal reader, in the IDE everithing that the app...
1
by: Hans Kesting | last post by:
Hi, I can't get a vs2008 solution to compile. I have copied the source from a (compiling) 2.0 web-application to a new directory (I didn't want to change the old code), and tried to compile...
2
by: test3 | last post by:
Hello folks, I'm using System.Diagnostics.Process to start a thirdparty program (that works perfectly when started via command line). I'm using Process.StandardOutput to get the output of the...
4
by: Mahernoz | last post by:
Hi Friends, I have this code in a C# console application which calls a URL on my website(Asp.net/C#) with Querystrings. (I have also tried without querystrings). The problem is my program...
3
by: Microsoft | last post by:
Hi I have a c# program that continually runs 24/7 and performs a variety of tasks based on a timer. There is one routine that hangs every Saturday morning without fail. If I restart the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.