Results 1 to 11 of 11
Thread: Help With Scanner
- 10-12-2009, 07:48 PM #1
Member
- Join Date
- Mar 2009
- Posts
- 17
- Rep Power
- 0
Help With Scanner
I trust I'm posting this in the right forum. You nice people do consider 3 semesters of java still "new," right??
Anyway, I'm having a problem with Scanner. I have my program prompting me for a file name, and when I type in a file name and hit enter, the program does not seem like it's going to the next step. Now, when I step through in debug mode, it looks like it's making it to a few lines down, where I start scanning the file and counting the number of lines. But my lines variable goes waaaaay past the true number of lines in the file.
Anyway, what I don't understand, mainly, is why my program seems to get stuck. Is it because it's counting the lines? Also, why does it keep incrementing? My file has 10 lines...
Here's my code:
Java Code
Here is everything in the text file I'm scanning:Java Code:public static void main(String[] args) { String filename; int lines = 0; Scanner input_scan = new Scanner(System.in), file_scan; System.out.print("Enter a file name: "); filename = input_scan.next(); file_scan = new Scanner(filename); while (file_scan.hasNextLine()) lines++;
1234 23 widget1
3456 123 widget2
4356 45 widget3
1056 1234 widget4
9999 237 widget5
9322 22 widget6
5322 755 widget7
3422 2 widget8
3333 95 widget9
1111 15 widget10
That's it.
Any help would be greatly appreciated!Last edited by Fubarable; 10-12-2009 at 08:43 PM. Reason: code tags added for readability
-
Where are you reading in the lines from the file? It appears that you've truncated the code that you've posted. Oh, by the way, I've placed code tags in your code, something you may wish to do yourself in future posts.
Much luck
- 10-12-2009, 09:24 PM #3
Member
- Join Date
- Mar 2009
- Posts
- 17
- Rep Power
- 0
Fubarable.. nice name. heh :)
I thought I had put code tags around my code. *confused*
Anyway, here's where I am counting the lines (but not yet saving them anywhere):
Java Code:while (file_scan.hasNextLine()) lines++;
-
This never gets a nextLine and so will never progress and will loop forever.Java Code:while (file_scan.hasNextLine()) lines++;
If you just want to count lines, at least do this:
Also, always enclose loop blocks and control blocks in curly braces. This will help when you want to add more code.Java Code:while (file_scan.hasNextLine()) { lines++; file_scan.nextLine(); // get the line and discard it. }
- 10-12-2009, 09:41 PM #5
Member
- Join Date
- Mar 2009
- Posts
- 17
- Rep Power
- 0
I get it now. I didn't realize that I had to discard the one line to get it to count the next. Thank you so much!!
-
I think that the Scanner has an internal pointer or placeholder pointing to where it currently is located in the file. Your initial code has that placeholder set at the very top of the file, and you continually ask the scanner if there are more lines to read but don't move the placeholder, so it will continue to say "yes" forever.
- 10-12-2009, 10:10 PM #7
Member
- Join Date
- Mar 2009
- Posts
- 17
- Rep Power
- 0
I see. That makes a lot of sense.
- 10-12-2009, 11:16 PM #8
Member
- Join Date
- Mar 2009
- Posts
- 17
- Rep Power
- 0
Now I'm getting a FileNotFoundException at compile time (bolded part)! How does it know the file can't be found when I haven't even given the file name yet?
I don't understand this stuff.
Java Code:import java.util.Scanner; import java.io.*; public class RecordSorter { static int compare_count, move_count; public static void main(String[] args) throws FileNotFoundException { String filename, p_name; int lines = 0, p_num, p_qty; Scanner input_scan = new Scanner(System.in), file_scan; System.out.print("Enter a file name: "); [b]filename = input_scan.nextLine();[/b] input_scan.close(); file_scan = new Scanner(new File(filename)); while (file_scan.hasNextLine()) { lines++; file_scan.nextLine(); } Record[] records = new Record[lines]; for (int i = 0; i < records.length; i++) { p_num = file_scan.nextInt(); p_qty = file_scan.nextInt(); p_name = file_scan.next(); records[i] = new Record(p_num, p_qty, p_name); } insertionSort(records); for (int i = 0; i < records.length; i++) System.out.println(records[0]); } public static void selectionSort(Record[] data) { compare_count = 0; move_count = 0; int min; Record temp; for (int index = 0; index < data.length - 1; index++) { min = index; for (int scan = index + 1; scan < data.length; scan++) { compare_count++; if (data[scan].getNum() < data[min].getNum()) min = scan; } temp = data[min]; data[min] = data[index]; data[index] = temp; move_count += 3; } } public static void insertionSort(Record[] data) { compare_count = 0; move_count = 0; for (int index = 1; index < data.length; index++) { int key = data[index].getNum(); int position = index; while (position > 0 && data[position - 1].getNum() > key) { compare_count++; data[position] = data[position - 1]; move_count++; position--; } data[position] = data[index]; move_count++; } } }
-
This doesn't make sense as this error can't be thrown at compile time. Can you print the exact error message?
- 10-12-2009, 11:23 PM #10
Member
- Join Date
- Mar 2009
- Posts
- 17
- Rep Power
- 0
Oh nevermind. I just needed a throws clause. Oops! :)
- 10-12-2009, 11:24 PM #11
Member
- Join Date
- Mar 2009
- Posts
- 17
- Rep Power
- 0
Similar Threads
-
Need help with scanner.
By mainy in forum New To JavaReplies: 3Last Post: 07-28-2009, 02:11 PM -
how to use Scanner with a number
By cew27 in forum New To JavaReplies: 10Last Post: 04-03-2009, 06:23 PM -
Scanner
By choko in forum New To JavaReplies: 10Last Post: 01-24-2009, 03:37 PM -
need help with scanner
By whiterex in forum New To JavaReplies: 1Last Post: 04-22-2008, 01:41 PM -
help with IP scanner
By tommy in forum New To JavaReplies: 1Last Post: 08-06-2007, 08:00 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks