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

Thread: Printing highest number of array. Can't find my error.

  1. #1
    Member
    Join Date
    Feb 2013
    Posts
    38
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default Printing highest number of array. Can't find my error.

    Hi all. Your's truly here again. Hope everyone is doing well. Alas, I'm having trouble pointing out what I'm doing wrong here with this little program I'm writing for class. What I need to do is ask the user to input numbers, store them in an array of ten, and then display the largest one. Simple enough I thought. But when I run my program, it just keeps displaying '2' as the largest entry no matter what. I was really happy with the speed at which the code poured out of my n00b brain that I'm really disappointed it's not working. Any thoughts would be appreciated.

    import javax.swing.JOptionPane;
     
    public class PeerProgram3
    {
    	public static void main(String[] args)
    	{
    		final int TOTAL = 10;
    		String input;
    		String newInput;
    		int[] numbers = new int[TOTAL];
     
     
    		JOptionPane.showMessageDialog(null,"I'm going to ask you to input 10 numbers.");
    		input = JOptionPane.showInputDialog("Please enter a number:");
    		for (int index = 1; index < TOTAL; index++)
    		{
    			newInput = JOptionPane.showInputDialog("Please enter another number");
    			numbers[index] = Integer.parseInt(newInput);
     
    		}
    		int highest = numbers[0];
    		for (int index = 1; index < TOTAL; index++)
    		{
    			if (numbers[index] > highest)
    			highest = numbers[index];
    			JOptionPane.showMessageDialog(null, "The highest number you entered is " + highest + ".");
    			System.exit(0);
    		}
     
     
    	}


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Printing highest number of array. Can't find my error.

    Don't be in a rush to code. Have a plan! and be able to express it in ordinary English before coding.

    input = JOptionPane.showInputDialog("Please enter a number:");
    for (int index = 1; index < TOTAL; index++)
    {
        newInput = JOptionPane.showInputDialog("Please enter another number");
        ...

    Here you appear to be asking the user for an input but then do absolutely nothing with it. It seems to me that a plan or "recipe" for this problem will only involve one thing that you refer to as "input": you get the input, you deal with the input, you move on. There isn't really a need for an input and a newInput.

    for (int index = 1; index < TOTAL; index++)
    {
        if (numbers[index] > highest)
        highest = numbers[index];
        JOptionPane.showMessageDialog(null, "The highest number you entered is " + highest + ".");
        ...

    Here you are displaying something every time you go around the loop. That can't be right. Go round the loop until, at the end of the array you know what the largest value is. And then, after the loop has finished, display the message.

  3. #3
    Member
    Join Date
    Feb 2013
    Posts
    38
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default Re: Printing highest number of array. Can't find my error.

    Thanks pbrockway2. Yeah, it's not really as well put together as I thought, now that I look at it. I wasn't really rushing through it, I was just hoping that because the code seem to pour from my finger tips, I was really catching on.

Similar Threads

  1. 3x3 array find largest number
    By joecan2010 in forum Loops & Control Statements
    Replies: 1
    Last Post: November 27th, 2012, 11:56 PM
  2. [SOLVED] how to find all occurrences of a number in an array recursivelly
    By mia_tech in forum What's Wrong With My Code?
    Replies: 4
    Last Post: June 13th, 2012, 01:37 PM
  3. Find Biggest Number in Array
    By jo15765 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: May 12th, 2012, 03:01 PM
  4. Finding the highest number in an array?
    By halfwaygone in forum Algorithms & Recursion
    Replies: 1
    Last Post: April 17th, 2010, 03:56 PM
  5. Replies: 4
    Last Post: June 10th, 2009, 01:04 AM