Results 1 to 10 of 10
  1. #1
    csrLewis93 is offline Member
    Join Date
    Feb 2013
    Posts
    6
    Rep Power
    0

    Default 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. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: finding largest and smallest integer in array

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

  3. #3
    csrLewis93 is offline Member
    Join Date
    Feb 2013
    Posts
    6
    Rep Power
    0

    Default 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. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: finding largest and smallest integer in array

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

  5. #5
    csrLewis93 is offline Member
    Join Date
    Feb 2013
    Posts
    6
    Rep Power
    0

    Default 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. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default 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. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default 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. #8
    csrLewis93 is offline Member
    Join Date
    Feb 2013
    Posts
    6
    Rep Power
    0

    Default 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. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: finding largest and smallest integer in array

    Quote Originally Posted by csrLewis93 View Post
    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. #10
    csrLewis93 is offline Member
    Join Date
    Feb 2013
    Posts
    6
    Rep Power
    0

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

Similar Threads

  1. Finding a Smallest Integer
    By antnas in forum New To Java
    Replies: 3
    Last Post: 10-26-2012, 05:20 AM
  2. Finding the Smallest Element in an Array
    By Cod in forum New To Java
    Replies: 1
    Last Post: 02-20-2011, 03:05 AM
  3. Finding the largest number in an array
    By starchildren3317 in forum New To Java
    Replies: 14
    Last Post: 11-03-2010, 07:49 AM
  4. Array[] get smallest/largest value
    By bobocheez in forum New To Java
    Replies: 13
    Last Post: 09-10-2010, 12:16 AM
  5. Finding largest and smallest integer
    By mlhazan in forum New To Java
    Replies: 2
    Last Post: 01-12-2008, 11:30 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
  •