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 5 of 5

Thread: Stuck :(

  1. #1
    Junior Member hing09's Avatar
    Join Date
    Mar 2010
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Stuck :(

    I'm trying to create a program that asks a user to enter 5 numbers and prints the highest number entered.

    So far i have:
    import java.util.Scanner;
    import java.io.*;
    import java.io.IOException;
    import java.util.*;
     
    public class arrayNum
    {
     
           public static void main(String[] args)throws IOException{
     
                    Scanner input = new Scanner(System.in);
                    int array[] = new int[5];
                    int count=0;
                    int numbers;
     
     				System.out.println("Please enter 5 numbers");
     
                    {
                    while (count < 5)
                    {
    				numbers = input.nextInt();
    						count++;
     
                    }
     
     
    				}
     
     
    {
     
    				int max = array[5];
     
    				for(int i = 0; i < array.length; i++)
    				{
     
    				 if(array[i] > max)
    				 {
     				 	max = array[i];
    				 }
    			 	}
    }
     
     
     
     
     
     
     
    System.out.println("Largest number is:  " + max  );
     
    										}
    }
    Any help will be greatly appreciated!

    Regards
    Rob.
    Last edited by helloworld922; March 16th, 2010 at 10:54 PM.


  2. #2
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    494
    Thanks
    8
    Thanked 48 Times in 46 Posts

    Default Re: Stuck :(

    Go through this, and do your own homework.
    The Java™ Tutorials

    db

  3. #3
    Junior Member hing09's Avatar
    Join Date
    Mar 2010
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Stuck :(

    Mate this is my own homework, i just don't know why its not outputting the highest number entered?

    Geez take it easy..

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Stuck :(

    The reason your program is failing is because Java arrays start from 0, not 1. So, when you try to reference array[5], you are really trying to access the 6th element of the array. This of course doesn't exists, and Java will tell you so. Simply change it to any valid index for the array (I'll leave this up to you now that you know where to look).

    Darryl, please keep it respectful. Helping in this case is completely acceptable because it is a simple error and they have shown that they have tried to solve the problem themselves.

  5. The Following User Says Thank You to helloworld922 For This Useful Post:

    hing09 (March 17th, 2010)

  6. #5
    Junior Member hing09's Avatar
    Join Date
    Mar 2010
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Stuck :(

    Thanks helloworld922 my program now works with your solution!

Similar Threads

  1. Totally stuck on this assignment
    By Sgiahatch in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 25th, 2010, 04:25 PM
  2. Help I'm stuck on code.
    By GreenM in forum Loops & Control Statements
    Replies: 3
    Last Post: January 29th, 2010, 09:27 PM
  3. I'm stuck
    By iank in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 5th, 2009, 10:21 AM
  4. Problem while programming a simple game of Car moving on a road
    By rojroj in forum Java Theory & Questions
    Replies: 3
    Last Post: April 2nd, 2009, 10:24 AM
  5. stuck on this program can any one help me
    By clive in forum AWT / Java Swing
    Replies: 2
    Last Post: March 10th, 2009, 05:54 PM