0

I have a String array in Java (Using the NetBeans IDE) containing a small text. When inserting the lines of text into the array I ended up with too much unnecessary space and characters which I would like to get rid of. Here is an example:

1 experimental investigation of the aerodynamics of a wing in a slipstream .   an experimental study of a wing [...] flow theory .   an empirical [...] .
2  small viscosity . in the study of high-speed [...] vorticity .  the discussion here is restricted to two-dimensional incompressible steady flow .

As you can see, in some cases I end up with 3 spaces between a period and the next word. How do I get rid of the extra space and characters such as periods, commas, etc?

Edit: Here is the process.

-Inserting the text on x position within the String array:

try{
            coleccion = new File (File location);
            fr = new FileReader (coleccion);
            br = new BufferedReader(fr);
            String numDoc = " ";
            int pos = 0;
            while((numDoc=br.readLine())!=null){
                if(numDoc.contains(".W")){
                    while((numDoc=br.readLine())!= null && !numDoc.contains(".I")){
                        if(Text[pos] != null) {
                            Text[pos] = Texto[pos] + " " + numDoc;s
                        }
                        else {
                            Text[pos] = numDoc;
                        }
                    }
                    pos++;
                }
            }
        }
        catch(Exception e){
             e.printStackTrace();
        }

-Printing the array (1400 positions):

for(int i=0; i<=1399; i++){
            //System.out.println(ID[i]);
            System.out.println(i+1 + " " + Texto[i]);
        }

-Extra info on the initial problem: .txt file to arrays using Java

Cœur
  • 37,241
  • 25
  • 195
  • 267
kaozbender
  • 95
  • 3
  • 8

1 Answers1

0

So i guess that by unnecessary space you mean consecutive spaces, if that's the case then this is what you want : https://stackoverflow.com/a/2932439/4088809

replaceAll("^ +| +$|( )+", "$1")

that's what you're looking for, just apply it to the entire line you get with the readLine, that should do the trick.

Community
  • 1
  • 1
Tahar Bakir
  • 716
  • 5
  • 14