# Thread: finding largest and smallest integer in array

1. Member
Join Date
Feb 2013
Posts
6
Rep Power
0

## finding largest and smallest integer in array

Hello. I am successfully finding the largest and smallest integers in an array. The problem is that when I run the program and it actually finds the largest and smallest, the numbers are all mushed together. How do I get each number on its own seperate line?
Java Code:
```import java.util.Scanner;

public class lab6edit
{
public static void main(String[] args)
{
final int LENGTH = 100;
double[] values = new double[LENGTH];
int currentSize = 0;

System.out.println("Please enter values, Q to quit");
Scanner in = new Scanner(System.in);
while (in.hasNextDouble() && currentSize < values.length)
{
values[currentSize] = in.nextDouble();
currentSize++;
}

double largest = values[0];
for (int i = 1; i < currentSize; i++)
{
if (values[i] > largest)
{
largest = values[i];
}
}
double smallest = values[0];
for (int i = 1; i < currentSize; i++)
{
if (values[i] < smallest)
{
smallest = values[i];
}
}

for (int i = 0; i < currentSize; i++)
{
System.out.print(values[i]);
if (values[i] == largest)
{
System.out.print(" <== largest value");
System.out.println();
}

if (values[i] == smallest)
{
System.out.print(" <== smallest value");
System.out.println();
}
}
}
}```
And I keep getting this. I entered the values 22,34,86,3,12 and got this:
Java Code:
```22.034.086.0 <== largest value
3.0 <== smallest value
12.0```

2. ## Re: finding largest and smallest integer in array

Are you familiar with the difference between System.out.print(...) and System.out.println(...)?

3. Member
Join Date
Feb 2013
Posts
6
Rep Power
0

## Re: finding largest and smallest integer in array

Yes I am familiar with the difference but I'm still too dumb to see what's wrong...I've tried using both and still can't figure it out. It works with just finding the largest but when I want to find the largest AND the smallest it does what I showed before.

4. ## Re: finding largest and smallest integer in array

What is your goal? What do you want the printout to look like?

5. Member
Join Date
Feb 2013
Posts
6
Rep Power
0

## Re: finding largest and smallest integer in array

I want it to look like this:

22.0
34.0
86.0 <== largest value
3.0 <== smallest value
12.0

Without the numbers clumping together.
Last edited by csrLewis93; 02-18-2013 at 05:05 AM.

6. ## Re: finding largest and smallest integer in array

OK, so you want...

• one number to be shown on each line.
• But you don't want the newline to occur right after the number is printed, but sometimes after a message is printed as well.
• You want a new line to occur *regardless* of whether or not the message is printed, (think about what the if block means for this)
• And again, you want the new line to be printed *after* the message is shown.

So think on this ... should any println() statements be inside of an if block? You only have them inside of an if block if you only want them printed if the if condition is true, but you don't want this, do you? There's a better place to put the println() statement, and this logic will point you to that place...

7. ## Re: finding largest and smallest integer in array

Edit: sorry about being so indirect with the answers, but I have a feeling that you'll be able to figure this one out easy and in a short time too!

8. Member
Join Date
Feb 2013
Posts
6
Rep Power
0

## Re: finding largest and smallest integer in array

Thank you! I just took it out of the if statement and it works fine now. It's always something simple, right?
Last edited by csrLewis93; 02-18-2013 at 05:28 AM.

9. ## Re: finding largest and smallest integer in array

Originally Posted by csrLewis93
Thank you! I just took it out of the if statement and it works fine now. It's always something simple, right?
I wish it were always something simple, but in this case it is. The key to solving stuff like this problem is to try to mentally walk through your code as if you were the JVM and you were running it, and seeing in your mind what happens on each line.

10. Member
Join Date
Feb 2013
Posts
6
Rep Power
0

## Re: finding largest and smallest integer in array

Yes thank you. I just started learning java a few weeks ago and will mentally walk through my code next time.

#### Posting Permissions

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