# Thread: Finding the Min and Max of numbers

1. Member
Join Date
Sep 2012
Posts
2
Rep Power
0

## Finding the Min and Max of numbers

Hi. I'm pretty new to java and I'm stuck on an exercise. Here's the problem:
The program is supposed to read 5 integers from the command line argument between 1 and 10. If any of the 5 numbers are out of that range, it exits the program. If they are within range, it prints out the numbers and and then it finds the Max and Min of that set. So if the numbers are "1 2 3 4 5", it would print out those numbers and have 5 as Max and 1 as Min.
I'm stuck on this. I don't know where to begin. Any help is greatly appreciated. Here's what I have so far.

public class MinMax2{
public static void main(String [] args)
{
double n1 = Double.parseDouble(args[0]);
double n2 = Double.parseDouble(args[1]);
double n3 = Double.parseDouble(args[2]);
double n4 = Double.parseDouble(args[3]);
double n5 = Double.parseDouble(args[4]);
double NumSet = n[i];

System.out.println("Enter five digits = " + n1 + " " + n2 + " " + n3 + " " + n4 + " " + n5 + " ");

if(NumSet >= 1 || NumSet <= 10)
{
System.out.println("Five digits entered = " + NumSet);
}

else if(NumSet <= 1 || NumSet >= 10)
{
System.out.println("Exit Program");
}
}
}
Last edited by Alexis_DB; 09-29-2012 at 06:49 PM.

2. ## Re: Finding the Min and Max of numbers

What does the program print out when it executes? Please copy the console for when you execute the code and paste it here showing the command line and the program's output.

Please edit your post and wrap the code in code tags. See: BB Code List - Java Programming Forum

3. Member
Join Date
Sep 2012
Posts
2
Rep Power
0

## Re: Finding the Min and Max of numbers

Hello. This is what I'm getting:

Microsoft Windows XP [Version 6.1.7601]

C:\Users\Joseph\Desktop\cmdline-setup (2)\cmdline-setup>javac MinMax2.java
MinMax2.java:10: cannot find symbol
symbol : variable n
location: class MinMax2
double NumSet = n[i];
^
MinMax2.java:10: cannot find symbol
symbol : variable i
location: class MinMax2
double NumSet = n[i];
^
2 errors

C:\Users\Joseph\Desktop\cmdline-setup (2)\cmdline-setup>
I'm stuck on this part and do not know how to go any further.

4. ## Re: Finding the Min and Max of numbers

You're using [quote] [/quote] tags not [code] [/code] tags and so your posted code is still unformatted and difficult to read. Please consider editing your posts and changing this.

Regarding your problem, the compiler is telling you exactly what's wrong: you're trying to use variables that don't exist, that you've never declared or initialized. The first error complains about the n variable. This appears to be an array, but no where in your code to you declare or initialize such a variable. Same for the next error regarding the variable i.

5. Member
Join Date
Sep 2012
Posts
44
Rep Power
0

## Re: Finding the Min and Max of numbers

When finding min and maxium numbers, it's best that you assume (as starting point) that one of the numbers is max/min.

Example, let's say you want to find the maxium number. First you need a variable for maxium, and you say that the variable maxium will be the first number.

Then you do four if tests for the four numbers remaining. And if one of the numbers is bigger than the number that is set to be maxium - then in the test you write a setence saying that the new maxium value is that number.

In the end the maxium number will prevail.

And you need to do a chain of four if tests each for Maxium , and four for minium. Same thing.

6. Member
Join Date
Sep 2012
Posts
8
Rep Power
0

## Re: Finding the Min and Max of numbers

Java Code:
```import java.util.Scanner;

public class NumberSorter
{

private double[] beans;
private double max;
private double min;

public NumberSorter(int howManyEntered)
{
beans = new double[howManyEntered];
max = 0;
min = 0;
}
public void sort()
{
max = beans[0];
min = beans[0];
for(int i = 0; i < beans.length; i++)
{
if(beans[i] < min)
{
min = beans[i];
}
if(beans[i] > max)
{
max = beans[i];
}
}
}

public void getNumbers()
{
Scanner in = new Scanner(System.in);
for(int i = 0; i < beans.length; i++)
{
System.out.println("Enter a number: ");
beans[i] = in.nextDouble();
}
}

public double getMax()
{
return max;
}

public double getMin()
{
return min;
}
}```
You're Welcome lol
Here's the main method:
Java Code:
```import java.util.Scanner;

public class SorterTester
{
public static void main(String[] args)
{
System.out.println("Hey man how many numbers you want to find max/min for?");
Scanner dude = new Scanner(System.in);
int howMany = dude.nextInt();
NumberSorter mySorter = new NumberSorter(howMany);

mySorter.getNumbers();
mySorter.sort();
System.out.println(" The max is " + mySorter.getMax() + " and the min is " + mySorter.getMin());

}
}```
I reccommend using the scanner class provided by java it's much easier at reading input assuming it is a beginner class like mine >_>

Anyways I had to go to my professor to see how to do this because it was bugging me >_< lol

I tested it and it should work just fine

Pretty neat concept... you test for each index of the array starting at the 0 index and going up to the length - 1 or just less than the length of the array.

I don't know if you have done arrays in your class yet or not... Either way! Arrays are awesome you can say how many numbers you got that u want... make a new array with that many spaces.. Enter the numbers... sort em... return the max return the min and TA DA Insta awesomness :D

I reccomend studying this

7. Member
Join Date
Sep 2012
Location
Australia
Posts
12
Rep Power
0

## Re: Finding the Min and Max of numbers

Java Code:
```
public class MinMax
{
public static final int SIZE = 5;

public static void main(String[] args)
{
if(args.length < SIZE)
{
printErrorMessage("Not sufficient Arguments.......");
}
else if(args.length > SIZE)
{
printErrorMessage("Too many arguments.........");
}

int numberArray[] = new int[5];

for(int i = 0; i < SIZE; i++)
{
try
{
numberArray[i] = Integer.parseInt(args[i]);

if(numberArray[i] < 1 || numberArray[i] > 10)
{
printErrorMessage("Number you entered was out of range.....");
}
}
catch(Exception e)
{
printErrorMessage("Argument that you entered was not digit........");
}
}

printAllNumbers(numberArray);
findMinNumber(numberArray);
findMaxNumber(numberArray);

}

public static void printAllNumbers(int[] numberArray)
{
System.out.print("You entered following numbers: ");

for(int i = 0; i < SIZE; i++)
{
System.out.print(numberArray[i] + " ");
}

System.out.println();
}

public static void findMinNumber(int[] numArray)
{
int minNumber = numArray[0];

for(int i = 1; i < SIZE; i++)
{
if(numArray[i] < minNumber)
{
minNumber = numArray[i];
}
}

System.out.println("Min Number is : " + minNumber);
}

public static void findMaxNumber(int[] numArray)
{
int maxNumber = numArray[0];

for(int i = 1; i < SIZE; i++)
{
if(numArray[i] > maxNumber)
{
maxNumber = numArray[i];
}
}

System.out.println("Max Number is : " + maxNumber);
}

public static void printErrorMessage(String message)
{
System.err.println(message);
System.err.println("Terminating Program.....");
System.exit(0);
}

}```
Last edited by Amok; 09-30-2012 at 01:02 PM.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•