Results 1 to 17 of 17

Thread: Scanner Skip

  1. #1
    right2001 is offline Member
    Join Date
    Feb 2009
    Posts
    25
    Rep Power
    0

    Default Scanner Skip

    I'm trying to modify a text file which includes "<", without changing "<="

    Scanner scan= new Scanner(new BufferedReader(new FileReader("in.txt"))).skip("<=").useDelimiter("<" );

    but It doesn't work. Does anybody have idea about that?

    Thanks.

  2. #2
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    The skip(String) method does not ignore delimiters. Use the skip(Pattern) method instead.

  3. #3
    right2001 is offline Member
    Join Date
    Feb 2009
    Posts
    25
    Rep Power
    0

    Default

    Quote Originally Posted by toadaly View Post
    The skip(String) method does not ignore delimiters. Use the skip(Pattern) method instead.
    Actually I got java.util.NoSuchElementException in either case.

  4. #4
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    6

    Default

    i think toadaly was talking about useDelimiter(Pattern pattern)

  5. #5
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    826
    Rep Power
    6

    Default Regarding Scanner

    Somehow skip method is throwing "No such Element execption" .Please use the below code block which skips
    "<=" and give the rest of the content of the file for modification.

    Scanner scannerObj = new Scanner(new FileInputStream(new File("sample.txt")));
    scannerObj.useDelimiter(Pattern.compile("<="));
    while(scannerObj.hasNext())
    {
    System.out.println(scannerObj.next());
    }

  6. #6
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    6

    Default

    RamyaSivakanth ...
    right2001 want to use Delimiter "<", but not affect "<="...

    Pattern seems cant do that...

  7. #7
    Join Date
    Mar 2009
    Posts
    25
    Rep Power
    0

    Default

    How about using two loops? One that uses a delimiter on "<=" so you know where the "<=" is. Then another one for "<" so you can combine the string.

    e.g.

    Java Code:
    public static void main(String[] args) {
    	File f = new File("text.txt");
    	Scanner s;
    	String fstring = "";
    	try {
    		s = new Scanner(f).useDelimiter("<=");
    		String st = s.next();
    		while (s.hasNext()) {
    			Scanner tmp = new Scanner(st).useDelimiter("<");
    			while (tmp.hasNext()) {
    				fstring += tmp.next();
    			}
    			fstring += "<=";
    			st = s.next();
    		}
    		Scanner tmp = new Scanner(st).useDelimiter("<");
    		while (tmp.hasNext()) {
    			fstring += tmp.next();
    		}
    		System.out.println(fstring);
    	} catch (FileNotFoundException f) {
    		System.out.println(f.toString());
    	}
    }
    Contents of text.txt
    Hi! < This is a Text file.
    <= hopefully <this wasn't sk<ipped.

    output of program:
    Hi! This is a Text file.
    <= hopefully this wasn't skipped.
    Last edited by hawaiian robots; 04-16-2009 at 01:17 PM.

  8. #8
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    826
    Rep Power
    6

    Default Scanner

    Hi Hawaiian,
    Please go thru the code.I have used 2 loops.I dont think any other solution.

    import java.io.*;
    import java.util.*;
    import java.util.regex.*;

    class Sample
    {
    public static void main(String[] args) throws Exception
    {
    //in.txt file contains "<a<=b<c"
    Scanner scannerObj = new Scanner(new FileInputStream(new File("sample.txt")));
    //Removing the <=
    scannerObj.useDelimiter(Pattern.compile("<="));
    String inputValue ="";
    while(scannerObj.hasNext())
    {
    inputValue= inputValue+scannerObj.next();
    }
    System.out.println("inputValue "+ inputValue);
    //After removing the <= I got the inputValue as "<ab<c"

    //I have splitted the above value with "<"
    String finalValueArray[]= inputValue.split("<");
    String finalValue="";
    for (int i = 0 ; i < finalValueArray.length ; i++)
    {
    finalValue = finalValue+finalValueArray[i];

    }//for

    //Final value i got here is "abc"
    System.out.println(finalValue);
    }//main
    }//Sample

  9. #9
    Join Date
    Mar 2009
    Posts
    25
    Rep Power
    0

    Default

    First what are you trying to do? Do you want to retain the "<=" but leave the "<" ? Or just remove all of them all together.

    Because running your file through my code gave the output (as the OP wanted) as "a<=bc".

  10. #10
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    826
    Rep Power
    6

    Default Hi

    Hi,
    My input string is "<a<=b<c"
    1. Iam just forming a string by removing "<=
    2. Again forming another string by removing <
    3.finally iam printing the value as abc

    That is what he asked know?

  11. #11
    Join Date
    Mar 2009
    Posts
    25
    Rep Power
    0

    Default

    I'm trying to modify a text file which includes "<", without changing "<="
    That says (to me) that he wants to keep all the "<="'s but get rid of all the "<"'s. Hence the reason why he wanted to use the skip() method.

  12. #12
    right2001 is offline Member
    Join Date
    Feb 2009
    Posts
    25
    Rep Power
    0

    Default

    Yeah that's what I'm looking for. I think Hawaiian Code implements that. I'm gonna try that..

    Thanks to Hawaiian.

  13. #13
    right2001 is offline Member
    Join Date
    Feb 2009
    Posts
    25
    Rep Power
    0

    Default Explanation

    Hawaiian, could you explain your code a little bit? It looks intricate to me:confused:

    Quote Originally Posted by hawaiian robots View Post
    How about using two loops? One that uses a delimiter on "<=" so you know where the "<=" is. Then another one for "<" so you can combine the string.

    e.g.

    Java Code:
    public static void main(String[] args) {
    	File f = new File("text.txt");
    	Scanner s;
    	String fstring = "";
    	try {
    		s = new Scanner(f).useDelimiter("<=");
    		String st = s.next();
    		while (s.hasNext()) {
    			Scanner tmp = new Scanner(st).useDelimiter("<");
    			while (tmp.hasNext()) {
    				fstring += tmp.next();
    			}
    			fstring += "<=";
    			st = s.next();
    		}
    		Scanner tmp = new Scanner(st).useDelimiter("<");
    		while (tmp.hasNext()) {
    			fstring += tmp.next();
    		}
    		System.out.println(fstring);
    	} catch (FileNotFoundException f) {
    		System.out.println(f.toString());
    	}
    }
    Contents of text.txt
    Hi! < This is a Text file.
    <= hopefully <this wasn't sk<ipped.

    output of program:
    Hi! This is a Text file.
    <= hopefully this wasn't skipped.

  14. #14
    right2001 is offline Member
    Join Date
    Feb 2009
    Posts
    25
    Rep Power
    0

    Default

    Actually, what I'm trying to do is not to delete < operators, but to append comments before and after it without corrupting <=.

  15. #15
    right2001 is offline Member
    Join Date
    Feb 2009
    Posts
    25
    Rep Power
    0

    Default

    The appropriate way may be check the next character after < to see whether it's = or not, but as far as I know there is no nextChar command in JAVA, right?

  16. #16
    Join Date
    Mar 2009
    Posts
    25
    Rep Power
    0

    Default

    I commented my code a bit:

    Based on the contents being:
    Hi! < This is a Text file.
    <= hopefully <this wasn't sk<i<=pped.

    Java Code:
    public static void main(String[] args) {
    	File f = new File("text.txt");
    	Scanner s;
    	String fullstring = "";
    	try {
    		s = new Scanner(f).useDelimiter("<=");
    		/* The string has been broken up on "<=" 
    		 * So It will look like:
    		 * "Hi! < This is a Text file."
    		 * "hopefully <this wasn't sk<i"
    		 * "pped."
    		 */
    		String string1 = s.next();
    		/* Loop on the first scanner
    		 * This will stop looping right after we get the last element
    		 */
    		while (s.hasNext()) {
    			/* Create a new scanner based on the string from the first scanner
    			 * This new scanner will look like this for the first line:
    			 * "Hi! "
    			 * " This is a Text file."
    			 */
    			Scanner tmp = new Scanner(string1).useDelimiter("<");
    			// So we combine this string first:
    			while (tmp.hasNext()) {
    				fullstring += tmp.next();
    			}
    			// When we are done we add the "<=" to the string to the end
    			fullstring += "<=";
    			// grab the next element
    			string1 = s.next();
    		}
    		/* Because there will still be data left, we need to do this one more time
    		 * This is so we don't add an unwanted "<=" to the end
    		 */
    		Scanner tmp = new Scanner(string1).useDelimiter("<");
    		while (tmp.hasNext()) {
    			fullstring += tmp.next();
    		}
    		System.out.println(fullstring);
    	} catch (FileNotFoundException e) {
    		System.out.println(e.toString());
    	}
    }

  17. #17
    Join Date
    Mar 2009
    Posts
    25
    Rep Power
    0

    Default

    Quote Originally Posted by right2001 View Post
    The appropriate way may be check the next character after < to see whether it's = or not, but as far as I know there is no nextChar command in JAVA, right?
    String classes have a charAt(INDEX) function
    Java Code:
    public static void main(String[] args) {
       File f = new File("text.txt");
       Scanner s;
       String tmp = "";
       try {
          s = new Scanner(f);
          while (s.hasNextLine()) {
             tmp = s.nextLine();
             for (int i = 0; i < tmp.length(); i++) {
                if (i == tmp.length() - 1) {
                   /* last index of the string */
                } else {
                   if (tmp.charAt(i) == '<') {
                      if (tmp.charAt(i + 1) == '=') {
                         /* followed by a '=' */
                      } else {
                         /* not followed by '=' */
                      }
                   }
                }
             }
          }
       } catch (FileNotFoundException e) {
       }
    }

Similar Threads

  1. 2 errors with Scanner , help
    By enghar in forum New To Java
    Replies: 2
    Last Post: 01-24-2009, 09:34 PM
  2. Scanner
    By choko in forum New To Java
    Replies: 10
    Last Post: 01-24-2009, 03:37 PM
  3. need help with scanner
    By whiterex in forum New To Java
    Replies: 1
    Last Post: 04-22-2008, 01:41 PM
  4. Using ava.util.Scanner
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-20-2007, 04:47 PM
  5. help with IP scanner
    By tommy in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 08:00 PM

Posting Permissions

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