Results 1 to 9 of 9
  1. #1
    Join Date
    Jan 2012
    Posts
    19
    Rep Power
    0

    Default Easy Question! Print out file, count number of lines, number of spaces w/ Scanner

    I am trying to read in a file line by line and count the number of spaces in each line.
    The prompt is:
    "Loop through each line to read the text document and print it out. Put in a counter to count the number of lines that the program read. Print out the number. Alter this program so that it tells you how many words are in each line. Hint: Store the line, loop through it looking for spaces, and print out the number of spaces."

    The problem is that the program is running infinitely and not displaying anything. This should be really simple but I'm making a stupid mistake. Does anyone have an idea where I'm going wrong? Thank you!
    Last edited by LogicalOutlier; 01-20-2012 at 09:39 PM.

  2. #2
    Join Date
    Jan 2012
    Posts
    19
    Rep Power
    0

    Default Re: Easy Question! Print out file, count number of lines, number of spaces w/ Scanner

    Java Code:
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.Scanner;
    
    public class Scanning {
    
    
    public static void main(String[] args) throws FileNotFoundException{
    	
    File file = new File("frost.txt");
    Scanner scan = new Scanner(file);
    int count = 0;
    int sCount = 0;
    	
    while(scan.hasNext()){
    	
    	while(scan.hasNextLine()){
    	
    		String lines = scan.nextLine();
    	    count++;
    	
    		while(scan.hasNext()){
    			char[]line = new char[lines.length()];
    			line = lines.toCharArray();
    			
    			for(int i = 0; i < lines.length(); i++){
    				
    				if(line[i]==' ')
    				{
    			    sCount++;
    				} //if bracket
    				
    			} //for bracket
    
            }//while'' bracket
    			
        } //while' bracket
    	
    System.out.println(scan.nextLine());
    System.out.println("Lines in the file: " + count);	
    System.out.println("Spaces in the file: "+ sCount);	
    
    }//while bracket
    
    } //main method bracket
    
    } //class bracket
    Last edited by Norm; 01-20-2012 at 10:47 PM. Reason: added code tags

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,415
    Rep Power
    25

    Default Re: Easy Question! Print out file, count number of lines, number of spaces w/ Scanner

    problem is that the program is running infinitely
    Add some printlns that print out the values of the variables that control the looping. The output should help you understand what the problem is.

    Also read the API doc for the Scanner class's hasNext methods. I think some of the methods may block waiting for input.

    Why do you have so many nested loops? Why not just one loop for reading lines from the file.
    The other loops for looking for the spaces in a String that was read from the file.
    Last edited by Norm; 01-20-2012 at 10:52 PM.

  4. #4
    Join Date
    Jan 2012
    Posts
    19
    Rep Power
    0

    Default Re: Easy Question! Print out file, count number of lines, number of spaces w/ Scanner

    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.Scanner;

    public class Scanning {


    public static void main(String[] args) throws FileNotFoundException{

    File file = new File("frost.txt");
    Scanner scan = new Scanner(file);
    int count = -1;
    int sCount = 0;
    int i = 0;

    while(scan.hasNextLine()){

    String lines = scan.nextLine();
    System.out.println(lines);
    count++;

    for (i = 0; i < lines.length(); i++){
    if(i==' '){
    sCount++;
    }

    }

    }

    System.out.println("\nLines in the file: " + count);
    System.out.println("\nSpaces in the file: "+ sCount);

    }

    }


    That helped, thanks!

    It's counting lines correctly now. But it's not counting the total number of spaces in the file because the for loop is from 0 to line.length()-1. It's currently returning 18 spaces.

    What bounds do I use in the for loop?

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,415
    Rep Power
    25

    Default Re: Easy Question! Print out file, count number of lines, number of spaces w/ Scanner

    What bounds do I use in the for loop?
    How many times do you need to go around the loop?

    What is this line of code supposed to test for?
    Java Code:
    if(i==' '){
    You are comparing the loop index against the value of a space????

  6. #6
    Join Date
    Jan 2012
    Posts
    19
    Rep Power
    0

    Default Re: Easy Question! Print out file, count number of lines, number of spaces w/ Scanner

    It needs to loop once for each line of the file and count the number of spaces in each line, then total that.
    Or it could just loop once and count the number of spaces for the whole file.

    if(i==' ') should test for the number of times a space appears in the file. I'm not sure how to represent the characters in the file though...?

  7. #7
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,415
    Rep Power
    25

    Default Re: Easy Question! Print out file, count number of lines, number of spaces w/ Scanner

    Forget about the reading lines from a file for now.
    Write a method:countSpaces() that will count the number of spaces in a String. Call that method with a short String with a few spaces.
    For example:
    Java Code:
    String data = "A line with 4 spaces";  // define a String
    int nbrSpaces = countSpaces(data);   // pass the String to the method to do the counting
    System.out.println("nbrSpaces=" + nbrSpaces); // show results
    You need to work out this logic without worrying about reading lines from a file.

  8. #8
    Join Date
    Jan 2012
    Posts
    19
    Rep Power
    0

    Default Re: Easy Question! Print out file, count number of lines, number of spaces w/ Scanner

    for(int i = 0; i < data.length(); i++){
    if(Character.isWhitespace(data.charAt(i))){
    spaces++;
    }

    This code segment works.

  9. #9
    Join Date
    Jan 2012
    Posts
    19
    Rep Power
    0

    Default Re: Easy Question! Print out file, count number of lines, number of spaces w/ Scanner

    It's working now, thank you! :)

Similar Threads

  1. day number count
    By droidus in forum New To Java
    Replies: 14
    Last Post: 03-23-2011, 10:15 PM
  2. Replies: 9
    Last Post: 03-06-2011, 03:00 AM
  3. Replies: 5
    Last Post: 12-06-2010, 12:43 AM
  4. Count number of digits in string using scanner
    By wendysbiggy in forum New To Java
    Replies: 35
    Last Post: 01-20-2010, 05:11 AM
  5. Replies: 2
    Last Post: 12-10-2009, 12:06 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •