Java 如何将带“[]”的字符串转换为整数数组
我有两条线:Java 如何将带“[]”的字符串转换为整数数组,java,string,get,Java,String,Get,我有两条线: String s1 = "[143, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 157, 158, 159, 162, 163, 164]"; String s2 = "[20, 35, 74, 78, 124, 125, 126, 127, 131, 132, 143, 144, 145, 146]"; 我只想在这两个字符串中找到最小的公共数。所以我首先替换[]符号,并将数字从字符串中提取为整数。然后使用循环
String s1 = "[143, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 157, 158, 159, 162, 163, 164]";
String s2 = "[20, 35, 74, 78, 124, 125, 126, 127, 131, 132, 143, 144, 145, 146]";
我只想在这两个字符串中找到最小的公共数。所以我首先替换[]符号,并将数字从字符串中提取为整数。然后使用循环找到最小的公共数。程序如下:
s1 = s1.replace("[","");
s1 = s1.replace("]","");
String [] band = s1.split(",");
s2 = s2.replace("[","");
s2 = s2.replace("]","");
String [] hotel = s1.split(",");
System.out.println( EarliestCommonSlot(hotel,band) );
public static int EarliestCommonSlot(String [] a1, String [] b1){
int i=0,j=0;
int common = -1;
int [] a = new int [a1.length];
int [] b = new int [b1.length];
for(i = 0;i < a1.length;i++)
{
a[i] = Integer.parseInt( a1[i]);
System.out.println(a1[i]);
}
for(i = 0;i < b1.length;i++)
{
b[i] = Integer.parseInt( b1[i]);
System.out.println(b1[i]);
}
i = 0; j=0;
while ( i< a.length && j < b.length){
if ( a[i] == b[j] ){
common = a[i]; break;
}
if ( a[i] < b[j] ){
i++;
}
else j++;
}
return common;
}
最早的公共插槽定义如下:
s1 = s1.replace("[","");
s1 = s1.replace("]","");
String [] band = s1.split(",");
s2 = s2.replace("[","");
s2 = s2.replace("]","");
String [] hotel = s1.split(",");
System.out.println( EarliestCommonSlot(hotel,band) );
public static int EarliestCommonSlot(String [] a1, String [] b1){
int i=0,j=0;
int common = -1;
int [] a = new int [a1.length];
int [] b = new int [b1.length];
for(i = 0;i < a1.length;i++)
{
a[i] = Integer.parseInt( a1[i]);
System.out.println(a1[i]);
}
for(i = 0;i < b1.length;i++)
{
b[i] = Integer.parseInt( b1[i]);
System.out.println(b1[i]);
}
i = 0; j=0;
while ( i< a.length && j < b.length){
if ( a[i] == b[j] ){
common = a[i]; break;
}
if ( a[i] < b[j] ){
i++;
}
else j++;
}
return common;
}
为什么呢?我怎样才能解决这个问题呢?而不是
s1.replace("]"," "); // This replaces the bracket with a space.
// The number with the space will emit the error
使用:
新代码:
s1 = s1.replace("[","");
s1 = s1.replace("]","");
String [] band = s1.split(", ");
s2 = s2.replace("[","");
s2 = s2.replace("]","");
String [] hotel = s1.split(", "); //Comma and a space. Thanks to SaviourSelf
System.out.println( EarliestCommonSlot(hotel,band) );
请从数组中的字符串中删除空格。这可以通过 st.replaceAll\\s+和st.replaceAll\\s 错误本身表明143在前缀中有空格。 线程main java.lang.NumberFormatException中的异常:对于输入字符串:143使用修剪方法
public class Test {
public static void main(String[] args) {
String s1 = "[143, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 157, 158, 159, 162, 163, 164]";
String s2 = "[20, 35, 74, 78, 124, 125, 126, 127, 131, 132, 143, 144, 145, 146]";
s1 = s1.replace("[","");
s1 = s1.replace("]","");
s1 = s1.replace(" ","");
String [] band = s1.split(",");
s2 = s2.replace("[","");
s2 = s2.replace("]","");
String [] hotel = s1.split(",");
System.out.println( EarliestCommonSlot(hotel,band) );
}
public static int EarliestCommonSlot(String [] a1, String [] b1){
int i=0,j=0;
int common = -1;
int [] a = new int [a1.length];
int [] b = new int [b1.length];
for(i = 0;i < a1.length;i++)
{
a[i] = Integer.parseInt( a1[i]);
System.out.println(a1[i]);
}
for(i = 0;i < b1.length;i++)
{
b[i] = Integer.parseInt( b1[i]);
System.out.println(b1[i]);
}
i = 0; j=0;
while ( i< a.length && j < b.length){
if ( a[i] == b[j] ){
common = a[i]; break;
}
if ( a[i] < b[j] ){
i++;
}
else j++;
}
return common;
}
}
String s1 = "[143, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 157, 158, 159, 162, 163, 164]";
String s2 = "[20, 35, 74, 78, 124, 125, 126, 127, 131, 132, 143, 144, 145, 146]";
s1 = s1.replace("[","");
s1 = s1.replace("]","");
String [] band = s1.split(",");
s2 = s2.replace("[","");
s2 = s2.replace("]","");
String [] hotel = s1.split(",");
System.out.println( EarliestCommonSlot(hotel,band) );
public static int EarliestCommonSlot(String [] a1, String [] b1){
int i=0,j=0;
int common = -1;
int [] a = new int [a1.length];
int [] b = new int [b1.length];
for(i = 0;i < a1.length;i++)
{
a[i] = Integer.parseInt( a1[i].trim());
System.out.println(a1[i]);
}
for(i = 0;i < b1.length;i++)
{
b[i] = Integer.parseInt( b1[i].trim());
System.out.println(b1[i]);
}
i = 0; j=0;
while ( i< a.length && j < b.length){
if ( a[i] == b[j] ){
common = a[i]; break;
}
if ( a[i] < b[j] ){
i++;
}
else j++;
}
return common;
}
我已经运行了这个方法,它起作用了前导空间就是问题所在。尝试Integer.parseInt a1[i].trim;或者确保分割的字符串不包含任何空格。@NUO使用.trim删除字符串的前导空格和尾随空格。@NUO必须使用replace],;用于字符串s1和s2以及两个括号。数字由逗号和空格分隔。所有整数前面都会有一个空格,而不仅仅是第一个。@SaviourSelf谢谢。我没注意到。编辑它。