Results 1 to 11 of 11
  1. #1
    jtmoney0511 is offline Member
    Join Date
    Mar 2009
    Posts
    17
    Rep Power
    0

    Default 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

    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++;
    Here is everything in the text file I'm scanning:

    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 09:43 PM. Reason: code tags added for readability

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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

  3. #3
    jtmoney0511 is offline Member
    Join Date
    Mar 2009
    Posts
    17
    Rep Power
    0

    Default

    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++;

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Java Code:
    while (file_scan.hasNextLine())
      lines++;
    This never gets a nextLine and so will never progress and will loop forever.

    If you just want to count lines, at least do this:
    Java Code:
    while (file_scan.hasNextLine()) {
      lines++;
      file_scan.nextLine();  // get the line and discard it.
    }
    Also, always enclose loop blocks and control blocks in curly braces. This will help when you want to add more code.

  5. #5
    jtmoney0511 is offline Member
    Join Date
    Mar 2009
    Posts
    17
    Rep Power
    0

    Default

    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!!

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  7. #7
    jtmoney0511 is offline Member
    Join Date
    Mar 2009
    Posts
    17
    Rep Power
    0

    Default

    I see. That makes a lot of sense.

  8. #8
    jtmoney0511 is offline Member
    Join Date
    Mar 2009
    Posts
    17
    Rep Power
    0

    Default

    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++;
    	 }	  
      
      }
    }

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    This doesn't make sense as this error can't be thrown at compile time. Can you print the exact error message?

  10. #10
    jtmoney0511 is offline Member
    Join Date
    Mar 2009
    Posts
    17
    Rep Power
    0

    Default

    Oh nevermind. I just needed a throws clause. Oops! :)

  11. #11
    jtmoney0511 is offline Member
    Join Date
    Mar 2009
    Posts
    17
    Rep Power
    0

    Default

    By the way, the error said:

    RecordSorter.java:19: unreported exception java.io.FileNotFoundException; must be caught or declared to be thrown
    file_scan = new Scanner(new File(filename));

Similar Threads

  1. Need help with scanner.
    By mainy in forum New To Java
    Replies: 3
    Last Post: 07-28-2009, 03:11 PM
  2. how to use Scanner with a number
    By cew27 in forum New To Java
    Replies: 10
    Last Post: 04-03-2009, 07:23 PM
  3. Scanner
    By choko in forum New To Java
    Replies: 10
    Last Post: 01-24-2009, 04:37 PM
  4. need help with scanner
    By whiterex in forum New To Java
    Replies: 1
    Last Post: 04-22-2008, 02:41 PM
  5. help with IP scanner
    By tommy in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 09:00 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
  •