Advertisement
cd62131

Text to Simple Json

Nov 16th, 2013
343
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 0.93 KB | None | 0 0
  1. package qa10116588765;
  2.  
  3. import java.util.*;
  4.  
  5. public class Main {
  6.   private StringBuilder sb;
  7.  
  8.   public void parse(Scanner in) {
  9.     sb = new StringBuilder();
  10.     sb.append("{");
  11.     while (in.hasNextLine()) {
  12.       Scanner line = new Scanner(in.nextLine());
  13.       String head = line.next();
  14.       if (head.isEmpty()) continue;
  15.       sb.append("" + head + ": \"");
  16.       while (line.hasNext()) {
  17.         sb.append(line.next());
  18.         if (line.hasNext()) sb.append(" ");
  19.       }
  20.       line.close();
  21.       sb.append("\"");
  22.       if (in.hasNextLine()) sb.append(", ");
  23.     }
  24.     in.close();
  25.     sb.append("}");
  26.   }
  27.  
  28.   @Override
  29.   public String toString() {
  30.     return sb.toString();
  31.   }
  32.  
  33.   public static void main(String[] args) {
  34.     Main main = new Main();
  35.     Scanner in = new Scanner("aaa\tbbb\nccc\tddd\n");
  36.     // Scanner in = new Scanner(System.in);
  37.     main.parse(in);
  38.     System.out.println(main);
  39.   }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement