Results 1 to 3 of 3
  1. #1
    Josh.Hoekstra is offline Member
    Join Date
    Jun 2008
    Posts
    2
    Rep Power
    0

    Smile [SOLVED] Simple Scanner Method - Plus Sign throwing me off...

    Hello friends, this should be an easy fix, I hope:

    I have a method which uses a scanner to read an .ini file. The .ini file looks like this:

    **Product_B Path
    //server/folder/folder/ProductB

    **Product_B+ Path
    //server/folder/folder/ProductB+

    **Product_C Path
    //server/folder/folder/ProductC

    My method reads through the file using a scanner, looking to see if the variable productType (Product_B, Product_B+, or Product_C) matches any of the lines using scanner.findInLine(). If it does, then the method returns the following line (the path).

    Here is my code:

    public String getPath (String productType) {
    String file = "//server/folder/folder/file.ini";
    try {
    Scanner scanner = new Scanner(new File(file));
    while(scanner.findInLine("" + productType)==null) {
    if(!scanner.hasNextLine()) {
    scanner.close();
    return null;
    }
    scanner.nextLine();
    }
    //productType has been found, now return the directory path
    scanner.nextLine();
    return scanner.nextLine();
    }
    catch (FileNotFoundException err) {
    err.printStackTrace();
    }
    return null;
    }


    So here is my problem: when I send the method "Product_B" or "Product_C", it works as it should, returning the correct directory. However, whenever I send the method "Product_B+" it returns the Product_B directory. I think the plus sign is messing me up somehow, but I don't know why. If I change the "+" to something else like a "P" then it works fine.

    Can anyone offer help? Thanks very much. :)

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    10

    Default

    Check the Method Detail for the findInLine(String pattern) method to see that the method ignores delimiters. The "+" character might be counted as a delimiter.
    This seems to work okay.
    Java Code:
    import java.io.*;
    import java.util.Scanner;
    
    public class ScanningTest {
        public static void main(String[] args) {
            String[] types = {
                "Product_B Path",
                "Product_B+ Path",
                "Product_C Path"
            };
            for(int i = 0; i < types.length; i++) {
                String path = getPath(types[i]);
                System.out.printf("path for %s = %s%n", types[i], path);
            }
        }
    
        private static String getPath (String productType) {
            String filePath = "scanning.txt";
            String retVal = null;
            try {
                Scanner scanner = new Scanner(new File(filePath));
                while(scanner.hasNextLine()) {
                    String line = scanner.nextLine();
                    if(line.indexOf(productType) != -1) {
                        retVal = scanner.nextLine();
                        break;
                    }
                }
                scanner.close();
            } catch (FileNotFoundException err) {
                err.printStackTrace();
            }
            return retVal;
        }
    }

  3. #3
    Josh.Hoekstra is offline Member
    Join Date
    Jun 2008
    Posts
    2
    Rep Power
    0

    Default

    hardwired,

    Thank you very much for the help, your suggestion works perfectly. Just FYI, I did make many attempts at changing the delimiter to no avail. The default delimiter given from scanner.delimiter() is: \p{javaWhitespace}+

    Thanks again.

Similar Threads

  1. Main method throwing specific Exception
    By bugger in forum New To Java
    Replies: 5
    Last Post: 05-13-2009, 02:34 PM
  2. Replies: 2
    Last Post: 05-04-2008, 05:27 PM
  3. Simple Method Question
    By Froz3n777 in forum New To Java
    Replies: 2
    Last Post: 02-13-2008, 03:39 AM
  4. throwing Exception
    By bugger in forum New To Java
    Replies: 3
    Last Post: 11-09-2007, 10:35 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
  •