# Find smallest number of list

Page 1 of 1

## 9 Replies - 68591 Views - Last Post: 05 May 2011 - 06:09 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=34161&amp;s=ba617adc88765492dcb31be69ec99904&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 bulletb1331

• New D.I.C Head

Reputation: 0
• Posts: 4
• Joined: 13-September 07

# Find smallest number of list

Posted 27 September 2007 - 07:47 PM

Hey everyone,

wondering if anyone can help me with this. The goal of this program is to allow a user to input a list of numbers ending it with a negative number as the trigger to execute the calculations. The calculations include finding the average of the list, the largest number and the smallest number, but none of the calculations can include the negative number. I've figured out how to get the largest number and the average but my code for smallest number isn't working. Can anyone tell me what will work or at least point me in the right direction? here's what i've got so far.

``` /*
* Robert Durning
* HW4a
*/

//Determine Small, Large, Average values for a list of integers

import java.util.*;

public class SmLrgAvg
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);

System.out.println("Enter in a list of non-negative integers. End the list with a negative integer.");

int n = 0, count = 0;
double largestInteger = 0, smallestInteger = n, sum = 0;
while (n >= 0)
{
n = keyboard.nextInt();
smallestInteger = n;

if (n >= 0)
{
sum = sum + n;
count = count + 1;
}
if (n >= largestInteger)
largestInteger = n;
if (n < smallestInteger && n >= 0)
smallestInteger = n;
}
System.out.println("The largest integer is: " + largestInteger);
System.out.println("The smallest integer is: " + smallestInteger);
System.out.println("The average of your list is: " + sum/count);
}

}
```

thanks

Is This A Good Question/Topic? 0

## Replies To: Find smallest number of list

### #2 capty99

• i am colt mccoy

Reputation: 98
• Posts: 10,081
• Joined: 26-April 01

## Re: Find smallest number of list

Posted 27 September 2007 - 08:18 PM

the problem is after you called the n=keyboard.nextInt() you define the smallest integer at n.

so therefore,
the last one, regardless of if you break the loop with a negative.
is going to be that negative number,
it doesn't even have to get to the end looping part.

this may or may not help you,
did it a couple of weeks ago for a project... i think it is the same thing

```import java.util.*;
import java.math.*;
/*
File: AverageTemp.java

Description: Takes temperature values as input from the user and displays the low, high and average temperature.

Name: Tyler Lee

UT EID: Tjl262

Course Name: CS 313E

Unique Number: 56440

Date Created: 9/10/2007

Date Last Modified: 9/10/2007
*/

public class AverageTemp
{
public static void main(String[] args){
// Initialize all variables used
int tempEntries = 0; //Keeps track of number of entries
double tempTotal = 0; //For averaging purposes
double tempHigh = 0; //Keeps track of the highest temperature
double tempLow = 0; //Keeps track of the lowest temperature
double temp=0;// Initializes what will be used by the scanner

//Scanner from java.util.
Scanner scan = new Scanner(System.in);

// Prompt the user to enter an integer
System.out.println("Please enter a temperature: ");
System.out.println("You may enter a negative number to quit.");

//Sets up a loop to keep the scanner running,
// as long as the input temp is greater than zero
for(int i=0; temp >= 0; i++)
{
// calls for a user input
temp = scan.nextDouble();

if(temp > 0)
{
// add to total number of entries by user
// add temp entered to combined temp of all entries
// for averaging
tempEntries += 1;
tempTotal += temp;

//if the most recently entered temp is higher
//than the previous high, it becomes the new high
if(temp > tempHigh)
{
tempHigh = temp;
}

//if this is the first run of the loop
//then this temp becomes the lowest temp
if(i==0)
{
tempLow = temp;
}

//if this most recently entered temp is lower
// than the previous low, it becomes the new low
if(temp < tempLow)
{
tempLow = temp;
}
}

// if the temp entered was not a positive number,
// then the program is going to output all the results
else
{
//finds the average temp by subtracting the total
// we had built up and the number of entries
double tempAvg = (tempTotal/tempEntries);

//rounds off all three to the nearest int using math.round
int roundedtempHigh = (int) Math.round(tempHigh);
int roundedtempLow = (int) Math.round(tempLow);
int roundedtempAvg = (int) Math.round(tempAvg);

//Outputs the High, Low and Average Temp
System.out.println("High temp: " + roundedtempHigh);
System.out.println("Low temp: " + roundedtempLow);
System.out.println("Average temp:" + roundedtempAvg);

}
}
}
}
```

Was This Post Helpful? 0

### #3 bulletb1331

• New D.I.C Head

Reputation: 0
• Posts: 4
• Joined: 13-September 07

## Re: Find smallest number of list

Posted 27 September 2007 - 08:50 PM

Ok thanks, i changed my code but i'm still getting the negative number as the lowest number even though i'm using and if(n>=0) so I have no idea why it's giving that answer back

here's the code

```/*
* Robert Durning
* HW4a
*/

//Determine Small, Large, Average values for a list of integers

import java.util.*;

public class SmLrgAvg
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);

System.out.println("Enter in a list of non-negative integers. End the list with a negative integer.");

int n = 0, count = 0;
double largestInteger = 0, smallestInteger = 0, sum = 0;
for (int i = 1; n >= 0; i ++)
{
n = keyboard.nextInt();
if (n >= 0)
{
sum = sum + n;
count = count + 1;
}
if (n >= largestInteger)
largestInteger = n;
if (i == 1);
smallestInteger = n;
if (n >= 0 && n < smallestInteger)
smallestInteger = n;
}
System.out.println("The largest integer is: " + largestInteger);
System.out.println("The smallest integer is: " + smallestInteger);
System.out.println("The average of your list is: " + sum/count);
}

}
```

