Results 1 to 10 of 10
 02182013, 03:53 AM #1Member
 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(); } } } }
Java Code:22.034.086.0 <== largest value 3.0 <== smallest value 12.0

Re: finding largest and smallest integer in array
Are you familiar with the difference between System.out.print(...) and System.out.println(...)?
 02182013, 04:56 AM #3Member
 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.

Re: finding largest and smallest integer in array
What is your goal? What do you want the printout to look like?
 02182013, 05:02 AM #5Member
 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; 02182013 at 05:05 AM.

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...

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!
 02182013, 05:25 AM #8Member
 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; 02182013 at 05:28 AM.

Re: finding largest and smallest integer in array
 02182013, 05:37 AM #10Member
 Join Date
 Feb 2013
 Posts
 6
 Rep Power
 0
Similar Threads

Finding a Smallest Integer
By antnas in forum New To JavaReplies: 3Last Post: 10262012, 05:20 AM 
Finding the Smallest Element in an Array
By Cod in forum New To JavaReplies: 1Last Post: 02202011, 03:05 AM 
Finding the largest number in an array
By starchildren3317 in forum New To JavaReplies: 14Last Post: 11032010, 07:49 AM 
Array[] get smallest/largest value
By bobocheez in forum New To JavaReplies: 13Last Post: 09102010, 12:16 AM 
Finding largest and smallest integer
By mlhazan in forum New To JavaReplies: 2Last Post: 01122008, 11:30 PM
Bookmarks