File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Comparing = sign and [

 
Jesse Kelm
Greenhorn
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 12022
25
Chrome Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ted Smyth
Ranch Hand
Posts: 73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jesse Kelm
Greenhorn
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 3747
62
Chrome Netbeans IDE Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 563
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12022
25
Chrome Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic