Discussions

EJB programming & troubleshooting: Need Java Guru help with transforming data

  1. Need Java Guru help with transforming data (5 messages)

    Can you transfer this data:

    PM|||1|XX|PID HOST|HOT|

    INTO:

    PM|NULL|NULL|1|XX|PID HOST|HOT|

    I need this function to go through (||) and make it (|null|)

    Is there a good way of doing this. I'm no expert, but I need some help with this. I would appreciate any help or advices on how to go about it.

    This is my code:

     

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

    public class App1 {
         public App1 ( ) throws IOException{

              //URL anyURL = new URL("http://localhost/anyfile.ext");
             //BufferedInputStream buf = new BufferedInputStream(anyURL.openStream());

                  //BufferedReader br = new BufferedReader(new FileReader(new File("final.txt")));
                  BufferedReader br = new BufferedReader(new FileReader(new File("data.txt")));
                  BufferedWriter out = new BufferedWriter(new FileWriter(new File("finalTest.txt")));

                  StringTokenizer st;
                  ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
                  //Function that I'm thinking of creating to check stream for null values

                  String c="||";

                  ValidString = replaceVal(br.readLine(),c);

                  ///////////////////////////////////////////////////////////////////////////////////////////////////////////////

                   public static replaceVal(String, String) {

                   This function should loop by replacing a value for another value

                   like "|" for "|null|"

                   }

                  ///////////////////////////////////////////////////////////////////////////////////////////////////////////////

                  int temp;

                  while(true){
                         if( (c=br.readLine()) == null) break;
                         st = new StringTokenizer(c);
                         temp = st.countTokens();
                         for(int i = 0; i < (temp+1); i++){
                             //System.out.println(" " + st.nextToken());

                               if(i!=0){
                                      //out.write(" " + st.nextToken());
                                      System.out.print(" " + st.nextToken());

                                }
                               else{
                                      //out.newLine();
                                      System.out.println(" ");
                               }
                         } //for
                  } //while


          out.flush();
          br.close();
          out.close();
         } //end of constructor

        public static void main ( String args[] ) throws IOException {
                new App1();
        }//End of Main Method

     

    } // End of Application


  2. Here, I wrote a small java class to do just the same...hope u find it simpler.

    Thanks,
    Uday

    public class TestClass
    {
    public static void main(String[] args)
    {
    System.out.println("Hello World!");

    String s1 = "PM|||1|XX|PID HOST|HOT|";
    int i =0;
    while ((i=s1.indexOf("||")) > 0){
    StringBuffer sb = new StringBuffer(s1);
    sb.replace(i,i+2,"|null|");
    s1 = new String(sb);
    }
    System.out.println(s1);
    }
    }
  3. How abt using regionMatches func to determine if a string contain '||'. Tell me what you think about this function.
  4. When I use it, I get an error that I can't convert a java String into a

    A get an error that I can't convert a java.lang.String into a java.io.BufferedReader and that I can't convert a java.io.BufferedReader into a java.lang.String. Any solution in my modifying my code below to get it working


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

    public class App4 {

         public App4 ( ) throws IOException{

              //URL anyURL = new URL("http://localhost/anyfile.ext");
              //BufferedInputStream buf = new BufferedInputStream(anyURL.openStream());

                  //BufferedReader br = new BufferedReader(new FileReader(new File("final.txt")));
                  BufferedReader br = new BufferedReader(new FileReader(new File("data.txt")));
                  BufferedWriter out = new BufferedWriter(new FileWriter(new File("finalTest.txt")));

                  StringTokenizer st;

                  String c="|";

      //////////////////////////////////////////////////////////////////////////////////////////////
      //Function that I'm thinking of creating to check stream for null values


                  int j =0;

    while ((j=br.readLine().indexOf("||")) > 0){
    StringBuffer sb = new StringBuffer(br);
    sb.replace(j,j+2,"|null|");
    br = new String(sb);

    /* App4.replace(a,aa); //Maybe by modifying this function it might work */

    }
    //Verify output
    System.out.println(br);

      /////////////////////////////////////////////////////////////////////////////////////////////



                  int temp;


                  while(true){
                         if( (c=br.readLine()) == null) break;
                         st = new StringTokenizer(c);
                         temp = st.countTokens();
                         for(int i = 0; i < (temp+1); i++){
                             //System.out.println(" " + st.nextToken());

                               if(i!=0){
                                      //out.write(" " + st.nextToken());
                                      System.out.print(" " + st.nextToken());

                               } else {
                                      //out.newLine();
                                      System.out.println(" ");
                               }
                         } //for
                  } //while


          out.flush();
          br.close();
          out.close();
        } //end of constructor

        public static void main ( String args[] ) throws IOException {
                new App4();
        }//End of Main Method


    // Another option would be to use this function

    /////////////////////////////////////////////////////////////////////////////////

    //This function should loop by replacing a value for another value

    /* public String replace(char oldChar, char newChar)
    {
    char[] buff = new char[value.length];
    boolean found = false;

    for (int i = 0; i < value.length; i++)
    {
    char c = value[i];
    if (c == oldChar)
    {
    found = true;
    buff[i] = newChar;
    }
    else
    buff[i] = c;
    }

    return found ? new String(buff) : this;
    }
    */
    ////////////////////////////////////////////////////////////////////////////////////////////////



    } // End of Application
  5. When I use it, I get an error that I can't convert a java String into a

    A get an error that I can't convert a java.lang.String into a java.io.BufferedReader and that I can't convert a java.io.BufferedReader into a java.lang.String. Any solution in my modifying my code below to get it working


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

    public class App4 {

         public App4 ( ) throws IOException{

              //URL anyURL = new URL("http://localhost/anyfile.ext");
              //BufferedInputStream buf = new BufferedInputStream(anyURL.openStream());

                  //BufferedReader br = new BufferedReader(new FileReader(new File("final.txt")));
                  BufferedReader br = new BufferedReader(new FileReader(new File("data.txt")));
                  BufferedWriter out = new BufferedWriter(new FileWriter(new File("finalTest.txt")));

                  StringTokenizer st;

                  String c="|";

      //////////////////////////////////////////////////////////////////////////////////////////////
      //Function that I'm thinking of creating to check stream for null values


                  int j =0;

    while ((j=br.readLine().indexOf("||")) > 0){
            StringBuffer sb = new StringBuffer(br);
    sb.replace(j,j+2,"|null|");
    br = new String(sb);

    /* App4.replace(a,aa); //Maybe by modifying this function it might work */

    }
    //Verify output
    System.out.println(br);

      /////////////////////////////////////////////////////////////////////////////////////////////



                  int temp;


                  while(true){
                         if( (c=br.readLine()) == null) break;
                         st = new StringTokenizer(c);
                         temp = st.countTokens();
                         for(int i = 0; i < (temp+1); i++){
                             
                               if(i!=0){
                                      //out.write(" " + st.nextToken());
                                      System.out.print(" " + st.nextToken());

                               } else {
                                      //out.newLine();
                                      System.out.println(" ");
                               }
                         } //for
                  } //while


          out.flush();
          br.close();
          out.close();
        } //end of constructor

        public static void main ( String args[] ) throws IOException {
                new App4();
        }//End of Main Method


    // Another option would be to use this function

    /////////////////////////////////////////////////////////////////////////////////

    //This function should loop by replacing a value for another value

    /* public String replace(char oldChar, char newChar)
    {
    char[] buff = new char[value.length];
    boolean found = false;

    for (int i = 0; i < value.length; i++)
    {
    char c = value[i];
    if (c == oldChar)
    {
    found = true;
    buff[i] = newChar;
    }
    else
    buff[i] = c;
    }

    return found ? new String(buff) : this;
    }
    */
    ////////////////////////////////////////////////////////////////////////////////////////////////



    } // End of Application
  6. There is no StringBuffer constructor which takes a BufferedReader as the argument. Look at the code marked with >>>> <This is the reason you are getting the exception.

    <your-code>
    //Function that I'm thinking of creating to check stream for null values

    int j =0;
    while ((j=br.readLine().indexOf("||")) > 0){

    <the-problem>

    >>>>> StringBuffer sb = new StringBuffer(br); <//This StringBuffer constructor is illegal / non-existent
    //br is a BufferedReader, not a String

    </the-problem>

            sb.replace(j,j+2,"|null|");
            br = new String(sb);
    </your-code>

    Hope this helps,
    --Das