Reputation: 694
I have a converter that will convert an array of int into a String.
**EDIT** [1, 2, -45, 678] -> "1 2 -45 678";
[1] -> "1"
Into this converter, I have a supportFromString
method to identify if the string can be converted back into an array.
I though of using regex to do so, but I can't find a good match.
I tried "\\d+\\s"
and "[\\d+]\\s"
but s.matches(pattern)
returns false.
Any suggestion? If one non-numeric character is found, it's not valid.
EDIT Here is the toString method of the converter
@Override
public String toString(Object o, ConverterContext converterContext) {
StringBuilder builder = new StringBuilder();
for (Integer integer : ((Integer[]) o)) {
if (builder.toString().length() > 0) {
builder.append(" ");
}
builder.append(integer);
}
return builder.toString();
}
Upvotes: 0
Views: 257
Reputation: 2878
This is the regex you can use to test if the string is a number optionally followed by other numbers separated by one or more spaces:
"(0|-?[1-9][0-9]*)(\\s+(0|-?[1-9][0-9]*))*"
EDIT: Included negative integers and excluded leading zeros.
Upvotes: 0
Reputation: 43053
Try this code :
String pattern = "(-?\\d+\\s*)+";
String test = "1 -5 2 45 678";
System.out.println(test.matches(pattern));
Output
True
If you want to find the digits, use this pattern alone: -?\\d+\\s*
.
Upvotes: 1
Reputation: 20163
Here my take, which handles negative numbers.
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.Arrays;
/**
<P>java IntArrayToStringToIntArray</P>
**/
public class IntArrayToStringToIntArray {
private static final Pattern pDigits = Pattern.compile("-?\\d+");
public static final void main(String[] igno_red) {
String sArray = Arrays.toString(new int[]{1, 2, -45, 678});
System.out.println("---The array: " + sArray);
System.out.println(isSafeToConvertToStringedArrayToIntArray(sArray));
int[] ai = getIntArrayFromToStringedArray(sArray);
for(int i : ai) {
System.out.println(i);
}
System.out.println();
sArray = Arrays.toString(new int[]{1});
System.out.println("---The array: " + sArray);
System.out.println(isSafeToConvertToStringedArrayToIntArray(sArray));
ai = getIntArrayFromToStringedArray(sArray);
for(int i : ai) {
System.out.println(i);
}
System.out.println();
sArray = "bogus";
System.out.println("---The array: " + sArray);
System.out.println(isSafeToConvertToStringedArrayToIntArray(sArray));
}
//Assumes the string is formatted as if by Arrays.toString(int[])
public static final boolean isSafeToConvertToStringedArrayToIntArray(String s_ofInts) {
//Eliminate the surrounding square brackets
s_ofInts = s_ofInts.substring(1, s_ofInts.length() - 1);
String[] as = s_ofInts.split(", ");
for(String s : as) {
Matcher m = pDigits.matcher(s);
if(!m.matches()) {
return false;
}
}
return true;
}
//Assumes the string is formatted as if by Arrays.toString(int[])
public static final int[] getIntArrayFromToStringedArray(String s_ofInts) {
//Eliminate the surrounding square brackets
s_ofInts = s_ofInts.substring(1, s_ofInts.length() - 1);
String[] as = s_ofInts.split(", ");
int[] ai = new int[as.length];
for(int i = 0; i < as.length; i++) {
String s = as[i];
Matcher m = pDigits.matcher(s);
if(m.matches()) {
ai[i] = Integer.parseInt(s);
} else {
throw new IllegalArgumentException("getIntArrayFromToStringedArray: Element " + i + " in s_ofInts is not an int: \"" + s + "\".");
}
}
return ai;
}
}
Upvotes: 0
Reputation: 314
static Pattern validPattern = Pattern.compile("^(\\-{0,1}\\d+\\s)*(\\-){0,1}\\d+$");
public static void main(String[] args) {
isStringValid("1 2 3 4 5");
isStringValid("1 26 35 44 53");
isStringValid("1 2a 3s 4 5");
isStringValid("1234");
isStringValid("1");
isStringValid("1 ");
isStringValid("a b c");
isStringValid("1 b c");
isStringValid("1 1 2 33333");
isStringValid("-1 1 2 33333");
isStringValid("1 1 2 -33333");
isStringValid("1 1 2 ----33333");
isStringValid("---1 1 2 -33333");
}
public static boolean isStringValid(String s){
s = s.trim();
boolean b = validPattern.matcher(s).find();
if (b){
System.out.printf("%s: is valid.\n", s);
}else{
System.out.printf("%s: is NOT valid.\n", s);
}
return(b);
}
Result:
1 2 3 4 5: is valid.
1 26 35 44 53: is valid.
1 2a 3s 4 5: is NOT valid.
1234: is valid.
1: is valid.
1: is valid.
a b c: is NOT valid.
1 b c: is NOT valid.
1 1 2 33333: is valid.
-1 1 2 33333: is valid.
1 1 2 -33333: is valid.
1 1 2 ----33333: is NOT valid.
---1 1 2 -33333: is NOT valid.
Upvotes: 1
Reputation: 145
I would test the string against the following regex:
[^\d ]
If you get any results it means you have something else other than a digit (\d) or a space
Upvotes: 2
Reputation: 571
You could read the entire input line from scanner, then split the line by,then you have a String[], parse each number into int[] with index one to one matching(assuming valid input and no NumberFormatExceptions)like
String line = scanner.nextLine();
String[] numberStrs = line.split(",");
int[] numbers = new int[numberStrs.length];
for(int i = 0;i < numberStrs.length;i++)
{
numbers[i] = Integer.parseInt(numberStrs[i]);
}
Upvotes: -1