Results 1 to 14 of 14
- 05-13-2010, 06:51 PM #1
Member
- Join Date
- May 2010
- Posts
- 6
- Rep Power
- 0
Strings made by scanner don't work with startsWith() method
Hi all,
For some reason the strings that the scanner is making based on the lines in a .txt file aren't being compared to a startsWith() method call like they should. The bolded if statement should evaluate to true, as far as I can tell, but it isn't. Can anyone help out with this? Thanks!
This is the first line in the file JavaOutput.txt.Java Code:File output = new File("JavaOutput.txt"); System.out.println(output.exists()); PrintWriter outputWriter = new PrintWriter(output); for (int i = 140, j = 0; i < 160; i++, j++) { if (((File) arraySchem.get(j)).exists()) { Scanner newScanner = new Scanner((File) arraySchem.get(j)); while (newScanner.hasNextLine()) { String s = newScanner.nextLine(); outputWriter.println(s); outputWriter.println(); [B]if (s.startsWith("***")) { System.out.println("if working"); }[/B] } newScanner.close(); }
*** Header Start ***ഀഊLast edited by Fubarable; 05-15-2010 at 03:12 AM. Reason: Moderator Edit: Code tags added
- 05-13-2010, 08:42 PM #2
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 4
Try s.trim().startsWith("blabla"), maybe there's a whitespace character at the start of the expression.
Ever seen a dog chase its tail? Now that's an infinite loop.
- 05-13-2010, 09:25 PM #3
Member
- Join Date
- May 2010
- Posts
- 6
- Rep Power
- 0
Yeah I thought that there might be white spaces too. No luck though. Thanks for the reply!
- 05-13-2010, 11:24 PM #4
Do some debugging by using println()s to show what is in the s String. Put delimiters in the output to show spaces. EG (">" + s + "<")
- 05-14-2010, 03:12 AM #5
Member
- Join Date
- May 2010
- Posts
- 6
- Rep Power
- 0
Here is a copy and paste of the beginning of the output file. The example was intended to trigger the first line. All strings (s) are separated by a new line separator (\n). This:
if (s.trim().startsWith("***")) {
System.out.println("if working");
}
now replaces the boolean from the original post. Nothing is printed to the console when the program runs.
*** Header Start ***ഀഊ
VersionPersist: 1ഀഊ
LevelName: Sessionഀഊ
LevelName: Blockഀഊ
LevelName: Trialഀഊ
LevelName: SubTrialഀഊ
LevelName: LogLevel5ഀഊ
LevelName: LogLevel6ഀഊ
LevelName: LogLevel7ഀഊ
LevelName: LogLevel8ഀഊ
LevelName: LogLevel9ഀഊ
LevelName: LogLevel10ഀഊ
Experiment: Gears (S)
- 05-14-2010, 03:56 PM #6
What is output if you add the the following printlln() after the line:
String s = newScanner.nextLine();
System.out.println("s=" + s); // show value of s
Also what about this statement: if (((File) arraySchem.get(j)).exists()) {
Add some debug statements to show what is returned by get().
You're assuming that the above if returns true.Last edited by Norm; 05-14-2010 at 04:10 PM.
- 05-14-2010, 10:17 PM #7
Member
- Join Date
- May 2010
- Posts
- 6
- Rep Power
- 0
Ok so here is the code. I attached the the output because I couldn't copy and paste it from the console (it would copy the 's=' part and not the rest of the line). The thing on screenshot was the output for the string I was trying to get with the if(s.trim()... statement (sorry for small size - only way I could get it to 19.5 kb). I changed it in various ways to try to match the string, but none worked (took out spaces, added tab at the beginning, added tab and space, etc...). When I printed the strings to an output file, those rectangle characters weren't present. The if(((File) arrayS... is evaluating to true. Thanks!
Moderator Edit: Code tags addedJava Code:for (int i = 140, j = 0; i < 160; i++, j++) { System.out.println(((File) arraySchem.get(j)).exists()); if (((File) arraySchem.get(j)).exists()) { Scanner newScanner = new Scanner((File) arraySchem.get(j)); while (newScanner.hasNextLine()) { String s = newScanner.nextLine(); System.out.println("s=" + s); if (s.trim().startsWith(" G e a r D i s p l a y")) { System.out.println("if working"); } } newScanner.close(); } }Last edited by Fubarable; 05-15-2010 at 03:11 AM. Reason: Moderator Edit: Code tags added
- 05-15-2010, 02:13 AM #8
Weird looking output. Can you look at the input file in a hex editor to see what's there?
Has the file been stored in Unicode vs ASCII?
Also print out the name of the file returned by arraySchem.get(j)
Whoops, missed that you'd changed the String in startsWith()Last edited by Norm; 05-15-2010 at 02:29 AM.
- 05-15-2010, 02:22 AM #9
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
-
Code tags added to OP's posts. OP, please read the link in my signature regarding code tags. Much luck!
- 05-15-2010, 11:17 PM #11
Member
- Join Date
- May 2010
- Posts
- 6
- Rep Power
- 0
Thanks fubarable.
I tested the code with a bunch of variations of the string in startsWith(). The one in my previous post was just a last, unlikely effort. The variation from the original post was the first one I tried.
I made a simpler version of the program that only extracts data from a single file (removing the necessity for the loops and ArrayList). It produces similar output. And the same logic error.
Interestingly, if I only have one asterisk in the startsWith() call, the if evaluates to true. The getName() method returned the correct file name.Java Code:public static void main(String[] args) throws FileNotFoundException { File file = new File("Gears (R)-145-1.txt"); System.out.println(file.getName()); System.out.println(file.exists()); Scanner scanner = new Scanner(file); while (scanner.hasNextLine()) { String s = scanner.nextLine(); System.out.println("s=" + s); if (s.trim().startsWith("**")) { System.out.println("if working"); } } }
The file is encoded unicode. This is the first line as analyzed by a hex editor and the corresponding text (copied and pasted from the original .txt file):
FF FE 2A 00 2A 00 2A 00 20 00 48 00 65 00 61 00
*** Header Start ***
I'm going to read up on hex code, and post this in the mean time. Thanks!
- 05-15-2010, 11:40 PM #12
It looks like your text file is in Unicode format.
How did you create it?
Open the file in Notepad and then do a Save As and check the Encoding: field at the bottom. It'll probably say Unicode.
Another clue is if the number of bytes in the file is 2X + 2 + nbr of lines the number of bytes of text displayed.
Change it to ANSI and save with new name and look at it with the Hex EditorLast edited by Norm; 05-15-2010 at 11:42 PM.
- 05-15-2010, 11:55 PM #13
Member
- Join Date
- May 2010
- Posts
- 6
- Rep Power
- 0
That's the solution! I changed the format to ANSI and the boolean works.
The original text was copied and pasted from the output of a program called e-Prime, which is used in the behavioral sciences for performing experiments and recording data.
Thanks Norm!
- 05-16-2010, 12:09 AM #14
Similar Threads
-
Problem with while loop and the scanner method
By hhh80p in forum New To JavaReplies: 2Last Post: 02-28-2010, 11:47 AM -
what made paintComponent() method to be called twice??
By Y. Progammer in forum New To JavaReplies: 5Last Post: 02-21-2010, 10:19 PM -
comparison of two (identical) strings doesn't work
By langaro in forum CLDC and MIDPReplies: 4Last Post: 10-02-2009, 04:02 PM -
[SOLVED] Simple Scanner Method - Plus Sign throwing me off...
By Josh.Hoekstra in forum New To JavaReplies: 2Last Post: 06-02-2008, 10:24 PM -
Using the scanner method
By silvia in forum Advanced JavaReplies: 1Last Post: 08-07-2007, 05:50 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks