By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,797 Members | 1,336 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,797 IT Pros & Developers. It's quick & easy.

How to Display the content of a substring

P: 11
Dear Friends,
I have two textfiles ,
Ex:first text is like this
AAA A B C D
..................
....................
ZZZ CC GG
Second text file is
A a
B b
C c
........
........
GG g
Like this

I have to print the content of AAA as A B C D and at the same time i have to ptint abcd in one line as the second output.

Now i already done the first step ie i print A B C D.Now i have the doubt is how can i print the abcd .Iam attaching my second part code with this .This is the code from aftr getting the first output
Expand|Select|Wrap|Line Numbers
  1. import java.io.*; 
  2. import java.util.regex.*;
  3. class A
  4. {
  5.   public static void main(String args[])
  6.   {
  7.         DataInputStream di = null; 
  8.         String rec=null;
  9.          int recCount = 0; 
  10.            try
  11.             { 
  12.               File s = new File("D:/s/pr/cmu_tam_map.txt"); 
  13.               FileInputStream fi = new FileInputStream(s); 
  14.                BufferedInputStream bi = new BufferedInputStream(fi); 
  15.                di = new DataInputStream(bi);  
  16.                       String out;
  17.               String ou;
  18.                       String ouput ="K AH0 N T IH1 N Y UW0";
  19.                        String arr[]={};
  20.                         arr=ouput.split("\\s");
  21.  while((rec=di.readLine()) != null)
  22.                         {
  23.                            for(int i=0;i<8;i++)
  24.                            {
  25.  
  26.                              //System.out.println(arr[i]);
  27.                              if(rec.startsWith(arr[i]))
  28.                              {
  29.                                  int len = rec.length();
  30.                                  out = rec.substring(arr[i].length(),len);
  31.                                  // System.out.print(out);//continue;
  32.                              // String ne=out;
  33.                               System.out.print(out);
  34.                              } 
  35.                              }
  36.  
  37.                    }
  38.  
  39.                     recCount++; 
  40.  
  41.              } 
  42.  
  43.              catch (IOException e)
  44.              { 
  45.               System.out.println("Uh oh, got an IOException error!" + e.getMessage()); 
  46.               } 
  47.  
  48.                finally 
  49.                   { 
  50.                     if (di != null)
  51.                        { 
  52.                           try 
  53.                             {
  54.                              di.close(); 
  55.                             }
  56.                          catch (IOException ioe)
  57.                             {
  58.                             }
  59.                         } 
  60.                   }
  61.  
  62.   }
  63.  
  64. }
  65.  
Jan 28 '07 #1
Share this Question
Share on Google+
1 Reply


10K+
P: 13,264
Dear Friends,
I have two textfiles ,
Ex:first text is like this
AAA A B C D
..................
....................
ZZZ CC GG
Second text file is
A a
B b
C c
........
........
GG g
Like this

I have to print the content of AAA as A B C D and at the same time i have to ptint abcd in one line as the second output.

Now i already done the first step ie i print A B C D.Now i have the doubt is how can i print the abcd .Iam attaching my second part code with this .This is the code from aftr getting the first output
Expand|Select|Wrap|Line Numbers
  1. import java.io.*; 
  2. import java.util.regex.*;
  3. class A
  4. {
  5.   public static void main(String args[])
  6.   {
  7.         DataInputStream di = null; 
  8.         String rec=null;
  9.          int recCount = 0; 
  10.            try
  11.             { 
  12.               File s = new File("D:/s/pr/cmu_tam_map.txt"); 
  13.               FileInputStream fi = new FileInputStream(s); 
  14.                BufferedInputStream bi = new BufferedInputStream(fi); 
  15.                di = new DataInputStream(bi);  
  16.                       String out;
  17.               String ou;
  18.                       String ouput ="K AH0 N T IH1 N Y UW0";
  19.                        String arr[]={};
  20.                         arr=ouput.split("\\s");
  21.  while((rec=di.readLine()) != null)
  22.                         {
  23.                            for(int i=0;i<8;i++)
  24.                            {
  25.  
  26.                              //System.out.println(arr[i]);
  27.                              if(rec.startsWith(arr[i]))
  28.                              {
  29.                                  int len = rec.length();
  30.                                  out = rec.substring(arr[i].length(),len);
  31.                                  // System.out.print(out);//continue;
  32.                              // String ne=out;
  33.                               System.out.print(out);
  34.                              } 
  35.                              }
  36.  
  37.                    }
  38.  
  39.                     recCount++; 
  40.  
  41.              } 
  42.  
  43.              catch (IOException e)
  44.              { 
  45.               System.out.println("Uh oh, got an IOException error!" + e.getMessage()); 
  46.               } 
  47.  
  48.                finally 
  49.                   { 
  50.                     if (di != null)
  51.                        { 
  52.                           try 
  53.                             {
  54.                              di.close(); 
  55.                             }
  56.                          catch (IOException ioe)
  57.                             {
  58.                             }
  59.                         } 
  60.                   }
  61.  
  62.   }
  63.  
  64. }
  65.  
1.)Do not use streams to read files. Use Scanner or File Reader.
2.)Please explain more clearly what the program should do in general. Say something like "given any text file, the program should ..."
Jan 29 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.