Results 1 to 4 of 4
Thread: help debugging
- 08-21-2010, 05:30 AM #1
Member
- Join Date
- Aug 2010
- Posts
- 1
- Rep Power
- 0
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 } }
- 08-21-2010, 06:54 AM #2
"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. ;)
- 08-21-2010, 09:13 AM #3
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.
- 08-21-2010, 01:28 PM #4
To debug your code you need to add more println()s to show how the execution flow goes and how the variable values change.please help me debug it.
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
-
Debugging
By daro in forum EclipseReplies: 0Last Post: 07-22-2009, 05:02 PM -
Debugging with Eclipse 3.4.0
By edcaru in forum New To JavaReplies: 2Last Post: 11-07-2008, 06:27 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks