Results 1 to 7 of 7
Thread: whats wrong
- 06-15-2010, 12:02 AM #1
Member
- Join Date
- Jun 2010
- Posts
- 3
- Rep Power
- 0
whats wrong
Hello,
can anyone tell me why on earth am i getting only zeros while i'm iterating the array in main method ?
Java Code:public static int[] readFile(String fName) { try { Scanner scanner = new Scanner(new File(fName)); while (scanner.hasNext()) { myNext = new int[scanner.toString().length()]; int c = 0; myNext[c] = scanner.nextInt(); System.out.print( myNext[c] + " "); //dbg output c++; } } catch (FileNotFoundException e) { System.err.println("FileNotFoundException: " + e.getMessage()); } return myNext; } public static void main(String[] args) { System.out.println( System.getProperty("user.dir")); int fileArrayLength = readFile("readme.txt").length; int[] fileArray = readFile("readme.txt"); // selectionSort(fileArray); System.out.println(); for (int i = 0; i <= fileArrayLength-1; i++) { System.out.print(fileArray[i] + " "); }
-
Do you want to create a new myNext array each time the while loop iterates? If myNext is being returned by the readFile method, should it be a static class field (my own opinion here is "no")? What the do you think this code does:
?Java Code:myNext = new int[scanner.toString().length()];
Have you checked what scanner.toString() returns? And why does the length of this String matter? Finally, what does the input fill look like?Last edited by Fubarable; 06-15-2010 at 12:59 AM.
- 06-15-2010, 04:22 AM #3
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Means about this.
As Fubarable explains, what happen if the scanner found more than one line of text. Think about that on each line found in the scanner what happen in myNext. Put a print statement to check the length of it and check.Java Code:while (scanner.hasNext()) { myNext = new int[scanner.toString().length()]; }
- 06-15-2010, 09:52 AM #4
Member
- Join Date
- Jun 2010
- Posts
- 3
- Rep Power
- 0
Since i dont know the length of the file input - i thought i'll have to create every time a new array with its new length.
was suppose to read the length of scanner and parse it as an array length for myNextJava Code:myNext = new int[scanner.toString().length()];
How else would i get the scanner length ?
Add: the file contains only space separated numbersLast edited by atenv; 06-15-2010 at 09:54 AM.
- 06-15-2010, 11:34 AM #5
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 4
Also, in your loop, you always set c to 0. You do increment it at the end of the loop, but in the next iteration it gets set back to 0.
Ever seen a dog chase its tail? Now that's an infinite loop.
- 06-15-2010, 01:28 PM #6
Member
- Join Date
- Jun 2010
- Posts
- 3
- Rep Power
- 0
-
and delete any contents held in the array previously. No I wouldn't do this.
But if you display what scanner.toString() holds, you'll find that your code does nothing of the kind. To toString method here will only display some information about the scanner object and taking its length is useless to your program. As to getting the length of data, that depends upon how the data is stored. Previously I asked for you to display it, but we've yet to see it. You may have to either use an ArrayList which won't care about the size of the data, or else run through the data file twice, first to get the length and the second time to extract the data itself.was suppose to read the length of scanner and parse it as an array length for myNextJava Code:myNext = new int[scanner.toString().length()];
How else would i get the scanner length ?
Similar Threads
-
What did I do wrong?
By GoingThroAPhase in forum New To JavaReplies: 4Last Post: 04-03-2010, 04:51 AM -
Threads don't start after few iterations
By gaurav2211 in forum Threads and SynchronizationReplies: 2Last Post: 12-18-2009, 09:34 AM -
Wrong output (well.. the one who's wrong is probably me ;) )
By shacht1 in forum New To JavaReplies: 2Last Post: 11-22-2009, 03:48 PM -
what am i doing wrong here??
By tornbacchus in forum New To JavaReplies: 19Last Post: 04-16-2009, 03:54 AM -
what wrong
By pro85 in forum New To JavaReplies: 3Last Post: 02-09-2009, 01:07 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks