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!! -
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;
-
}
-
}
-
1 1693
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 -
int n = 0, foundAt = -1;
-
while (n < list.length && foundAt == -1) {
-
locationMax = n;
-
}
-
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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)...
|
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...
|
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)....
|
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...
|
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...
|
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...
|
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...
|
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...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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
|
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...
|
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,...
|
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...
|
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,...
|
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...
| |