0

I have a list like this but much longer in a text document.

123451234
1234123
1234567
12345678

I need it to be reformatted to

"1123451234", "1234123","1234567","123123123","112345678"

I have tried the robot class with java in a loop. I can add the " " and the , but the problem is each line is a different length so I can't simply have it click the left arrow a certain amount of times. What could I do? If you need more info please ask and thanks in advance!

Here's the code for the person that asked.

Robot r = new Robot(); 

r.keyPress(KeyEvent.VK_QUOTE);
r.keyRelease(KeyEvent.VK_QUOTE);
r.keyPress(KeyEvent.VK_RIGHT);
r.keyRelease(KeyEvent.VK_RIGHT);
r.keyPress(KeyEvent.VK_RIGHT);
r.keyRelease(KeyEvent.VK_RIGHT);
r.keyPress(KeyEvent.VK_RIGHT);
r.keyRelease(KeyEvent.VK_RIGHT);
r.keyPress(KeyEvent.VK_RIGHT);
r.keyRelease(KeyEvent.VK_RIGHT);
r.keyPress(KeyEvent.VK_COMMA);
r.keyRelease(KeyEvent.VK_COMMA);
r.keyPress(KeyEvent.VK_QUOTE);
r.keyRelease(KeyEvent.VK_QUOTE);
SolidCloudinc
  • 329
  • 10
  • 27

3 Answers3

0

You could write a helper program that will take the data in the text file (for example, file.txt) and reformat it like so (untested code, but you get the idea):

BufferedReader reader = new BufferedReader(new FileReader(new File("file.txt")));
String line, result = "";
while ((line = reader.readLine()) != null) {
    result += "\"" + line + "\",";
}
result = result.substring(0, result.length() - 1); // remove last comma
// do stuff with result

About your Robot-based approach: you can use the Home and End keys to jump to the beginning or end of the line:

// jump to end
r.keyPress(KeyEvent.VK_END);
r.keyRelease(KeyEvent.VK_END);

// jump to start
r.keyPress(KeyEvent.VK_HOME;
r.keyRelease(KeyEvent.VK_HOME);
tckmn
  • 57,719
  • 27
  • 114
  • 156
  • OP need to add " as well in the beginning and end of each word. – Ahsan Shah Sep 19 '13 at 21:22
  • I will be trying this. – SolidCloudinc Sep 19 '13 at 21:46
  • @Doorknob BufferedReader reader = new BufferedReader(new FileReader(new File("file.txt"))); gives an error. Where should I place file.txt? – SolidCloudinc Sep 19 '13 at 22:08
  • @SolidCloudinc What error? Did you remember to `import` the relevant classes? – tckmn Sep 19 '13 at 22:16
  • @Doorknob Yes, and no suggestions available. – SolidCloudinc Sep 19 '13 at 22:19
  • @Doorknob Exception in thread "main" java.lang.Error: Unresolved compilation problems: The constructor BufferedReader(FileReader) is undefined FileReader cannot be resolved to a type File cannot be resolved to a type at Format.main(Format.java:8) – SolidCloudinc Sep 19 '13 at 22:22
  • @Doorknob sorry for all the question but now i get Exception in thread "main" java.io.FileNotFoundException: file.txt (The system cannot find the file specified) at java.io.FileInputStream.open(Native Method) at java.io.FileInputStream.(Unknown Source) at java.io.FileReader.(Unknown Source) at Format.main(Format.java:11) – SolidCloudinc Sep 19 '13 at 22:27
  • @SolidCloudinc `The system cannot find the file specified` Please actually read the error messages. The last one was quite clear, and this one is too. – tckmn Sep 19 '13 at 22:28
  • @Doorknob Right but where do I save the file? – SolidCloudinc Sep 19 '13 at 22:30
  • @SolidCloudinc In the same folder as your program... or just do `new File("C:/some/path/file.txt");`. I have to go now, hope you solve your problem! – tckmn Sep 19 '13 at 22:30
0

If you don't want to do this programmatically, I'd suggest just opening any decent text editor such as Sublime Text and do a find - replace with regular expressions switched on.

Find (\d+)\n

Replace with "$1",

With this, if you do a replace all, you'll end up with

"123451234","1234123","1234567",12345678

So, just do the last one manually.

adarshr
  • 61,315
  • 23
  • 138
  • 167
0

Using sed command-line

$ cat test.dat
123451234
1234123
1234567
12345678

$ sed -e's/^\(.*\)$/"\1"/' test.dat
"123451234"
"1234123"
"1234567"
"12345678"

The regular expression matches the entire line and wraps it with quotes. This outputs to stdout, but just redirect to a file to save the output:

$ sed -e's/^\(.*\)$/"\1"/' test.dat > quoted.dat
Jim Garrison
  • 85,615
  • 20
  • 155
  • 190