Was This Post Helpful? 0

### #4 sl4ck3r

• D.I.C Regular

Reputation: 11
• Posts: 285
• Joined: 22-September 07

## Re: Find smallest number of list

Posted 27 September 2007 - 10:17 PM

why dont you just change your loop to a while (keyboard.hasNext()) or .hasNextInt() and then you dont have to exit your loop like that... or just use a break; and thatll exit the loop.

This post has been edited by sl4ck3r: 27 September 2007 - 10:20 PM

Was This Post Helpful? 0

### #5 orcasquall

• D.I.C Head

Reputation: 12
• Posts: 158
• Joined: 14-September 07

## Re: Find smallest number of list

Posted 28 September 2007 - 07:11 AM

This is the problem
```				if (i == 1);
smallestInteger = n;

```

Remove the semicolon at the end of the if condition. As it stands, smallestInteger is always assigned the input integer, and a negative integer is read at the end. So your smallestInteger is always negative.

Actually, if you're gonna do an "if (i==1)", you might as well do it for largestInteger as well.
This should work just as well
```for (int i = 1; n >= 0; i ++)
{
n = keyboard.nextInt();
sum = sum + n;
count = count + 1;
if (i==1)
{
largestInteger = n;
smallestInteger = n;
}
else
{
if (n > largestInteger) largestInteger = n;
if (n < smalllestInteger) smallestInteger = n;
}
}

```

The condition in the for loop already checks for non-negative n's, so you don't have to check again in the loop.

You might want to change the variable type for largestInteger and smallestInteger from double to int. Because they're um, integers?

Hope this helps!
Was This Post Helpful? 1

Reputation:

## Re: Find smallest number of list

Posted 27 February 2010 - 01:03 AM

heyaaaaaaaaaaa plz help meeeeeeeeeeeeeee
i got a program of finding lowest value among values enterd by user untill he enter 0 but 0 should not be included
eg
output is
5
6
23
4
68
654
0
lowest value is 4
Was This Post Helpful? -2

### #7 remorseless

• D.I.C Head

Reputation: 29
• Posts: 129
• Joined: 08-August 09

## Re: Find smallest number of list

Posted 27 February 2010 - 06:13 AM

Using a while loop is much cleaner; in other words, while the user is can input an integer, keep on going. Then, if that integer is negative, break out of that loop.

I have no idea why you declared largestInteger and smallestInteger and so on as doubles.

I also have no idea why you used if (i==1) smallestInteger = n when you the numbers are already initialised with 0.

Lastly, while your way worked, what if the user inputted "a"? Your program would crash, that's what. Using this way, if a user tried to input "a", it would tell him the average and so on.

```/* Robert Durning
* HW4a
*/
import java.util.*;

//Determine Small, Large, Average values for a list of integers
public class SmLrgAvg
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter in a list of non-negative integers. End the list with a negative integer.");
int count = 0, largestInteger = 0, smallestInteger = -1, sum = 0;
while(keyboard.hasInt())
{
int n = keyboard.nextInt();
if(n < 0) break; //Check if negative, and if so, break out of loop
if(smallestInteger == -1) smallestInteger = n; //First number is set to be smallest number
sum += n; count++; //Add 1 to count, and the number to sum
if(n > largestInteger) largestInteger = n; //Check if largest
if(n < smallestInteger) smallestInteger = n; //Check if smallest
}
System.out.println("The largest integer is: " + largestInteger);
System.out.println("The smallest integer is: " + smallestInteger);
System.out.println("The average of your list is: " + (sum/count));
}
}

```

Look at that code, see if it works, and most importantly, try to learn from it. Java is one of the greatest languages invented yet, so continue on learning it!

Hope I helped
Was This Post Helpful? 1

### #8 Dogstopper

• The Ninjaducky

Reputation: 2909
• Posts: 11,152
• Joined: 15-July 08

## Re: Find smallest number of list

Posted 27 February 2010 - 10:28 AM

Thank you remorseless, but that guest necro'd a 3 year old topic...
Was This Post Helpful? 1

### #9 petro123

• New D.I.C Head

Reputation: 0
• Posts: 1
• Joined: 04-May 11

## Re: Find smallest number of list

Posted 04 May 2011 - 08:19 PM

```
public class FindNumbers {
/**
* Determine Small, Large and Average values for a list of integer
*/
public static void main(String[] args) {

int[] numbers = {5,2,4,99,79,32,56,2,3245,78};
FindNumbers fn = new FindNumbers();
nowCompute(numbers);
}

private static void nowCompute(int[] numbers) {
int avg = 0;
int large = 0;

// large
for (int i=0; i < numbers.length; i++) {
avg = avg + numbers[i];
}
System.out.println("The Avarage  ==> " + avg/numbers.length);
// small
for (int s=0; s < numbers.length-1; s++) {

for (int s1=0; s1 < numbers.length-1; s1++) {

if (numbers[s1] > numbers[s1+1] ) {
int tmp = numbers[s1];
numbers[s1] = numbers[s1+1];
numbers[s1+1] = tmp;
}
}
}
// Sorted
for (int s=0; s < numbers.length; s++) {
System.out.println(numbers[s]);

}
large = numbers.length-1;
for (int s=0; s < numbers.length; s++) {
if (s==0) {
System.out.println("Small Number  ==" + numbers[0]);
}
if (s==large) {
System.out.println("Large Number  ==" + numbers[large]);
}
}
}
}

```

Was This Post Helpful? 0

### #10 pbl

• There is nothing you can't do with a JTable

Reputation: 8362
• Posts: 31,955
• Joined: 06-March 08

## Re: Find smallest number of list

Posted 05 May 2011 - 06:09 AM

And the question is ?
And don't necro'd years old thread. Start your own.
Was This Post Helpful? 0

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }