Home > Mobile >  Move first word at end of String with new characters
Move first word at end of String with new characters

Time:07-04

I have a list of words with the format word1**word2

I want to put the firt word at the end of the String like word2 - word1.

how can I split the first word without ** and add a - before paste the word at the end?

I want to let the lines read from a file words.txt and create a new file new-words.txt

For example, I want Bibi**Tina to be converted in Tina - Bibi

Edit: I tried a new code. Now I get the right output on the Console but the new created file is empty.

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

public class Main { 
 

    public static void main(String args[]) {
    List<String> lines = new ArrayList<String>();
     String line = null;
        try {
            File f1 = new File("C:\\Users\\PC\\Desktop\\new-words.txt");
            FileOutputStream fop = new FileOutputStream(f1);
            FileReader fr = new FileReader(f1);
            BufferedReader  br = new BufferedReader(new FileReader("C:\\\\Users\\\\PC\\\\Desktop\\words.txt"));
            
            
            while ((line = br.readLine()) != null) {
                if (!line.contains("\\*\\*\\yok")) {
                String[] a = line.split("\\*\\*");
                System.out.println(a[1]   "  - "   a[0]);
                }
            }
            fr.close();
            br.close();

            FileWriter fw = new FileWriter(f1);
            BufferedWriter out = new BufferedWriter(fw);
            for(String s : lines)
            out.write(s);
            out.flush();
            out.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    
}
    

CodePudding user response:

Look at the official java doc. There are 3 options of how to use the write method, and none of them take a string as an argument.

  • void write(byte[] b): Writes b.length bytes from the specified byte array to this file output stream.
  • void write(byte[] b, int off, int len): Writes len bytes from the specified byte array starting at offset off to this file output stream.
  • void write(int b): Writes the specified byte to this file output stream.

You need to convert your string into a byte[] in order to write it to a file using a file output stream. See this StackOverflow post which talks about how to do that.

CodePudding user response:

This may work for you. Use replaceAll which takes a regular expression.

  • (\\w ) - capture a word
  • \\*\\* - back to back asterisks (must be escaped as they have special regex properties)
  • $1 and $2 - back references to the just captured words.
String s = "This is a test word1**word2";
s = s.replaceAll("(\\w )\\*\\*(\\w )", "$2 - $1");
System.out.println(s);

prints

This is a test word2 - word1

If the phrase isn't matched, the original string will be returned.

So in your case it would be the following:

while ((line = br.readLine()) != null) {
     line = line.replaceAll("(\\w )\\*\\*(\\w )", "$2 - $1");
     System.out.println(line);
}

CodePudding user response:

star.split('**').reverse().join('-')
  • Related