Removing blank spaces

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • prad
    New Member
    • Oct 2006
    • 4

    #1

    Removing blank spaces

    Hello,
    Following is a part of java program.
    I am reading Result Set returned by Stored Procedure in db2 and writing that into a file.
    I have used DataOutputStrea m dos to write read object into file.

    int totRows = 0;
    System.out.prin tln("Result Set"+(count-1)+" Writing into file...");
    while (rs.next()) {
    totRows++;
    for (int i = 1; i <= col; i++) {
    Object o = rs.getObject( i);
    dos.writeChars( String.valueOf( o));
    dos.writeUTF("\ t");
    }
    dos.writeUTF("\ r\n");
    }
    System.out.prin tln(totRows+" rows written...");


    Output is : File contents r like
    T A B L E N A M E T A B L E C O U N T
    C U S T O M E R I N F O T A B L E 6666
    ........
    ......
    n so on


    I want it to look like
    TABLENAME TABLECOUNT
    CUSTOMERINFOTAB LE 6666
    .............

    Expect to get some help atleast this time!
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by prad
    Hello,
    Following is a part of java program.
    I am reading Result Set returned by Stored Procedure in db2 and writing that into a file.
    I have used DataOutputStrea m dos to write read object into file.

    int totRows = 0;
    System.out.prin tln("Result Set"+(count-1)+" Writing into file...");
    while (rs.next()) {
    totRows++;
    for (int i = 1; i <= col; i++) {
    Object o = rs.getObject( i);
    dos.writeChars( String.valueOf( o));
    dos.writeUTF("\ t");
    }
    dos.writeUTF("\ r\n");
    }
    System.out.prin tln(totRows+" rows written...");


    Output is : File contents r like
    T A B L E N A M E T A B L E C O U N T
    C U S T O M E R I N F O T A B L E 6666
    ........
    ......
    n so on


    I want it to look like
    TABLENAME TABLECOUNT
    CUSTOMERINFOTAB LE 6666
    .............

    Expect to get some help atleast this time!
    I do not think the problem is caused by the writeChars method but rather the String.valueOf for your objects is the one that is putting in the spaces. (It does not seem to be happening for numbers?). You may need to split the output first:
    Code:
    String [] s = o.toString().split(" ");
    String output = "";
    for(String i: s) {
    output += i;
    }
    dos.writeChars(output);
    Or maybe you can try to look at your toString method

    Comment

    • prad
      New Member
      • Oct 2006
      • 4

      #3
      Hi,
      Thanx for the solution!
      Its working properly for strings... but not for numbers...
      can u tel me y so?wat can be done 4 dat?

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by prad
        Hi,
        Thanx for the solution!
        Its working properly for strings... but not for numbers...
        can u tel me y so?wat can be done 4 dat?
        What is happening for the numbers?

        Comment

        • prad
          New Member
          • Oct 2006
          • 4

          #5
          Its gvg smthing like this....
          here CARD is first column n remaining is a number field
          t CARDw8 sr java.lang.Long ;‹äÌ#ß J valuexr java.lang.Numb er†¬• ”à‹ xp H€w< 

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            Originally posted by prad
            Its gvg smthing like this....
            here CARD is first column n remaining is a number field
            t CARDw8 sr java.lang.Long ;‹äÌ#ß J valuexr java.lang.Numb er†¬• ”à‹ xp H€w< 
            What happens of you change String.valueOf( o) instead of o.toString()

            Comment

            • CodeMan007
              New Member
              • Oct 2006
              • 3

              #7
              Originally posted by r035198x
              I do not think the problem is caused by the writeChars method but rather the String.valueOf for your objects is the one that is putting in the spaces. (It does not seem to be happening for numbers?). You may need to split the output first:
              Code:
              String [] s = o.toString().split(" ");
              String output = "";
              for(String i: s) {
              output += i;
              }
              dos.writeChars(output);
              Or maybe you can try to look at your toString method

              Dont u think there is an error in the code that u proposed

              for(String i: s)
              the colon between String i and s
              at least its not working for me...nor do i know what that means..!!

              Comment

              • r035198x
                MVP
                • Sep 2006
                • 13225

                #8
                Originally posted by CodeMan007
                Dont u think there is an error in the code that u proposed

                for(String i: s)
                the colon between String i and s
                at least its not working for me...nor do i know what that means..!!
                Certainly looks strange doesn't it? Well it's not an error but it's the new for loop format added to jdk 1.5. That code will only work for 1.5 and higher compilers. Visit sun.com to get the other additions brought into 1.5 and 1.6

                Comment

                • gustaverikoberg
                  New Member
                  • Feb 2008
                  • 2

                  #9
                  I am having a similar problem. There is function that is called String.getBytes ()
                  that is said to remedy the problem of blank spaces.
                  Here is an example from Ivor Hortons Beginning Java (10 ed)
                  Code:
                  import java.io.*;
                  import java.nio.channels.*;
                  import java.nio.*;//det här är inte som i exemplet
                  
                  public class WriteAStringAsBytes {
                  
                  	/**
                  	 * @param args
                  	 */
                  	public static void main(String[] args) {
                  		System.out.println("Writing...");
                  		// TODO Auto-generated method stub
                  		String phrase = new String("Garbage in, garbage out\n");
                  		//String dirname = "C:/Skrivna filer/Bokstavsdata";
                  		String dirname = "c:/Skrivna filer";
                  		String filename = "byteData.txt";
                  		
                  		File aFile = new File(dirname, filename);
                  		//Create the file output stream
                  		FileOutputStream file = null;
                  		try{
                  			file = new FileOutputStream(aFile, true);
                  		}catch(FileNotFoundException e){
                  			e.printStackTrace(System.err);
                  		}
                  		FileChannel outChannel = file.getChannel();
                  		ByteBuffer buf = ByteBuffer.allocate(phrase.length());
                  		byte[] bytes = phrase.getBytes();
                  		
                  		buf.put(bytes);
                  		buf.flip();
                  		try{
                  			outChannel.write(buf);
                  			file.close();
                  			System.out.println("It has been written.");
                  		}catch(IOException e){
                  			e.printStackTrace(System.err);
                  		}
                  	}
                  }

                  Comment

                  • JosAH
                    Recognized Expert MVP
                    • Mar 2007
                    • 11453

                    #10
                    Originally posted by prad
                    Expect to get some help atleast this time!
                    Don't use a DataOutputStrea m for that if you want just readable text. A DataOutputStrea m
                    writes Strings as they are: two bytes and for ASCII characters one of those bytes
                    will be 0x00 (zero). DataOutputStrea ms are not for producing text, i.e. they produce
                    raw data.

                    kind regards,

                    Jos

                    Comment

                    • gustaverikoberg
                      New Member
                      • Feb 2008
                      • 2

                      #11
                      Good tip about the output stream.
                      I have been working on some code and solved my problem (maybe in a not so smart way, my exam and line of proffession is in population studies) with the empty s p a c es , so if anyone would be helped here is the code:
                      Code:
                      public void writeArray()
                      	{
                      		Individual I = null;
                      		String dirname = "C:/Skrivna filer/Bokstavsdata";
                      		String filename = "proverbs2.txt";
                      		String[]sayings = new String[model.individualList.size() + 1];//+1 om det ska få plats variabelnamn
                      		String[][] values = new String[model.individualList.size()][Individual.class.getFields().length];
                      		Field[] fields= Individual.class.getFields();
                      		String variableNames = "";
                      		System.out.println(fields[0].getModifiers());
                      		int j = 0;
                      		Field field = null;
                      		
                      		for(int i = 0;i<Individual.class.getFields().length;i++)
                      		{
                      			field = fields[i];
                      			if(!(Modifier.isFinal(fields[i].getModifiers())||fields[i].isEnumConstant()||(!fields[i].getGenericType().toString().contains("double")
                      					&&!fields[i].getGenericType().toString().contains("int"))))
                      			{
                      				values[0][j]=fields[i].getName();
                      				variableNames = variableNames.concat(values[0][j].concat(","));//for some reason skipping "variableNames =" does not work
                      				j++;
                      			}	
                      		}
                      		System.out.println(variableNames.substring(0,variableNames.length()-1));
                      		sayings[0]=variableNames.substring(0,variableNames.length()-1).concat(System.getProperty("line.separator"));
                      		String valuesToStore="";
                      		Object o = null;
                      		for(int i = 0; i<model.individualList.size(); i++)
                      		{
                      			valuesToStore="";
                      			I = model.individualList.get(i);
                      			o = (Object)I;
                      			o.getClass();
                      			int j3 = 0;
                      			for(int j2 = 0;j2<fields.length;j2++)
                      			{
                      				
                      				if(!(Modifier.isFinal(fields[j2].getModifiers())||fields[j2].isEnumConstant()||(!fields[j2].getGenericType().toString().contains("double")
                      						&&!fields[j2].getGenericType().toString().contains("int"))))
                      				{
                      					
                      					values[i][j3]="" + OLSRegressionCalc.sendNumber(o,fields[j2].getName());
                      					
                      					
                      					
                      					valuesToStore = valuesToStore.concat(values[i][j3].concat(","));
                      					j3++;
                      				}
                      			}
                      			//sayings[i+1] = year + "," + age + System.getProperty("line.separator");
                      			sayings[i+1]=valuesToStore + System.getProperty("line.separator");
                      			
                      		}
                      			
                      		File aFile = new File(dirname, filename);//Skapar filobjektet i Javaprogrammet
                      		
                      		FileOutputStream outputFile = null;
                      		try{
                      			outputFile = new FileOutputStream(aFile, true);//Skapar själva filen
                      		}catch(FileNotFoundException e){
                      			e.printStackTrace(System.err);
                      			System.exit(1);
                      		}
                      		FileChannel outChannel = outputFile.getChannel();
                      		int maxLength = 0;
                      		
                      		for(String saying: sayings)
                      		{
                      			if(maxLength < saying.length())
                      				maxLength = saying.length();
                      		}
                      		ByteBuffer buf = ByteBuffer.allocate(2*maxLength + 4);
                      		int i = 0;
                      		try{
                      			for(String saying: sayings)
                      			{
                      				byte[] bytes = sayings[i].getBytes();
                      				
                      				buf.put(bytes);
                      				buf.flip();
                      				outChannel.write(buf);
                      				buf.clear();
                      				i++;
                      			}
                      			outputFile.close();
                      			System.out.println("Proverbs written to file");
                      		}catch(IOException e){
                      			e.printStackTrace(System.err);
                      			System.exit(1);
                      		}
                      		//System.exit(0);
                      	
                      	}
                      Last edited by gustaverikoberg; Feb 11 '08, 10:51 AM. Reason: Too long

                      Comment

                      Working...