Reputation: 47
In a nutshell, here's what I'm trying to do:
I want to read a file and detect if the character after the symbol is a number or a word. If it is a number, I want to delete the symbol in front of it, translate the number into binary and replace it in the file. If it is a word, I want to set the characters to number 16 at first, but then, if another word is used, I want to add the 1 to the original number and continue looping till it reaches the end of the input.
When I hard code the string that I want to input and read:
String input = "@5\n@word1\n@word2\n@word1\n@6";
String[] lines = input.split("\n"); // divide up the array
Or:
@5
@word1
@word2
@word1
@6
Then it outputs what I want it to output:
101
10000
10001
10000
110
But when I input anyLines[i] (an array that contains the file info that can change if another file is chosen):
String input = anyLines[i];
String[] lines = input.split("\n");
For the same data, suddenly it gives an incorrect output:
101
10000
10000 <-- PROBLEM - should be 10001
10000
110
Now the problem with this is that the wordValue doesn't increment. In the hard-coded string, wordValue incremented correctly.
Here's my overall method:
try {
ReadFile files = new ReadFile(file.getPath());
String[] anyLines = files.OpenFile();
int i;
// test if the program actually read the file
for (i=0; i<anyLines.length; i++) {
String input = anyLines[i];
String[] lines = input.split("\n");
int wordValue = 16; // to keep track words that are already used
Map<String, Integer> wordValueMap = new HashMap<String, Integer>();
for (String line : lines) {
// if line doesn't begin with "@", then ignore it
if ( ! line.startsWith("@")) {
continue;
}
// remove &
line = line.substring(1);
Integer binaryValue = null;
if (line.matches("\\d+")) {
binaryValue = Integer.parseInt(line);
}
else if (line.matches("\\w+")) {
binaryValue = wordValueMap.get(line);
// if the map doesn't contain the word value,
// then assign and store it
if (binaryValue == null) {
binaryValue = wordValue;
wordValueMap.put(line, binaryValue);
wordValue++;
}
}
// I'm using Commons Lang's
// StringUtils.leftPad(..) to create the zero padded string
System.out.println(Integer.toBinaryString(binaryValue));
}
}
}
Can you point me in the right direction please?
Upvotes: 1
Views: 2529
Reputation: 881653
The code looks okay at first glance. Your best bet is to probably print out the lines as you process them to see if they're of a ... strange ... format:
for (String line : lines)
System.out.println ("[" + line + "]");
In fact, I'd go all out, and put a print statement after every line that changes something (which prints a sequence number and the changed thing), so as to be sure there are no unintended effects:
Something like:
else if (line.matches("\\w+")) {
binaryValue = wordValueMap.get(line);
System.out.println ("A: binval set to " + binaryValue);
// if the map doesn't contain the word value,
// then assign and store it
if (binaryValue == null) {
binaryValue = wordValue;
System.out.println ("B: binval set to " + binaryValue);
wordValueMap.put(line, binaryValue);
System.out.println ("C: put " + binaryValue +
", now " + wordValueMap.get(line));
wordValue++;
System.out.println ("C: wordval set to " + wordValue);
}
}
This printf
method of debugging can often be invaluable though you could also chose to use a debugging tool :-)
Upvotes: 1