0

I'm working on my graphic user interface for a application I'm creating. Basically there is this JTextField that the user has to input integers. For example

25, 50, 80, 90

Now, I have this other class that needs to get those values and put them in an int Array.

I've tried the following.

guiV = dropTheseText.getText().split(",");

And in the other class file I retieve the String, but I have no idea how to get the value for each one.

In the end I'm just trying to get something like

int[] vD = {textfieldvaluessplitbycommahere};

Still fairly new to Java but this has me crazy.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Vernard
  • 345
  • 4
  • 10
  • 18
  • Create a method with the signature `int[] asInts(String[] data)`; this should hopefully show the task that needs to be done, `String[]` being the result of `String.split`. It would be called like: `int[] vD = asInts(str.split(","));`. Use `Integer.parseInt` to help inside this method. There are also libraries which can help here, but it'll be a "good exercise" to implement manually. – user2246674 Jun 23 '13 at 00:42
  • And this is why I *don't* use Java or languages without accepted/common use of HoFs :D – user2246674 Jun 23 '13 at 00:46
  • If these are coordinates, also consider graphical input, for [example](http://stackoverflow.com/a/5797965/230513). – trashgod Jun 23 '13 at 10:24

5 Answers5

0

As you are fairly new to Java, rather than giving you the code snippet, I will just give you some indications:

  1. Use the String class: it has a method for splitting a String into an array of Strings.
  2. Then use the Integer class: it has a method to convert a String to an int.
Alejandro Colorado
  • 6,034
  • 2
  • 28
  • 39
  • Yes (and the OP is already doing the first bit), but the problem is there is no trivial way to go `String[]->int[]` trivially in standard Java. (Writing a loop is easy, but not trivial - as in a single terse expression. Java lacks support for trivial "map" operations.) – user2246674 Jun 23 '13 at 00:52
  • I understand he is fairly new to Java but not to programming, so writing a loop is really a trivial issue. Likewise, I understand that giving some indications and forcing him/her to investigate is a better option. – Alejandro Colorado Jun 23 '13 at 00:55
  • Writing a loop is easy and tedious. Trivial is: `str.Split(',').Select(int.Parse)` (of course Java has no equivalent so ..) But take time to read the question; not just the title. This answer points out redundant information and doesn't even mention using a loop .. – user2246674 Jun 23 '13 at 00:57
  • Well, it's just that your concept of trivial is different to mine, but it's ok. – Alejandro Colorado Jun 23 '13 at 01:01
  • This is a great way to miss out on the accept when someone who isn't evasive and coy provides an answer with code and links to the relevant documentation. – millimoose Jun 23 '13 at 01:07
0

You cant do it directly, you may need to add a method to convert your string array to int array. Something like this:

public int[] convertStringToIntArray(String strArray[]) {
    int[] intArray = new int[strArray.length];
    for(int i = 0; i < strArray.length; i++) {
        intArray[i] = Integer.parseInt(strArray[i]);
    }
    return intArray;
}

Pass your guiV to this method and get back the int array

Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136
0

a simple solution is a function hat does the conversion:

public static int[] convertTextFieldCommaSeparatedIntegerStringToIntArray(String fieldText) {
    String[] tmp = fieldText.split(",");
    int[] result = new int[tmp.length];

    for(int i = 0; i < tmp.length; i++) {
        result[i] = Integer.parseInt(tmp[i].trim());
    }

    return result;
}

The essential methods are:

split for splitting the original input at the comma.

parseInt for converting a String -> int. The valueOf function of Integer is an option but then you have to convert String -> Integer -> int.


Note:

You should use trim to eliminate white-spaces. Furthermore, you should catch the NumberFormatException thrown by parseInt. As an unchecked exception you do not need to catch it, but it is always wise to check user input and sanitize it if necessary.

PerfectPixel
  • 1,918
  • 1
  • 17
  • 18
0
private JTextField txtValues = new JTextField("25, 50, 80, 90"); 

// Strip the whitespaces using a regex since they will throw errors
// when converting to integers
String values = txtValues.getText().replaceAll("\\s","");

// Get the inserted values of the text field and use the comma as a separator.
// The values will be returned as a string array
private String[] strValues = values.split(",");

// Initialize int array
private int[] intValues = new int[strValues.length()];
// Convert each string value to an integer value and put it into the integer array
for(int i = 0; i < strValues.length(); i++) {
    try {
       intValues[i] = Integer.parseInt(strValues[i]);
    } catch (NumberFormatException nfe) {
       // The string does not contain a parsable integer.
    }

}
Stef Heylen
  • 294
  • 1
  • 9
  • @DaftPunk Thanks for your remark. I stripped the whitespaces to prevent integers from being not parsable and caught the NumberFormatException as well. Wrote this code out of the blue – Stef Heylen Jun 23 '13 at 02:18
  • Keeps returning null for me? Did you exact steps. @StefHeylen – Vernard Jun 23 '13 at 21:24
  • @StefHeylen I added .trim() and that seemed to solve it, thank you. – Vernard Jun 23 '13 at 21:32
0

Try this code:

public int[] getAsIntArray(String str)
{
    String[] values = str.getText().split(",");
    int[] intValues = new int[values.length];
    for(int index = 0; index < values.length; index++)
    {
        intValues[index] = Integer.parseInt(values[index]);
    }
    return intValues;
}