2 Replies - 10397 Views - Last Post: 11 December 2008 - 08:43 PM Rate Topic: -----

#1 ykelpsis89  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 20-November 08

Counting total of characters from a file

Post icon  Posted 11 December 2008 - 08:29 PM

My problem is in the numCharacters(). I need to get the total sum of all the characters in the file imported, but I am not sure what I am doing wrong.



import java.io.*;
import java.util.*;

public class WordList {

	// instance variables
	private String[] list;
	private int count;

	/**
	 *	Constructs an empty list with the specified initial capacity
	 *
	 *	@param int capacity - the initial capacity of the list
	 */
	public WordList(int capacity) {

		list = new String[capacity];
		count = 0;
	}


	/**
	 *	prompts user for filename, opens the specified file, and builds the list 
	 *	of Words from the file.
	 *
	 *	TO BE COMPLETED BY THE STUDENT:
	 */
	public void buildList() throws IOException {

		// build an object for keyboard input
		BufferedReader keyboard = 
				 new BufferedReader(new InputStreamReader(System.in));

		// prompt for file name
		System.out.println();
		System.out.print("Enter the name of the file to open: ");
		String filename = keyboard.readLine();

		// build an object for file input
		BufferedReader file = null;
		try {
			file = new BufferedReader(new FileReader(new File(filename)));
		} catch (FileNotFoundException e) {	// check for valid filename
			System.out.println("File not found.");
			System.exit(-1);	// exit if invalid filename
		}

		// read the file
		String line;
		while ((line = file.readLine()) != null) {
			
			String[] words = line.split("[ .,:;?!]");
			for (int i = 0; i < words.length; i++) {

				String word = words[i];
				word = word.toLowerCase();
				if (!inList(word) && word.length()>3)
					add(word); 
			//*************************************************
			// ADD CODE TO COMPLETE METHOD AFTER THIS BLOCK
			//	a) convert word to all lower case characters
			//	b) only add to the list if the word is not already in the list
			//	c) only add to the list if the word length > 3
			//**************************************************



			}
		}
	}

	
//	Write other methods after this comment.  be sure to add JavaDoc comments
//	  at the beginning of each method. I've written comments and a stub
//	  for the first method as an example
	
	/**
	 *  appends valid words to the list, updates the list count, 
	 *   and resizes the list if necessary
	 * 
	 * @param String word - the word to be added to the list
	 */
	public void add(String word){
		if (count < capacity())
			list[count]= word;
			count ++;
				   
	}

	
	
	


//	The following methods are complete and should
//	be used when and if appropriate	

	/**
	 *	Displays all of the elements of the list to the monitor
	 */
	public String toString() {

		String str = "";

		for (int i = 0; i < count; i++) {

			str += list[i] + "\n";
		}

		return str;
	}

	/**
	 *	Returns the number of elements in this list. 
	 *
	 *	@return int - the number of elements in this list. 
	 */
	public int size() {

		return count;
	}

	/**
	 *	Tests if this list has no elements. 
	 *
	 *	@return boolean - true if this list has no elements; false otherwise. 
	 */
	public boolean isEmpty() {

		return count == 0;
	}

	/**
	 *	Returns the current number of slots available in this list. 
	 *
	 *	@return int - the current number of slots available in this list. 
	 */
	public int capacity() {

		return list.length;
	}

	/**
	 *	Tests if this list has has room for no more elements. 
	 *
	 *	@return boolean - true if this list has no available slots; false otherwise. 
	 */
	public boolean isFull() {

		return count == capacity();
	}

	/**
	 *	Increases the capacity of this WordList instance, if  necessary, to ensure  
	 *	that it can hold at least the double the current capacity of the list  
	 * 
	 */
	private void resize() {

		String[] temp = new String[capacity() * 2 + 1];

		for (int i = 0; i < size(); i++) {

			temp[i] = list[i];
		}

		list = temp;
	}

	private boolean inList(String word) {
		boolean found = false;
		for (int i = 0; i < count; i++) {
			if (list[i].equals(word)) {
				found = true;
			}
		}
		return found;
	}
//position index if other is in the list
	public int contains(String other) {
	int index = -1;
	for (int i = 0; i < size(); i++){
	 if (list[1].equals(other))
	  index = i;}
	return index;
	}
		
		
	
// sum of total of all characters 
  public int numCharacters() {
			for(int i= 0; i < list.length; i++){
			if(list.charAt(i));
			}
			 count++;
			
			}
					
  


Is This A Good Question/Topic? 0
  • +

Replies To: Counting total of characters from a file

#2 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Counting total of characters from a file

Posted 11 December 2008 - 08:38 PM

This is your function:
// sum of total of all characters 
  public int numCharacters() {
			for(int i= 0; i < list.length; i++){
			if(list.charAt(i));
			}
			 count++;
			
			}



What are you intending this if() statement to do?
if(list.charAt(i));

When will that evaluate to true?
When will that evaluate to false?

What is that semi-colon doing at the end?

This post has been edited by janotte: 11 December 2008 - 08:39 PM

Was This Post Helpful? 0
  • +
  • -

#3 BigAnt  Icon User is offline

  • May Your Swords Stay Sharp
  • member icon

Reputation: 101
  • View blog
  • Posts: 2,392
  • Joined: 16-August 08

Re: Counting total of characters from a file

Posted 11 December 2008 - 08:43 PM

Problem I see right away in:
// sum of total of all characters
  public int numCharacters() {
            for(int i= 0; i < list.length; i++){
            if(list.charAt(i));
            }
             count++;
           
            }



Is the the semicolon after the if will mean the if does nothing, and the ending brace for for is before the count is incremented. Anyways the code will not work as charAt returns a character and if needs to be a boolean.

Now if the list contains all of the words in the file then you can just sum up the lengths of the strings to get the total characters, but the file says that duplicate words are not added to the list, so using the list to get total characters in the File will not work.

You can get the total numbers of characters in the file by using scanner to read in each string in the file, leaving the default delimitter as whitespace. when you read in each string just sum up the length of the strings for the complete file to get number of characters.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1