Results 1 to 7 of 7

Thread: Arrays

  1. #1
    TheRocket is offline Member
    Join Date
    Nov 2008
    Posts
    20
    Rep Power
    0

    Default Arrays

    Ok I have created the code but it wont compile, the error I get is n is not initialized can anyone help? Code is below.

    Java Code:
    import javax.swing.*;
    public class ArrayTest
    {
    	public static void main(String[] args)
        {
            int[] numbers = new int[3];
            int count = getInput(numbers);
    
            for (int i = 0; i < count; i++)
            {
            	System.out.println(i + "\t" + numbers[i]);
            }
            System.exit(0);
        }
    
        public static int getInput(int[] numbers)
        {
            String input;
            int count = 0, n;
    
            while (count <= 2)
            {
                numbers[count] = n;
                count++;
                input = JOptionPane.showInputDialog(
                           "Enter a number");
                n = Integer.parseInt(input);
            }
    	   return count;
        }
    }

  2. #2
    neilcoffey is offline Senior Member
    Join Date
    Nov 2008
    Posts
    286
    Rep Power
    6

    Default

    To "initialise" means to "give an initial value to" a variable. When you create a local variable-- in other words, one that you define inside a method-- Java doesn't give it any initial value. In your code, you're trying to refer to n (in the line numbers[count] = n)before you've set any value to it.

    By the way, you don't have to declare all your variables at the very top of the method (as you do in C, for example). In Java, you can generally just declare them where you first need to use them.

  3. #3
    TheRocket is offline Member
    Join Date
    Nov 2008
    Posts
    20
    Rep Power
    0

    Default

    So I could just declare n as 0 and it would work?

  4. #4
    TheRocket is offline Member
    Join Date
    Nov 2008
    Posts
    20
    Rep Power
    0

    Default

    Ok I have got it to compile, but now when I run it. It doesn't output what I want it to. Example: I input 3,4,5 it should output:

    0 3
    1 4
    2 5

    but instead it only outputs:

    0 3
    1 4

    Code below.

    Java Code:
    public class ArrayTest
    {
    	public static void main(String[] args)
        {
            int[] numbers = new int[3];
            int count = getInput(numbers);
    
            for (int i = 0; i < count; i++)
            {
            	System.out.println(i + "\t" + numbers[i]);
            }
            System.exit(0);
        }
    
        public static int getInput(int[] numbers)
        {
            String input;
            int count = 0, n;
            
            input = JOptionPane.showInputDialog(
                       "Enter a number");
            n = Integer.parseInt(input);
            
            while (count < 2)
            {
                numbers[count] = n;
                input = JOptionPane.showInputDialog(
                           "Enter a number");
                n = Integer.parseInt(input);
                count++;
            }
    	   return count;
        }
    }

  5. #5
    neilcoffey is offline Senior Member
    Join Date
    Nov 2008
    Posts
    286
    Rep Power
    6

    Default

    So your problem is that the very last number isn't being added to the array. Try and think through step be step what happens on the last run through the while loop to figure out why this is.

  6. #6
    TheRocket is offline Member
    Join Date
    Nov 2008
    Posts
    20
    Rep Power
    0

    Default

    Nope I can't think of anything except moving the count++; above the input box which still gives me the same output.

  7. #7
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    7

    Default

    First glance makes me think your while parameter has broken logic.
    you have
    numbers[0] = n
    numbers[1] = n
    return count

    if you want your array to have 3 sections you need [0,2] not [0,2); meaning don't stop until you section 0, 1, AND 2 filled.

Similar Threads

  1. Help with Arrays
    By bri1547 in forum New To Java
    Replies: 4
    Last Post: 08-01-2008, 05:12 AM
  2. arrays
    By hasysf in forum New To Java
    Replies: 12
    Last Post: 07-28-2008, 02:38 AM
  3. Help on Arrays...
    By cuellar14 in forum New To Java
    Replies: 4
    Last Post: 07-25-2008, 08:16 PM
  4. need help with arrays
    By Jman in forum New To Java
    Replies: 17
    Last Post: 07-21-2008, 02:34 AM
  5. arrays help
    By Warren in forum New To Java
    Replies: 6
    Last Post: 11-23-2007, 07:23 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
  •