Results 1 to 3 of 3
- 06-02-2008, 05:33 PM #1
Member
- Join Date
- Jun 2008
- Posts
- 2
- Rep Power
- 0
[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. :)
- 06-02-2008, 09:10 PM #2
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; } }
- 06-02-2008, 10:24 PM #3
Member
- Join Date
- Jun 2008
- Posts
- 2
- Rep Power
- 0
Similar Threads
-
Main method throwing specific Exception
By bugger in forum New To JavaReplies: 5Last Post: 05-13-2009, 02:34 PM -
Help with a very simple method for a very simple beginner.
By cakeman in forum New To JavaReplies: 2Last Post: 05-04-2008, 05:27 PM -
Simple Method Question
By Froz3n777 in forum New To JavaReplies: 2Last Post: 02-13-2008, 02:39 AM -
throwing Exception
By bugger in forum New To JavaReplies: 3Last Post: 11-09-2007, 09:35 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