Results 1 to 14 of 14
  1. #1
    samza is offline Member
    Join Date
    May 2010
    Posts
    6
    Rep Power
    0

    Default 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!


    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();
    			}
    This is the first line in the file JavaOutput.txt.

    *** Header Start ***਍਍ഀഊ
    Last edited by Fubarable; 05-15-2010 at 03:12 AM. Reason: Moderator Edit: Code tags added

  2. #2
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    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.

  3. #3
    samza is offline Member
    Join Date
    May 2010
    Posts
    6
    Rep Power
    0

    Default

    Yeah I thought that there might be white spaces too. No luck though. Thanks for the reply!

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,397
    Rep Power
    25

    Default

    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 + "<")

  5. #5
    samza is offline Member
    Join Date
    May 2010
    Posts
    6
    Rep Power
    0

    Default

    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)

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,397
    Rep Power
    25

    Default

    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.

  7. #7
    samza is offline Member
    Join Date
    May 2010
    Posts
    6
    Rep Power
    0

    Default

    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!

    Java 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();
    			}
    
    		}
    Moderator Edit: Code tags added
    Attached Thumbnails Attached Thumbnails Strings made by scanner don't work with startsWith() method-screenshot-java.bmp  
    Last edited by Fubarable; 05-15-2010 at 03:11 AM. Reason: Moderator Edit: Code tags added

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,397
    Rep Power
    25

    Default

    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.

  9. #9
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Quote Originally Posted by samza View Post
    if (s.trim().startsWith(" G e a r D i s p l a y")) {
    This will never return true, because the string you're matching with starts with a space and a tab, which trim() would strip out, if they were there.

    -Gary-

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  11. #11
    samza is offline Member
    Join Date
    May 2010
    Posts
    6
    Rep Power
    0

    Default

    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.

    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");
    			}
    		}
    	}
    Interestingly, if I only have one asterisk in the startsWith() call, the if evaluates to true. The getName() method returned the correct file name.

    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!

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,397
    Rep Power
    25

    Default

    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 Editor
    Last edited by Norm; 05-15-2010 at 11:42 PM.

  13. #13
    samza is offline Member
    Join Date
    May 2010
    Posts
    6
    Rep Power
    0

    Default

    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!

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,397
    Rep Power
    25

    Default

    Another thought, if the source of the files MUST write Unicode files, Java can read and convert them to the proper internal format. Many I/O methods have an encoding parameter.

Similar Threads

  1. Problem with while loop and the scanner method
    By hhh80p in forum New To Java
    Replies: 2
    Last Post: 02-28-2010, 11:47 AM
  2. what made paintComponent() method to be called twice??
    By Y. Progammer in forum New To Java
    Replies: 5
    Last Post: 02-21-2010, 10:19 PM
  3. comparison of two (identical) strings doesn't work
    By langaro in forum CLDC and MIDP
    Replies: 4
    Last Post: 10-02-2009, 04:02 PM
  4. Replies: 2
    Last Post: 06-02-2008, 10:24 PM
  5. Using the scanner method
    By silvia in forum Advanced Java
    Replies: 1
    Last Post: 08-07-2007, 05:50 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •