Results 1 to 7 of 7
Thread: Arrays
- 12-10-2008, 03:12 PM #1
Member
- Join Date
- Nov 2008
- Posts
- 20
- Rep Power
- 0
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; } }
- 12-10-2008, 04:25 PM #2
Senior Member
- Join Date
- Nov 2008
- Posts
- 286
- Rep Power
- 5
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.Neil Coffey
Javamex - Java tutorials and performance info
- 12-10-2008, 04:36 PM #3
Member
- Join Date
- Nov 2008
- Posts
- 20
- Rep Power
- 0
So I could just declare n as 0 and it would work?
- 12-10-2008, 05:18 PM #4
Member
- Join Date
- Nov 2008
- Posts
- 20
- Rep Power
- 0
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; } }
- 12-10-2008, 05:39 PM #5
Senior Member
- Join Date
- Nov 2008
- Posts
- 286
- Rep Power
- 5
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.
Neil Coffey
Javamex - Java tutorials and performance info
- 12-10-2008, 05:46 PM #6
Member
- Join Date
- Nov 2008
- Posts
- 20
- Rep Power
- 0
Nope I can't think of anything except moving the count++; above the input box which still gives me the same output.
- 12-10-2008, 06:00 PM #7
Similar Threads
-
Help with Arrays
By bri1547 in forum New To JavaReplies: 4Last Post: 08-01-2008, 05:12 AM -
arrays
By hasysf in forum New To JavaReplies: 12Last Post: 07-28-2008, 02:38 AM -
Help on Arrays...
By cuellar14 in forum New To JavaReplies: 4Last Post: 07-25-2008, 08:16 PM -
need help with arrays
By Jman in forum New To JavaReplies: 17Last Post: 07-21-2008, 02:34 AM -
arrays help
By Warren in forum New To JavaReplies: 6Last Post: 11-23-2007, 07:23 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks