d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Java Working With Trees
Add Reply New Topic New Poll
Member
Posts: 33,402
Joined: Feb 19 2006
Gold: 312.85
Oct 26 2014 03:29pm
so for this project I have to run the program through a command prompt and I'm getting some errors not sure how to run a debugger when going through command prompt.


im getting a null pointer exception at MorseCodeTree.createTree(MorseCodeTree.java:39) and at MorseCode.main(MorseCode.java:17)

MorseCodeTree is my code it can be altered.
MorseCode and BinaryTree(not posted but I can if needed) is instructors code and cannot.

Code

public class MorseCodeTree extends BinaryTree<String>
{

public MorseCodeTree()
{
//calls the 3-argument BinaryTree consutrctor
super("", null, null);
}

public void createTree(String fileName) throws FileNotFoundException
{
Scanner fileScan = new Scanner(new File(fileName));

BinaryTree.Node morseNode = root;
while(fileScan.hasNext())
{
String line = fileScan.nextLine();
Scanner morseScan = new Scanner(line);
String letter = morseScan.next();
String code = morseScan.next();

morseNode = root;
for(int i=0; i < code.length() - 1; i++)
{
if(code.charAt(i) == '.')
{
morseNode = morseNode.left;
morseNode.left = new BinaryTree.Node(letter);
}
else if(code.charAt(i) == '-')
{
morseNode = morseNode.right;
morseNode.right = new BinaryTree.Node(letter);
}
else
{
throw new RuntimeException("Invalid Morse code: " + code);
}
}
}
}

public String encodeMessage(String message)
{
StringBuilder strBuilder = new StringBuilder();
for(int i = 0;i < message.length(); i++)
{
strBuilder.append(encodeLetter(message.substring(i, i + 1), root, "") + " ");
}
return strBuilder.toString();
}

private String encodeLetter(String letter, BinaryTree.Node<String> morseNode, String code)
{
String retStr = null;

if(morseNode == null)
{
return null;
}

if(((String)morseNode.data).equals(letter))
{
retStr = letter;
}

if(retStr == null && morseNode.left != null)
{
retStr = encodeLetter(letter, morseNode.left, ".");
}

if(retStr == null && morseNode.right != null)
{
retStr = encodeLetter(letter, morseNode.right, "-");
}
return retStr;
}

public String decodeMessage(String message)
{
StringBuilder strBuilder = new StringBuilder();

Scanner messageScanner = new Scanner(message);

BinaryTree.Node morseNode = root;
while(messageScanner.hasNext())
{
String scannedMessage = messageScanner.next();
if(scannedMessage.equals("null"))
{
strBuilder.append(" ");
}
else
{
morseNode = root;
for(int i = 0; i < scannedMessage.length(); i++)
{
if(scannedMessage.charAt(i) == '.')
{
morseNode = morseNode.left;
}
else if(scannedMessage.charAt(i) == '-')
{
morseNode = morseNode.right;
}
else
{
throw new RuntimeException("Invalid Morse code: " + scannedMessage);
}
}
strBuilder.append((String)morseNode.data);
}
}
return strBuilder.toString();
}
}


Code

public class MorseCode
{
public static void main(String[] args) throws FileNotFoundException
{
if(args.length != 3 || !(args[0].equalsIgnoreCase("encode") || args[0].equalsIgnoreCase("decode")))
{
System.out.println("usage: java MorseCode encodeOrDecode inputFileName outputFileName");
return;
}

MorseCodeTree tree = new MorseCodeTree();
tree.createTree("morsecode.txt");
Scanner in = new Scanner(new File(args[1]));
PrintWriter out = new PrintWriter(args[2]);

if(args[0].equalsIgnoreCase("encode"))
{
while(in.hasNextLine())
{
out.println(tree.encodeMessage(in.nextLine()));
}
out.close();
System.out.println(args[1] + " encoded into " + args[2]);
}
else
{
while(in.hasNextLine())
{
out.println(tree.decodeMessage(in.nextLine()));
}
out.close();
System.out.println(args[1] + " decoded into " + args[2]);
}
}
}
Member
Posts: 33,402
Joined: Feb 19 2006
Gold: 312.85
Oct 26 2014 03:32pm
the command to run in prompt is
java MorseCode encodeOrDecode inputFileName outputFileName

we were given text files marycode.txt, marytext.txt, and morsecode.txt

marycode.txt
Code

-- .- .-. -.-- null .... .- -.. null .- null .-.. .. - - .-.. . null .-.. .- -- -...
.. - ... null ..-. .-.. . . -.-. . null .-- .- ... null .-- .... .. - . null .- ... null ... -. --- .--
.- -. -.. null . ...- . .-. -.-- .-- .... . .-. . null - .... .- - null -- .- .-. -.-- null .-- . -. -
- .... . null .-.. .- -- -... null .-- .- ... null ... ..- .-. . null - --- null --. ---
.- -... -.-. -.. . ..-. --. .... .. .--- -.- .-.. -- -. --- .--. --.- .-. ... - ..- ...- .-- -..- -.-- --..



marytext.txt
Code

mary had a little lamb
its fleece was white as snow
and everywhere that mary went
the lamb was sure to go
abcdefghijklmnopqrstuvwxyz


morsecode.txt
Code

e .
t -
i ..
a .-
n -.
m --
s ...
u ..-
r .-.
w .--
d -..
k -.-
g --.
o ---
h ....
v ...-
f ..-.
l .-..
p .--.
j .---
b -...
x -..-
c -.-.
y -.--
z --..
q --.-
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Oct 26 2014 03:34pm
Quote (pyromaniac09 @ Oct 26 2014 05:29pm)
so for this project I have to run the program through a command prompt and I'm getting some errors not sure how to run a debugger when going through command prompt.


im getting a null pointer exception at MorseCodeTree.createTree(MorseCodeTree.java:39) and at MorseCode.main(MorseCode.java:17)

MorseCodeTree is my code it can be altered.
MorseCode and BinaryTree(not posted but I can if needed) is instructors code and cannot.



just use your favourite IDE to debug. you dont have to use the console for your own development. just make sure the console works when you turn it in.
Member
Posts: 33,402
Joined: Feb 19 2006
Gold: 312.85
Oct 26 2014 03:38pm
Quote (carteblanche @ Oct 26 2014 05:34pm)
just use your favourite IDE to debug. you dont have to use the console for your own development. just make sure the console works when you turn it in.


well i use bluej but i dont know how to debug it when you have to give the params through command prompt
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Oct 26 2014 03:39pm
Quote (pyromaniac09 @ Oct 26 2014 05:38pm)
well i use bluej but i dont know how to debug it when you have to give the params through command prompt


just modify your main to hard code the parameters then. skip the command prompt when you're debugging.

eg:
args[0] = "encode";
args[1] = inputFileName;
args[2] = outputFileName;

This post was edited by carteblanche on Oct 26 2014 03:40pm
Member
Posts: 33,402
Joined: Feb 19 2006
Gold: 312.85
Oct 26 2014 04:14pm
Quote (carteblanche @ Oct 26 2014 05:39pm)
just modify your main to hard code the parameters then. skip the command prompt when you're debugging.

eg:
args[0] = "encode";
args[1] = inputFileName;
args[2] = outputFileName;


ah i see thanks
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll