Guest User

Untitled

a guest
Jul 16th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.18 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.FileNotFoundException;
  3. import java.util.ArrayList;
  4. import java.util.Scanner;
  5. import java.util.regex.Pattern;
  6.  
  7. /**
  8. *
  9. */
  10.  
  11. /**
  12. * @author Criz
  13. *
  14. */
  15. public class RegExp {
  16.  
  17.  
  18. public static void main(String[] args) {
  19.  
  20. Scanner scanner;
  21. File fileName = new File("input.txt");
  22. //Create Scanner and check if input file exists
  23. try{
  24. scanner = new Scanner(fileName);
  25. ArrayList<String> l = new ArrayList<String>();
  26. while (scanner.hasNextLine()){
  27. l.add(revertOrder(scanner.nextLine()));
  28. }
  29. for (int i=0; i < l.size();i++){
  30. System.out.println(l.get(i));
  31. }
  32. }
  33. catch (FileNotFoundException e){
  34. }
  35. }
  36.  
  37. public static String revertOrder(String line){
  38. Pattern p = Pattern.compile("\t");
  39. String[] words = line.split(p.pattern());
  40. for (int i=0, j=words.length-1; i != j || i>j; i++,j--){
  41. String w1 = words[i];
  42. String w2 = words[j];
  43. words[i] = w2;
  44. words[j] = w1;
  45. }
  46. StringBuilder s = new StringBuilder();
  47. for(int i=0; i<words.length;i++){
  48. if(i == words.length-1)
  49. s.append(words[i]);
  50. else
  51. s.append(words[i] + "\t");
  52. }
  53. return s.toString();
  54. }
  55.  
  56. }
Add Comment
Please, Sign In to add comment