| Author |
Comparing = sign and [
|
Jesse Kelm
Greenhorn
Joined: Sep 06, 2011
Posts: 11
|
|
Obviously a beginner and can't seem to find an answer to my question so here goes:
I am importing data from a file line by line and need to test for the [ character and the = character, but when I try, Java terminates. The data is already stored as a String named input and the input looks like [20110906123456]. Here is what my code looks like:
Later on I will need to do the same sort of test with the = sign...
Thanks in advance for any assistance you can provide!!
|
 |
fred rosenberger
lowercase baba
Bartender
Joined: Oct 02, 2003
Posts: 9939
|
|
|
When it terminates, do you get an error message? Those actually give you a HUGE hint as to what the problem is. You did not give us enough code to try and run this, so nobody can really make suggestions - only wild guesses.
|
Never ascribe to malice that which can be adequately explained by stupidity.
|
 |
Ted Smyth
Ranch Hand
Joined: May 28, 2008
Posts: 73
|
|
fred rosenberger wrote:When it terminates, do you get an error message? Those actually give you a HUGE hint as to what the problem is. You did not give us enough code to try and run this, so nobody can really make suggestions - only wild guesses.
Indeed, post some sample contents (or the entire file content if you can) of a line in the file you are reading.
If Java is terminating, there should be a stacktrace which tells you what went wrong, and where.
|
Edward Smith
|
 |
Jesse Kelm
Greenhorn
Joined: Sep 06, 2011
Posts: 11
|
|
Sorry bout that...I don't get any messages, it just terminates. I am using a class named TextIO by David Eck for the input, output. I am sure there is a much better was of doing this, but again I am just learning. Everything works fine if I remove the comparing of the [ sign. Here is the code but I can't attach the .java class file:
public class Report {
public static void main(String[] args) {
String input = "";
String requestNumber = "";
TextIO.readFile("d:\\reports\\test.tmp");
while (!TextIO.eof()) {
input = TextIO.getlnString();
char ch;
int i;
input = (input.toUpperCase());
for (i = 0; i < input.length(); i++) {
ch = input.charAt(i);
if (ch == '[' ) { This line creates the issue:
if (ch >= '0' && ch <= '9') {
requestNumber = requestNumber + input.charAt(i);
}
}
}
}
}
}
|
 |
Martin Vajsar
Bartender
Joined: Aug 22, 2010
Posts: 2318
|
|
The problem lies here:
The line containing the requestNumber update is never executed. If the first condition (ch == '[') is met, the second condition will never be. Single char variable cannot be equal to the opening bracket and at the same time have a value between 0 and 9. You probably want to test the character that follows after the current character, ie. input.charAt(i+1), in the inner if statement. However, keep in mind that if the opening bracket is the last character in the string, accessing the following character will cause an exception.
|
 |
Ted Smyth
Ranch Hand
Joined: May 28, 2008
Posts: 73
|
|
Hey Jesse, try putting the [ code ] and [ /code ] tags around your code to format it nicely, like so:
And can you post the source code for TextIO? Without it we can't really tell if the file is even being read properly. To start, before you start parsing the file contents, just print out the "input" string, to see what was read from the file:
|
 |
Jesse Kelm
Greenhorn
Joined: Sep 06, 2011
Posts: 11
|
|
|
The more I look at it, I don't think the program is terminating, but not recognizing the compare of ch == '['. When outputting the value of ch to the screen, it is correct. Do you need to do a different sort of compare for reserved characters like [ and =?
|
 |
Greg Brannon
Bartender
Joined: Oct 24, 2010
Posts: 530
|
|
|
I think you're beginning to ask yourself the same question, but what makes you think the program is terminating abnormally? What do you think it should be doing that it's not? I think the program could be executing to completion and exiting normally.
|
Learning Java using Eclipse on OpenSUSE 11.2
Linux user#: 501795
|
 |
fred rosenberger
lowercase baba
Bartender
Joined: Oct 02, 2003
Posts: 9939
|
|
One of the most powerful tools you have is this:
If you want to know what your program is doing, sprinkle dozens of those in your code. If i had NO idea what this was doing, i'd do something like this:
When you run it, you'll get a lot of output, but you can start seeing the exact flow through your code. You can see if it is doing what you think it should (i.e. how many times is the while loop running? How many times is the for-loop running on each iteration? etc.
If you run this code, you will probably find out you never go into the inner if-statement, as several folks have pointed out...
|
 |
 |
|
|
subject: Comparing = sign and [
|
|
|