Results 1 to 9 of 9
- 01-20-2012, 09:12 PM #1
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.
- 01-20-2012, 09:39 PM #2
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 bracketLast edited by Norm; 01-20-2012 at 10:47 PM. Reason: added code tags
- 01-20-2012, 10:50 PM #3
Re: Easy Question! Print out file, count number of lines, number of spaces w/ Scanner
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.problem is that the program is running infinitely
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.
- 01-21-2012, 12:19 AM #4
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?
- 01-21-2012, 12:23 AM #5
Re: Easy Question! Print out file, count number of lines, number of spaces w/ Scanner
How many times do you need to go around the loop?What bounds do I use in the for loop?
What is this line of code supposed to test for?
You are comparing the loop index against the value of a space????Java Code:if(i==' '){
- 01-21-2012, 12:37 AM #6
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...?
- 01-21-2012, 01:01 AM #7
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:
You need to work out this logic without worrying about reading lines from a file.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
- 01-21-2012, 01:12 AM #8
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.
- 01-21-2012, 01:14 AM #9
Similar Threads
-
day number count
By droidus in forum New To JavaReplies: 14Last Post: 03-23-2011, 10:15 PM -
Help w/ java programming assignment counting number of spaces
By clemsontigers in forum New To JavaReplies: 9Last Post: 03-06-2011, 03:00 AM -
Count number of integers left in a file, hard with rules allowed!
By YeeP in forum New To JavaReplies: 5Last Post: 12-06-2010, 12:43 AM -
Count number of digits in string using scanner
By wendysbiggy in forum New To JavaReplies: 35Last Post: 01-20-2010, 05:11 AM -
Issue With Finding Total Number of Blank Spaces in File
By Cod in forum New To JavaReplies: 2Last Post: 12-10-2009, 12:06 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks