12 Replies Latest reply: Apr 24, 2008 1:32 AM by 800282 RSS

    Clear and understandable/Regular Exp / Indexof() Issue

    807591
      INPUT
      1234567891234560000000000000000000000000000000000098765432198765432100000000000000000000000000000000000

      OUTPUT
      45678912345600000000000000000000000000000000000
      32198765432100000000000000000000000000000000000

      Code


      import java.util.regex.*;
      import java.io.*;
      import java.nio.*;
      import java.nio.channels.*;
      import java.nio.charset.*;


      public class thirtyfivezeros
      {
      public static void main(String[] args) throws IOException
      {
           
           
           BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
      System.out.print("Enter file name for search: ");
      String filename = in.readLine();
      File file = new File(filename);
      if(!filename.endsWith(".txt")){
      System.out.println("This is not a text file.");
      System.out.println("Please enter file name withe \".txt\" extension.");
      System.exit(0);
      }
      else if(!file.exists()){
      System.out.println("File not found.");
      System.exit(0);
      }

      FileChannel fileChannel = new FileInputStream(filename).getChannel();
      CharBuffer charBuffer = Charset.forName("8859_1").newDecoder().decode(
      fileChannel.map(FileChannel.MapMode.READ_ONLY, 0, (int)fileChannel.size()));

      Pattern pattern = Pattern.compile("([0-9]{12})0{35}");
      Matcher matcher = pattern.matcher(charBuffer);

      int a = 0;
      while(matcher.find())
      {
           String matchStrings = matcher.group();
      FileWriter fstream = new FileWriter("c:/test.txt",true);
      BufferedWriter out = new BufferedWriter(fstream);
      out.write(matchStrings);
      out.newLine();
      System.out.println(matchStrings);
      a++;
      out.close();
      }
      System.out.println("Total occurance of 35 Consecutive zeros in the file is: " + a);
      }
      }


      DESIRED OUTPUT
      567 34 56 00000000000000000000000000000000000
      219 43 21 00000000000000000000000000000000000

      Ho do i do it. I think i am clear enough this time. Hope to get some help . you guys have been awesome
        • 1. Re: Clear and understandable/Regular Exp / Indexof() Issue
          796440
          You have provided the input and the desired output as examples, but not stated the rules. We can try to infer them, but you'll get better help if you spell them out. For instance, the leading part of your input has 123456 twice, and your desired output inludes some or all of those digits. How are we to know which "56" you mean in your desired output?

          Also, when you post code, use the CODE button or [code] and [/code] tags to make it readable.
          • 2. Re: Clear and understandable/Regular Exp / Indexof() Issue
            807591
            If you look at the Regular expression that i have in my code. It says. any 10 digits from 0 to 9, left of consecutive 35 zeros. Which means you are actually counting backwards to get those numbers . Form right to left, starting with first zero of consecutive 35 zeros. that 56 you are talking about is actually the one that is closest to 35 zeros. I hope this make sense
            • 3. Re: Clear and understandable/Regular Exp / Indexof() Issue
              800282
              String in = "1234567891234560000000000000000000000000000000000098765432198765432100000000000000000000000000000000000";
              Matcher m = Pattern.compile("(\\d{3})\\d{4}(\\d{2})(\\d{2})(0{35})").matcher(in);
              while(m.find()) {
                String temp = m.group(1)+" "+m.group(2)+" "+m.group(3)+" "+m.group(4);
                System.out.println(temp);
              }
              • 4. Re: Clear and understandable/Regular Exp / Indexof() Issue
                796440
                Hiklior wrote:
                If you look at the Regular expression that i have in my code.
                I'm not going to read somebody else's broken regex in unformatted code.

                Will the string always be exactly that many characters?

                Will the 35 zeros and their preceding characters always be in exactly the same spots?

                When one string has multiple occurrences of whatever your pattern is, do you one multiple seprate result strings?
                • 5. Re: Clear and understandable/Regular Exp / Indexof() Issue
                  807591
                  I dont see any difference its giving me the same output. And yes i will make sure i put that Code tag when post code next time.

                  Thank you
                  • 6. Re: Clear and understandable/Regular Exp / Indexof() Issue
                    807591
                    Sorry about that, I am not a guru like you i am a beginner. There will always be 35 zeros somewhere in the file and yes probably more than 100 times. But there will be atleast (I said atleast three could be more also )12 digits before each consecutive zeros. Let me explain you a little bit more.

                    Expected output (Example)
                    212 08 22 00000000000000000000000000000000000

                    222: is the machine number that generated 35 zeros
                    08 is the month when that machine generated those zeros
                    22 is the day when that machine produced those zeros.

                    All of these machine run 24-7 and they sometimes generate reports all the times. These reports will sometime contain these 35 consecutive zeros. Now i need a code that helps me figure out which machine is messing up and when.
                    • 7. Re: Clear and understandable/Regular Exp / Indexof() Issue
                      807591
                      i meant
                      212 is the machine number not 222.
                      • 8. Re: Clear and understandable/Regular Exp / Indexof() Issue
                        800282
                        Hiklior wrote:
                        I dont see any difference its giving me the same output. ...
                        Err, who are you addressing here? Me?
                        The code I posted gives the exact output from your original post under "desired output"!
                        • 9. Re: Clear and understandable/Regular Exp / Indexof() Issue
                          807591
                          Pro,

                          i dont have the input string in any variable. I am reading each line from the file.
                          • 10. Re: Clear and understandable/Regular Exp / Indexof() Issue
                            796440
                            Hiklior wrote:
                            Sorry about that, I am not a guru like you i am a beginner.
                            That's fine. Nobody's expecting you to be a guru. It's just that to help you, we need to have a clear understanding of your problem. Your main hurdle at the moment, at least with me, is communication, not Java.
                            There will always be 35 zeros somewhere in the file and yes probably more than 100 times. But there will be atleast (I said atleast three could be more also )12 digits before each consecutive zeros.
                            What about at most? Is there an upper bound?
                            Let me explain you a little bit more.
                            The meaning of the numbers doesn't really help. It's a clear explanation of what you're looking for and in what contexts it can occur that's needed. It looks like somebody smarter than I is doing a better job of understanding your requirements, so I'll leave you in his capable hands.

                            I'll just add that it looks like this is what you want:

                            "Every time there's an occurrence of 35 consecutive zeros, look at the 12 preceding digits, and get me the 5th-7th, the 9th-10th, and the 11th-12th." (Or something like that--I don't know exactly what the positions were.)

                            "For example if I have
                            111122234455 (followed by 35 zeroes)
                            then I want each of the following:
                            222
                            44
                            55"

                            The clear, precise explanation, along with a supporting example, would have made it much easier to understand.

                            Edited by: jverd on Apr 23, 2008 3:13 PM
                            • 11. Re: Clear and understandable/Regular Exp / Indexof() Issue
                              796440
                              Hiklior wrote:
                              Pro,

                              i dont have the input string in any variable. I am reading each line from the file.
                              So, a String that's read from a file can't go into a variable?
                              • 12. Re: Clear and understandable/Regular Exp / Indexof() Issue
                                800282
                                Hiklior wrote:
                                Pro,

                                i dont have the input string in any variable. I am reading each line from the file.
                                First read the entire file into a String, then use a Matcher (as shown) to find the parts you're interested in.