Question

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.

Was it helpful?

Solution

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.
    }

}

OTHER TIPS

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.

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

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.

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;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top