Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

53
LINES

< > BotCompany Repo | #576 // At-bracket-splitter (JavaX)

JavaX source code - run with: x30.jar

// Written without an IDE!

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

public class main {
  public static void main(String[] args) throws IOException {
    String s = readTextFile("input/main.java", "");
    System.out.println(splitAtBrackets(s));
  }
  
  static List<String> splitAtBrackets(String s) {
    List<String> list = new ArrayList<String>();
    int lastI = 0;
    for (int i = 0; i < s.length(); i++) {
      boolean a = i == 0 || "{}()[]".indexOf(s.charAt(i-1)) >= 0;
      boolean b = "{}()[]".indexOf(s.charAt(i)) >= 0;
      if (a || b) {
        // split
        if (lastI != i)
          list.add(s.substring(lastI, i));
        lastI = i;
      }
    }
    if (s.length() > lastI)
      list.add(s.substring(lastI));
    // done
    return list;
  }
  
  public static String readTextFile(String fileName, String defaultContents) throws IOException {
    if (!new java.io.File(fileName).exists())
      return defaultContents;

    FileInputStream fileInputStream = new FileInputStream(fileName);
    InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8");
    return loadTextFile(inputStreamReader);
  }

  public static String loadTextFile(Reader reader) throws IOException {
    StringBuilder builder = new StringBuilder();
    try {
      BufferedReader bufferedReader = new BufferedReader(reader);
      String line;
      while ((line = bufferedReader.readLine()) != null)
        builder.append(line).append('\n');
    } finally {
      reader.close();
    }
    return builder.length() == 0 ? "" : builder.substring(0, builder.length()-1);
  }

}

Author comment

Works with x5 -v2
("x5" alone seems buggy)

download  show line numbers  debug dex  old transpilations   

Travelled to 13 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

Comments [hide]

ID Author/Program Comment Date
157 #1000604 (pitcher) 2015-08-18 00:48:14
151 #1000610 (pitcher) 2015-08-18 00:48:00

add comment

Snippet ID: #576
Snippet name: At-bracket-splitter (JavaX)
Eternal ID of this version: #576/1
Text MD5: 4b23eff26394f2043653de8786097663
Author: stefan
Category:
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-04-14 17:58:46
Source code size: 1654 bytes / 53 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 1156 / 1078
Referenced in: [show references]