473,883 Members | 1,670 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Program Hangs and Will Not Finish

1 New Member
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 1722
r035198x
13,262 MVP
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
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
8535
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 my server program written in python it simply hangs.it does not show any error also.I've tried sample programs available .I don understand what the reason is as i am quite new to it. here is teh server side program: ///////////////////////
10
5318
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) but the amazing( at least for me) is that if i run the program step by step or i cancel the loop and run it by pressing the button again and again it never hangs . I want to have an infinite loop so my socket keeps on listening until i...
2
10544
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 threadTerminatedGood = uploadThread.Join(client.transmissionTimeout*60*1000); if (!threadTerminatedGood) { throw new Exception("The upload thread was interrupted by main thread
6
3394
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). So I added the StartInfo.UserName and Password to the Process. Now the program starts, but it hangs... nothing happens. In the task-manager the process is created, but it doesn't do anything - no cpu-load and no change of memory-usage. What...
1
1538
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 has to do it do it fine, with no errors, but when I exit the app it hangs, and hangs the entire IDE. I have compiled the app and it does the same, the app works fine, but when I use the part of the app that uses the winsock connection and after...
1
1752
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 it in vs2008 (beta 2). When compilation reaches about 75% of the 46 projects in this solution, it seems to hang with near 100% CPU usage. A colleague (who had the same problem) reported that compilation did finish after about 90 minutes of CPU time.
2
6889
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 program. That works for 95 %, but the other 5 % it doesn't. It seems to me that the started process just hangs, and therefor my program hangs, too (p.WaitForExit()). I researched that this only happens when the output of the program is longer than...
4
3372
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 gets hanged. Even no error message is displayed. HttpWebRequest req =
3
1690
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 program throughout the week is still hangs. I can restart it immediatly after the hang and it runs fine until next Saturday. There is nothing different about Saturday that I can think of.
0
9792
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11145
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10418
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7974
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7133
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5801
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4617
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4223
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3235
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.