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

1  
// Written without an IDE!
2  
3  
import java.util.*;
4  
import java.io.*;
5  
6  
public class main {
7  
  public static void main(String[] args) throws IOException {
8  
    String s = readTextFile("input/main.java", "");
9  
    System.out.println(splitAtBrackets(s));
10  
  }
11  
  
12  
  static List<String> splitAtBrackets(String s) {
13  
    List<String> list = new ArrayList<String>();
14  
    int lastI = 0;
15  
    for (int i = 0; i < s.length(); i++) {
16  
      boolean a = i == 0 || "{}()[]".indexOf(s.charAt(i-1)) >= 0;
17  
      boolean b = "{}()[]".indexOf(s.charAt(i)) >= 0;
18  
      if (a || b) {
19  
        // split
20  
        if (lastI != i)
21  
          list.add(s.substring(lastI, i));
22  
        lastI = i;
23  
      }
24  
    }
25  
    if (s.length() > lastI)
26  
      list.add(s.substring(lastI));
27  
    // done
28  
    return list;
29  
  }
30  
  
31  
  public static String readTextFile(String fileName, String defaultContents) throws IOException {
32  
    if (!new java.io.File(fileName).exists())
33  
      return defaultContents;
34  
35  
    FileInputStream fileInputStream = new FileInputStream(fileName);
36  
    InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8");
37  
    return loadTextFile(inputStreamReader);
38  
  }
39  
40  
  public static String loadTextFile(Reader reader) throws IOException {
41  
    StringBuilder builder = new StringBuilder();
42  
    try {
43  
      BufferedReader bufferedReader = new BufferedReader(reader);
44  
      String line;
45  
      while ((line = bufferedReader.readLine()) != null)
46  
        builder.append(line).append('\n');
47  
    } finally {
48  
      reader.close();
49  
    }
50  
    return builder.length() == 0 ? "" : builder.substring(0, builder.length()-1);
51  
  }
52  
53  
}

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: 1004 / 899
Referenced in: [show references]