473,405 Members | 2,171 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Removing blank spaces

4
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 DataOutputStream dos to write read object into file.

int totRows = 0;
System.out.println("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.println(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
CUSTOMERINFOTABLE 6666
.............

Expect to get some help atleast this time!
Nov 1 '06 #1
10 10280
r035198x
13,262 8TB
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 DataOutputStream dos to write read object into file.

int totRows = 0;
System.out.println("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.println(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
CUSTOMERINFOTABLE 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:
Expand|Select|Wrap|Line Numbers
  1. String [] s = o.toString().split(" ");
  2. String output = "";
  3. for(String i: s) {
  4. output += i;
  5. }
  6. dos.writeChars(output);
Or maybe you can try to look at your toString method
Nov 1 '06 #2
prad
4
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?
Nov 1 '06 #3
r035198x
13,262 8TB
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?
Nov 1 '06 #4
prad
4
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.Number†¬• ”à‹ xp H€w< 
Nov 1 '06 #5
r035198x
13,262 8TB
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.Number†¬• ”à‹ xp H€w< 
What happens of you change String.valueOf(o) instead of o.toString()
Nov 1 '06 #6
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:
Expand|Select|Wrap|Line Numbers
  1. String [] s = o.toString().split(" ");
  2. String output = "";
  3. for(String i: s) {
  4. output += i;
  5. }
  6. 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..!!
Dec 5 '06 #7
r035198x
13,262 8TB
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
Dec 6 '06 #8
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)
Expand|Select|Wrap|Line Numbers
  1. import java.io.*;
  2. import java.nio.channels.*;
  3. import java.nio.*;//det här är inte som i exemplet
  4.  
  5. public class WriteAStringAsBytes {
  6.  
  7.     /**
  8.      * @param args
  9.      */
  10.     public static void main(String[] args) {
  11.         System.out.println("Writing...");
  12.         // TODO Auto-generated method stub
  13.         String phrase = new String("Garbage in, garbage out\n");
  14.         //String dirname = "C:/Skrivna filer/Bokstavsdata";
  15.         String dirname = "c:/Skrivna filer";
  16.         String filename = "byteData.txt";
  17.  
  18.         File aFile = new File(dirname, filename);
  19.         //Create the file output stream
  20.         FileOutputStream file = null;
  21.         try{
  22.             file = new FileOutputStream(aFile, true);
  23.         }catch(FileNotFoundException e){
  24.             e.printStackTrace(System.err);
  25.         }
  26.         FileChannel outChannel = file.getChannel();
  27.         ByteBuffer buf = ByteBuffer.allocate(phrase.length());
  28.         byte[] bytes = phrase.getBytes();
  29.  
  30.         buf.put(bytes);
  31.         buf.flip();
  32.         try{
  33.             outChannel.write(buf);
  34.             file.close();
  35.             System.out.println("It has been written.");
  36.         }catch(IOException e){
  37.             e.printStackTrace(System.err);
  38.         }
  39.     }
  40. }
  41.  
Feb 11 '08 #9
JosAH
11,448 Expert 8TB
Expect to get some help atleast this time!
Don't use a DataOutputStream for that if you want just readable text. A DataOutputStream
writes Strings as they are: two bytes and for ASCII characters one of those bytes
will be 0x00 (zero). DataOutputStreams are not for producing text, i.e. they produce
raw data.

kind regards,

Jos
Feb 11 '08 #10
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:
Expand|Select|Wrap|Line Numbers
  1. public void writeArray()
  2.     {
  3.         Individual I = null;
  4.         String dirname = "C:/Skrivna filer/Bokstavsdata";
  5.         String filename = "proverbs2.txt";
  6.         String[]sayings = new String[model.individualList.size() + 1];//+1 om det ska få plats variabelnamn
  7.         String[][] values = new String[model.individualList.size()][Individual.class.getFields().length];
  8.         Field[] fields= Individual.class.getFields();
  9.         String variableNames = "";
  10.         System.out.println(fields[0].getModifiers());
  11.         int j = 0;
  12.         Field field = null;
  13.  
  14.         for(int i = 0;i<Individual.class.getFields().length;i++)
  15.         {
  16.             field = fields[i];
  17.             if(!(Modifier.isFinal(fields[i].getModifiers())||fields[i].isEnumConstant()||(!fields[i].getGenericType().toString().contains("double")
  18.                     &&!fields[i].getGenericType().toString().contains("int"))))
  19.             {
  20.                 values[0][j]=fields[i].getName();
  21.                 variableNames = variableNames.concat(values[0][j].concat(","));//for some reason skipping "variableNames =" does not work
  22.                 j++;
  23.             }    
  24.         }
  25.         System.out.println(variableNames.substring(0,variableNames.length()-1));
  26.         sayings[0]=variableNames.substring(0,variableNames.length()-1).concat(System.getProperty("line.separator"));
  27.         String valuesToStore="";
  28.         Object o = null;
  29.         for(int i = 0; i<model.individualList.size(); i++)
  30.         {
  31.             valuesToStore="";
  32.             I = model.individualList.get(i);
  33.             o = (Object)I;
  34.             o.getClass();
  35.             int j3 = 0;
  36.             for(int j2 = 0;j2<fields.length;j2++)
  37.             {
  38.  
  39.                 if(!(Modifier.isFinal(fields[j2].getModifiers())||fields[j2].isEnumConstant()||(!fields[j2].getGenericType().toString().contains("double")
  40.                         &&!fields[j2].getGenericType().toString().contains("int"))))
  41.                 {
  42.  
  43.                     values[i][j3]="" + OLSRegressionCalc.sendNumber(o,fields[j2].getName());
  44.  
  45.  
  46.  
  47.                     valuesToStore = valuesToStore.concat(values[i][j3].concat(","));
  48.                     j3++;
  49.                 }
  50.             }
  51.             //sayings[i+1] = year + "," + age + System.getProperty("line.separator");
  52.             sayings[i+1]=valuesToStore + System.getProperty("line.separator");
  53.  
  54.         }
  55.  
  56.         File aFile = new File(dirname, filename);//Skapar filobjektet i Javaprogrammet
  57.  
  58.         FileOutputStream outputFile = null;
  59.         try{
  60.             outputFile = new FileOutputStream(aFile, true);//Skapar själva filen
  61.         }catch(FileNotFoundException e){
  62.             e.printStackTrace(System.err);
  63.             System.exit(1);
  64.         }
  65.         FileChannel outChannel = outputFile.getChannel();
  66.         int maxLength = 0;
  67.  
  68.         for(String saying: sayings)
  69.         {
  70.             if(maxLength < saying.length())
  71.                 maxLength = saying.length();
  72.         }
  73.         ByteBuffer buf = ByteBuffer.allocate(2*maxLength + 4);
  74.         int i = 0;
  75.         try{
  76.             for(String saying: sayings)
  77.             {
  78.                 byte[] bytes = sayings[i].getBytes();
  79.  
  80.                 buf.put(bytes);
  81.                 buf.flip();
  82.                 outChannel.write(buf);
  83.                 buf.clear();
  84.                 i++;
  85.             }
  86.             outputFile.close();
  87.             System.out.println("Proverbs written to file");
  88.         }catch(IOException e){
  89.             e.printStackTrace(System.err);
  90.             System.exit(1);
  91.         }
  92.         //System.exit(0);
  93.  
  94.     }
  95.  
Feb 11 '08 #11

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

Similar topics

9
by: Jean-Marc Molina | last post by:
Hello, I can't find a way to execute a Windows application, whose directory path contains blank spaces, from a PHP script. I also wonder if the problem happens under Linux and other OS. ...
1
by: Danny | last post by:
Given this: "*" the pattern to remove the htnl code between brackets, how can I remove a series of spaces and just make one space. like here is a series of blank...
2
by: Nimmy | last post by:
Hi, How can I print blank spaces with printf() I want something like printf("%6d %2d", lcVar1, lcVarb); Printout should be: 123123 34 12 spaces in between 123123 and 34 and 15...
1
by: macupryk | last post by:
-- _list Count = 13 System.Collections.ArrayList + {QUEST} object {System.Data.DataColumn} + {S_DAT} object {System.Data.DataColumn} + ...
3
by: ShaeMills via AccessMonster.com | last post by:
In my table I imported from .txt, one of my columns has blank spaces. The column is as follows, how do I eliminate the blank spaces in between the second and third, and fifth and sixth digits? ...
2
aspamit
by: aspamit | last post by:
I am allowing users to select two months for the period.I have created 12 labels for 12 months. Suppose if user selects Feb to Apr.Only those labels and data should is displayed.Other labels and...
1
by: abhilash12 | last post by:
validating blank spaces. The input box should accept the value - for eg "JOHN SMITH" but it should not allow two blank spaces.
1
by: maryanncanor | last post by:
Hi everyone, My problem is whenever I export a report to a textfile. The output textfile have blank spaces. Here is my query: SELECT ( & '|' & & '|'...
2
by: SwapnilD | last post by:
I'm implementing a feature which reads comma separated txt file from server(one line at a time). Format of file is fixed, There are 3 columns on each row. After reading the row from file I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.