0

How can I create a Dynamic array that holds Integer values (not int) in PROCESSING.

I have stored the String into a text file (String Str= "12,13,14,15"). Now i need to split and convert them to Integer type after loading the text file.

MELWIN
  • 1,093
  • 4
  • 11
  • 19

3 Answers3

2

Since code is reading a file, I would use a Scanner instead:

    String str = "2,3,4,5,6,7";
    List<Integer> intList = new ArrayList<Integer>();
    Scanner sc = new Scanner(new File(/*yourFile*/));
    //Scanner sc = new Scanner(str);
    while(sc.hasNext()) {
        sc.useDelimiter(",");
        intList.add(sc.nextInt());
    }

    Integer[] wrapperArray = new Integer[intList.size()];
    intList.toArray(wrapperArray);

Scanner: How-to

Community
  • 1
  • 1
rocketboy
  • 8,990
  • 1
  • 31
  • 36
2

You can try this code.. It works fine for me

        try {
        FileReader fr = new FileReader("data.txt");
        BufferedReader br = new BufferedReader(fr);

        String str = br.readLine();

        String strArray[] = str.split(",");
        Integer intArray[] = new Integer[strArray.length];

        for (int i = 0; i < strArray.length; i++) {
            intArray[i] = Integer.parseInt(strArray[i]);
            System.out.println(intArray[i]);
        }

    } catch (Exception e) {
       // TODO: handle exception
       e.printStackTrace();
    }

I hope this will help you.

NSM
  • 21
  • 5
1
String str = "12,13,14,15";
String[] strArray = str.split(",");

int[] intArray = new int[strArray.length];

for (int i = 0; i < strArray.length; i++) {
    try {
        intArray[i] = Integer.parseInt(strArray[i]);
    } catch (NumberFormatException e) {
        // Handle the exception properly as noted by Jon
        e.printStackTrace();
    }
}
Sajal Dutta
  • 7,258
  • 25
  • 33
  • Note that just printing the stack trace is almost *always* the wrong way of handling exceptions. It's what IDEs tend to suggest automatically, but the code should pretty much never be left that way. – Jon Skeet Sep 03 '13 at 10:28
  • @JonSkeet Agreed but this should be handled by the designer how to handle it. I left it there so OP can handle it depending on the spec/design. – Sajal Dutta Sep 03 '13 at 10:30
  • But without mentioning it (or indeed writing *any* explanatory text) you're basically inviting the OP to copy and paste your code without change. IMO you should have *at least* added a TODO comment... and ideally written some text in your answer as well as the code. (Code-only answers are almost always less useful than those with an explanation.) – Jon Skeet Sep 03 '13 at 10:31
  • @JonSkeet Done but didn't know that what you find in the net is good for direct "copy paste" in your project. – Sajal Dutta Sep 03 '13 at 10:32
  • No, it's *not* a good idea - but that doesn't mean it doesn't happen :( – Jon Skeet Sep 03 '13 at 10:34
  • @JonSkeet well that sucks. – Sajal Dutta Sep 03 '13 at 10:36
  • I was able to convert the Strings into the int format using the spilt function. But the code needed was to suit the code in one.add("Triangle",new Integer[]{157,274,157,266,157,259,157,253,157,247,157,241,157,235};one.bind("ka","detected"); Its at the place of 157,274,157,... ,235 where i should be able to use the values that i have loaded. – MELWIN Sep 03 '13 at 10:48
  • @MELWIN one.add("Triangle", intArray); – Sajal Dutta Sep 03 '13 at 11:16
  • @SajalDutta thank you very much. Your idea helped me to solve my task... thank you – MELWIN Sep 03 '13 at 11:56