Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: New To Java (Help with arrays)

  1. #1
    Junior Member
    Join Date
    Sep 2010
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default New To Java (Help with arrays)

    Hello, i've just started using java, but I don't seem to be getting the following output correct:

    import java.util.Scanner;
     
    public class MinNum
    {
    	public static void main(String[] args)
    	{
    		final int NUMBER_OF_ELEMENTS = 10;
    		int[] numbers = new int[NUMBER_OF_ELEMENTS];
    		int min = 0;
     
    		java.util.Scanner input = new java.util.Scanner(System.in);
    		for(int i = 0 ; i < NUMBER_OF_ELEMENTS ; i++)
    		{
    			System.out.print("Enter number: ");
    			numbers[i] = input.nextInt();
    		}
    		for( int i = 0 ; i  < numbers[NUMBER_OF_ELEMENTS] ; i++)
    		{
    			if(numbers[i] > min)
    				min = min;
    			else
    			{
    				min = numbers[i];
    			}
    		}
    		System.out.print("The smallest number is "+min);
    	}
    }
    The code is supposed to check all the numbers in the array and return the smallest. I'm hoping someone can give me a hint as to where its going wrong.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,332
    Thanks
    181
    Thanked 829 Times in 772 Posts
    Blog Entries
    5

    Default Re: New To Java (Help with arrays)

    For future reference, please post the full error message you are receiving. At quick glance, I'd bet
    for( int i = 0 ; i  < numbers[NUMBER_OF_ELEMENTS] ; i++)
    is (at least one of) the culprit. To get the size of an array, call array.length (eg numbers.length)

  3. #3
    Junior Member
    Join Date
    Sep 2010
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: New To Java (Help with arrays)

    Sorry about that. It doesn't give me any errors, just the wrong output.

    When running the code:

    Enter Numbers: 5 7 9[ENTER]
    Output: Enter Numbers: Enter Numbers: Enter Numbers

    Thanks

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,332
    Thanks
    181
    Thanked 829 Times in 772 Posts
    Blog Entries
    5

    Default Re: New To Java (Help with arrays)

    And how do you want it to behave? It will loop through however you write it, and right now will ask you for number input 10 times before trying to calculate the min (I say try, because technically it will not if you set min=0, min=Integer.MAX_VALUE would be more appropriate)

  5. #5
    Junior Member
    Join Date
    Sep 2010
    Posts
    3
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: New To Java (Help with arrays)

    Hi SilentPirate,

    Please try the following code to check for Min.

    import java.util.Scanner;
    public class MinNum
    {
    public static void main(String[] args)
    {
    int min = 0;
    java.util.Scanner scan = new java.util.Scanner(System.in);
    System.out.print("Enter number: ");
    String input=new String(scan.nextLine());
    String[] numbers=input.split(" ");
    for( int i = 0 ; i < numbers.length; i++)
    {
    int value=Integer.parseInt(numbers[i]);
    if(i==0 || value<min)
    min=value;
    else
    min = min;
    }
    System.out.print("The smallest number is "+min);
    }
    }

  6. #6
    Junior Member
    Join Date
    Sep 2010
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: New To Java (Help with arrays)

    I tried your code and it works.

    I managed to figure it out yesterday with the following code:

    import java.util.Scanner;
     
    public class Check
    {
    	public static void main(String[] args)
    	{		
    		Scanner input = new Scanner(System.in);
     
    		System.out.print("Enter numbers: ");
     
    		int num, min, count = 1;
     
    	    num = input.nextInt();
    	    min = num;
     
    		while (num != 0)
    		{
    			num = input.nextInt();
     
    			if((num!=0) && (num<min))
    			{
    				min = num;
    			}
    			else if(num == min)
    			{
    				count++;
    			}
    		}
    		System.out.print("The smallest number is "+ min +"\nThe occurrence count of the smallest number is "+ count);
    	}
    }
    Last edited by SilentPirate; September 15th, 2010 at 06:11 AM.

  7. #7
    Junior Member
    Join Date
    Sep 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: New To Java (Help with arrays)

    You weren't that far off with your first attempt. You've actually got the answers before.
    I see 3 problems with the initial code and I'll point them out so you can understand what went wrong before:

    1. You will always get min = 0. That is, unless you enter a negative number.
    You should probably start off with
    min = numbers[0];
    which is basically what you are doing now in your new code. Or you could do what copeg suggested and do
    min = Integer.MAX_VALUE;

    2. You aren't looping through the entire array, or perhaps even beyond its size, which would generate an exception. Use
    for(int i = 0; i < NUMBER_OF_ELEMENTS; i++)
    like you did in the first loop, or number.length which is probably better (because it will always be correct even if you for some reason change the size of your array.

    3. You could skip the else and just change your if-test:
    if(numbers[i] < min) min = numbers[i];

    However, in your new code... do you want to use 0 as a special number to tell the program you want to quit?

Similar Threads

  1. How to Sort an Array using the java.util.Arrays class
    By JavaPF in forum Java SE API Tutorials
    Replies: 2
    Last Post: May 17th, 2014, 01:16 AM
  2. Replies: 1
    Last Post: December 22nd, 2011, 08:55 AM
  3. Arrays
    By mlan in forum Java Theory & Questions
    Replies: 2
    Last Post: February 26th, 2010, 09:23 AM
  4. 2d Arrays
    By mgutierrez19 in forum Collections and Generics
    Replies: 5
    Last Post: October 27th, 2009, 04:08 PM
  5. Details about 'CopyTo' of Arrays in Java
    By Fendaril in forum Collections and Generics
    Replies: 18
    Last Post: November 13th, 2008, 07:31 AM