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.

3
  • 1
    Do you mean new Integer[n] or ArrayList<Integer>? Commented Sep 3, 2013 at 10:25
  • 1
    Okay, you've said what you need to do - but you haven't said how you've tried to achieve it. What have you tried, and what went wrong? Have you got as far as splitting the numbers? Trying to parse each part in turn? Commented Sep 3, 2013 at 10:27
  • I meant new Integer[] Commented Sep 3, 2013 at 10:34

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

1 Comment

there is no keyword Scanner in processing 2 :(
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.

Comments

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();
    }
}

9 Comments

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.
@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.
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.)
@JonSkeet Done but didn't know that what you find in the net is good for direct "copy paste" in your project.
No, it's not a good idea - but that doesn't mean it doesn't happen :(
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.