Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 14 of 14

Thread: wildcard delete

  1. #1
    Member
    Join Date
    Mar 2011
    Posts
    198
    My Mood
    Daring
    Thanks
    7
    Thanked 4 Times in 4 Posts

    Default wildcard delete

    Ok so a text file has data per row. But some of the data changes constantly but at least keeps 2 of the same characters within the split section.

    MC	Cnj	Jup 	(L)	Tr-Na	00:01am		        [B]14°Ar3'D 	13°Ar49'D[/B]      -3.3
    Asc	Sxt	Sun/Sat 	(E)	Tr-Na	00:02am		[B]26°Ge1'D	27°Le1'D[/B]        2.71
    Asc	Sqr	Sat 	(E)	Tr-Na	00:05am		        [B]27°Ge05'D	27°Vi45'D[/B]       -0.78
    Mon	Tri	Jup/Sat 	(L)	Tr-Na	00:0am		[B]06°Pi47'D	0°Cn47'D[/B]         -2.5
    Asc	Sxt	Sun/Sat 	(X)	Tr-Na	00:06am		[B]27°Ge1'D	27°Le1'D[/B]         -0.1
    MC	Opp	Nep 	(E)	Tr-Na	00:08am		        [B]6°Ar23'D	         6°Li53'D[/B]          -2.8

    That for example.. How do i delete those? Instead of doing scan.ReplaceAll();

    The things id like to remove have around them. at the end of each line.

    There must be a simpler way.
    Last edited by macko; November 1st, 2011 at 01:10 PM.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,449
    My Mood
    Hungover
    Thanks
    144
    Thanked 637 Times in 545 Posts

    Default Re: wildcard delete

    What's wrong with replaceAll?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Member
    Join Date
    Mar 2011
    Posts
    198
    My Mood
    Daring
    Thanks
    7
    Thanked 4 Times in 4 Posts

    Default Re: wildcard delete

    whee would i begin? "*D" wont seem to work

    unless its "\*D" ?

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,449
    My Mood
    Hungover
    Thanks
    144
    Thanked 637 Times in 545 Posts

    Default Re: wildcard delete

    Like I said before, I really highly recommend reading up on the Pattern class API, then throwing together a little tiny test program that tests one line at a time. That's exactly what I do, and if you're new to regular expressions, that's exactly what you should be doing too.

    Pattern (Java Platform SE 6)
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Member
    Join Date
    Mar 2011
    Posts
    198
    My Mood
    Daring
    Thanks
    7
    Thanked 4 Times in 4 Posts

    Default Re: wildcard delete

    Ok will take a look now. May as well have 4 hours to waste lol its 4am.. had to much coke cant sleep heh

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,449
    My Mood
    Hungover
    Thanks
    144
    Thanked 637 Times in 545 Posts

    Default Re: wildcard delete

    Oh my! Where in the world are you? Here it's only 2:30 PM!

    Here is my tiny class that I use for testing regular expressions, maybe it will help you get started:

    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
     
    public class RegularExpressionTest {
     
    	public static void main(String... args){
    		 Pattern p = Pattern.compile(".*cat.*");
    		 Matcher m = p.matcher("cat test");
    		 boolean b = m.matches();
     
    		 System.out.println(b);
    	}
    }

    That's really what I had in there, haha, don't judge me!
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    Member
    Join Date
    Mar 2011
    Posts
    198
    My Mood
    Daring
    Thanks
    7
    Thanked 4 Times in 4 Posts

    Default Re: wildcard delete

    hehe it's ok i doubt i could do a better example as im still learning the class API hehe, I'm in australia. Will let u kno once i figure it out

  8. #8
    Member
    Join Date
    Mar 2011
    Posts
    198
    My Mood
    Daring
    Thanks
    7
    Thanked 4 Times in 4 Posts

    Default Re: wildcard delete

    hmm even when using *.D.* it still doesn't want to remove 14°Ar3'D 13°Ar49'D from the file .

    FileReader file = new FileReader("AD Converter/temp/update.txt");
    	    Scanner scan = new Scanner(file);
     
     
    	    int i = 0;
     
    	    while (scan.hasNext()) {
    	    	data[i] = scan.nextLine();
    	    	if(data[i].contains("*.D.*")){
        			data[i] = data[i].replaceAll("*.D.*", "").replace("*.D.*", "");
        		}
    }

    and yes i had tried your examples and a few on the class API demo. I'm brain damaged lol

    Starting to wonder how i got credits and distinctions on my java assignments lol xD
    Last edited by macko; November 1st, 2011 at 01:43 PM.

  9. #9
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,449
    My Mood
    Hungover
    Thanks
    144
    Thanked 637 Times in 545 Posts

    Default Re: wildcard delete

    This is how I meant you to use the class I posted:

    public class RegularExpressionTest {
     
    	public static void main(String... args){
     
    		String test = "MC	Cnj	Jup 	(L)	Tr-Na	00:01am		        [B]14°Ar3'D 	13°Ar49'D[/B]      -3.3";
     
    		if(test.contains("*.D.*")){
    			System.out.println("replacing");
    			test = test.replaceAll("*.D.*", "").replace("*.D.*", "");
    		}
    		else{
    			System.out.println("Doesn't contain");
    		}
     
    		System.out.println(test);
    	}
    }

    Now you can work with a single line, and we can be talking about the same exact code. Does this do what you expect? Where does its behavior differ from what you expect?

    The API is your best friend: Java Platform SE 6

    Check out the String API, and the contains method. Does it take a String or a regular expression? Also, why are you calling .replaceAll().replace()? Isn't that a bit redundant?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  10. #10
    Member
    Join Date
    Mar 2011
    Posts
    198
    My Mood
    Daring
    Thanks
    7
    Thanked 4 Times in 4 Posts

    Default Re: wildcard delete

    Don't judge me i'm abit dodgy altho your String test is equal to my data[] array my data[] array is scanning per line which stores it in the String data[i] and then im checking if it contains *.D.*and if so replace. but the problem is it wont pick up the ' character.

    But apart from that ye iv'e tried what you've just said.

  11. #11
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,449
    My Mood
    Hungover
    Thanks
    144
    Thanked 637 Times in 545 Posts

    Default Re: wildcard delete

    Quote Originally Posted by macko View Post
    Don't judge me i'm abit dodgy altho your String test is equal to my data[] array my data[] array is scanning per line which stores it in the String data[i] and then im checking if it contains *.D.*and if so replace. but the problem is it wont pick up the ' character.

    But apart from that ye iv'e tried what you've just said.
    Yeah I know, what I've posted is just a simplified version of exactly what you're doing. It's doing the exact same thing, it's just easier to work with. Run my code (which is really just your code), and see where exactly it's going wrong. I don't think your first problem is that it won't pick up the ' character.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  12. The Following User Says Thank You to KevinWorkman For This Useful Post:

    macko (November 1st, 2011)

  13. #12
    Member
    Join Date
    Mar 2011
    Posts
    198
    My Mood
    Daring
    Thanks
    7
    Thanked 4 Times in 4 Posts

    Default Re: wildcard delete

    Ok ive removed the 'D from the end so far. looking through the API it stated a non word character = \w

    So hence i used replaceAll("\\WD", "");

    now the rest shall be easy since they are letters

  14. #13
    Member
    Join Date
    Mar 2011
    Posts
    198
    My Mood
    Daring
    Thanks
    7
    Thanked 4 Times in 4 Posts

    Default Re: wildcard delete

    tada problem solved.. hehe, the correct syntax was:

    if(data[i].contains("D")){
        data[i] = data[i].replaceAll(".......\\WD", "");
    }

    Although the "." should have picked up the ' character.. I guess now we know that \W is for the characters we cannot get to work with the . Anyway thanks for the help man.. i believe my clients program is almost complete. time to do a graph to finish it off (y) time to go pick up my $350 hahaha

    Noticed how i removed the second .Replace() :$ i figured it was pointless of course.

  15. #14
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,449
    My Mood
    Hungover
    Thanks
    144
    Thanked 637 Times in 545 Posts

    Default Re: wildcard delete

    Are you sure that's working? I think you're missing a couple points when it comes to regular expressions. When I put that into my test program, it takes this String:

    "MC	Cnj	Jup 	(L)	Tr-Na	00:01am		        [B]14°Ar3'D 	13°Ar49'D[/B]      -3.3"
    And outputs this:
    MC	Cnj	Jup 	(L)	Tr-Na	00:01am		        [B 	[/B]      -3.3

    That doesn't seem right.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. how to delete a JTextField?
    By A4Andy in forum AWT / Java Swing
    Replies: 10
    Last Post: August 31st, 2011, 11:33 AM
  2. Jar self delete on exit?
    By KiwiProg in forum Java Theory & Questions
    Replies: 1
    Last Post: December 19th, 2010, 01:54 AM
  3. Can I delete one of these two?
    By ice in forum Java IDEs
    Replies: 2
    Last Post: November 14th, 2010, 03:02 AM
  4. How to delete data from php through GUI in Java?
    By BluXit in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 26th, 2010, 09:41 AM
  5. [SOLVED] how to delete an item from a form
    By mahdi in forum Java ME (Mobile Edition)
    Replies: 3
    Last Post: August 30th, 2009, 01:06 PM