Results 1 to 4 of 4

Thread: help debugging

  1. #1
    mluu510 is offline Member
    Join Date
    Aug 2010
    Posts
    1
    Rep Power
    0

    Default help debugging

    i am taking the stanford cs106a programming methodology via itunes U. i need help writing an acronym program that takes the character of each word and inject it with a random words from a lexicon

    example: hello
    output: hat evil love like outfit

    i wrote the program in eclipse and it's not functioning. i know the code isn't very efficient but that's not the focus. i just want it to run. please help me debug it. thank you.

    i am not very good with using eclipse's debug so it wasn't very helpful to me.

    Java Code:
    import acm.program.*;
    import acm.util.*;
    import java.io.*;
    import java.util.*;
    
    public class AcronymEncoding extends ConsoleProgram {
    	
    	private RandomGenerator rgen = new RandomGenerator(); // Random Generator
    	private ArrayList<String> wordList = new ArrayList<String>(); // Lexicon array list
    	
    	public void run() {
    		while(true) {
    			String message = readLine("Enter a word to encode: "); // Prompt user for a acronym
    			if(message.equals("")) break;
    			println(encode(message)); // Print the encoded message
    		}
    	}
    	
    	/* Message Encoder */
    	private String encode(String message) {
    		message = message.toUpperCase(); // Convert input into upper case
    		String acronym = "";
    		
    		for(int i = 0; i < message.length(); i++) { // Go through each character in the message
    			char ch = message.charAt(i);
    			acronym += randomWordStartingWith(ch) + " "; // Build acronym with random words
    		}
    		return acronym;
    	}
    	
    	/* Random Word Generator */
    	private String randomWordStartingWith(char ch) {
    		String randomWord;
    		try {
    			BufferedReader rd = new BufferedReader(new FileReader("HangmanLexicon.txt")); // Read the lexicon
    			
    			while(rd.ready()) { // Begin reading file
    				String line = rd.readLine();
    				if(line.charAt(0) == ch) { // Add word to list if first letter matches
    					wordList.add(line);
    				} else {
    					break; // Stop reading once the first letter don't match
    				}
    			}
    			
    			rd.close(); // Close the lexicon
    			randomWord = wordList.get(rgen.nextInt(0, wordList.size() - 1)); // Randomly generate a word from the word list
    		} catch(IOException e) {
    			return "Error loading lexicon!"; // Print error if read is unsuccessful
    		}
    		return randomWord; // Return the random word to the encoder
    	}
    }

  2. #2
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    "Help me debug it" does not help us determine why it is or is not working. You need to be more specific: Are errors being generated? What is the output of the program, and what should it be? Have you tried adding some System.out.println commands to find the problems?

    Help us help you. ;)

  3. #3
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

    Default

    is the "HangmanLexicon.txt" available on the web for download or can somebody specify the structure this file must have?
    Last edited by j2me64; 08-21-2010 at 09:21 AM.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    please help me debug it.
    To debug your code you need to add more println()s to show how the execution flow goes and how the variable values change.

    Break this statement up into single steps and display the result of each step:
    randomWord = wordList.get(rgen.nextInt(0, wordList.size() - 1));

    Display the full contents of wordList at several places in the code.

Similar Threads

  1. Debugging
    By daro in forum Eclipse
    Replies: 0
    Last Post: 07-22-2009, 05:02 PM
  2. Debugging with Eclipse 3.4.0
    By edcaru in forum New To Java
    Replies: 2
    Last Post: 11-07-2008, 06:27 AM

Posting Permissions

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