Results 1 to 5 of 5
- 09-07-2009, 12:33 PM #1
Member
- Join Date
- Sep 2009
- Posts
- 9
- Rep Power
- 0
Array length and printing out uninitialized array.
I am having trouble setting array length. My program reads in a file and reads integers only. However, I can print out all the integers, but once the last integer is read, it prints out 'null' until the declared array length ends.
below is my code:
Java Code:import java.util.Scanner; import java.io.File; import java.util.StringTokenizer; import java.util.InputMismatchException; import java.io.FileNotFoundException; import java.util.NoSuchElementException; public class KashiwabaraNicole3 { /******************************************************************************* * Outputs integers from user input external files. ********************************************************************************/ public static void main(String[] commandlineArguments)throws InputMismatchException { if(commandlineArguments.length == 0) { System.out.println("Please enter the file name " + "as the 1st commandline argument."); } else { Integer[] array = KashiwabaraNicole3.readFileReturnIntegers(commandlineArguments[0]); KashiwabaraNicole3.printArrayAndIntegerCount(array, commandlineArguments[0]); } } public static Integer []readFileReturnIntegers(String inputFile) { Integer [] array = new Integer [8000]; Integer size = new Integer(0); File file = new File(inputFile); Scanner scanFile = null; try { scanFile = new Scanner(file); } catch (FileNotFoundException exception) { System.out.print("ERROR: File not found for \""); System.out.println(inputFile +"\""); } if(scanFile != null) { while (scanFile.hasNextLine()) { try { int element = scanFile.nextInt(); array[size] = element; ++size; } catch (InputMismatchException exception) { scanFile.next(); } catch (NoSuchElementException exception) { scanFile.next(); } } } return array; } public static void printArrayAndIntegerCount(Integer [] array, String inputFile) { int num = 0; System.out.println("number of integers in file " + inputFile + " = " + array.length); for (int i = 0; i < 10; i++) { System.out.println("index = " + i + ", element = "+ array[i]); } } }
Ive attached the files.
My output for electricity:
number of integers in file electricity.txt = 8000
index = 0, element = 1877
index = 1, element = 1923
index = 2, element = 1879
index = 3, element = 2000
index = 4, element = null
index = 5, element = null
index = 6, element = null
index = 7, element = null
index = 8, element = null
index = 9, element = null.....(until index = 8000, element = null)
Thank you so much in advance for your help!!! :)Last edited by nicolek808; 09-07-2009 at 01:20 PM.
- 09-07-2009, 12:58 PM #2
Senior Member
- Join Date
- Dec 2008
- Location
- Hong Kong
- Posts
- 473
- Rep Power
- 13
as length of array is 8000
Java Code:Integer [] array = new Integer [8000];
i suggest you LinkedHashMap, Vector etc to store the integer
or you can try to return value of "size", so that you can loop for 0 to < size onlyLast edited by mtyoung; 09-07-2009 at 01:02 PM.
- 09-07-2009, 01:09 PM #3
Member
- Join Date
- Sep 2009
- Posts
- 9
- Rep Power
- 0
Thank you for your suggestion. :)
I tried using size as the array length, however, I cannot pass it into the method.
I believe it has something to do with passing 'element' into the array.
I have re-written as:
Java Code:array[size] = scanFile.nextInt(); ++size;
Any Suggestions for this?Last edited by nicolek808; 09-07-2009 at 01:19 PM.
-
1) As noted above, why not use a varaible sized collection instead of a fixed-sized array?
2) If you must use a large fix sized array, (sorry if I'm oversimplifying), why not check if the element is null before printing it? If not null, print it, if null, break out of the for loop.
- 09-10-2009, 10:12 AM #5
Member
- Join Date
- Sep 2009
- Posts
- 9
- Rep Power
- 0
Similar Threads
-
Printing Array elements
By new person in forum New To JavaReplies: 2Last Post: 02-23-2009, 09:57 PM -
printing array
By mayhewj7 in forum New To JavaReplies: 6Last Post: 02-12-2009, 03:01 PM -
Printing Byte Array
By suchismitasuchi in forum New To JavaReplies: 3Last Post: 01-19-2009, 11:58 AM -
Using reflection to check array type and length
By Java Tip in forum java.langReplies: 0Last Post: 04-23-2008, 09:15 PM -
Using reflection to check array type and length
By Java Tip in forum java.langReplies: 0Last Post: 04-14-2008, 09:42 PM
Bookmarks