我正在尝试将字符串解析为整数但是我得到了不兼容的类型。我的计数器是针对文件中的记录数量,程序可以附加到文件并从中读取。现在我想对它进行排序并将其写回文件。我该如何执行此任务?未在此声明的变量是全局dclared。
public static void sort_addresses() throws IOException
{
String tnumber;
String tname;
String tnrooms;
int[] tmprooms;
int[] tmprooms1;
int j;
for (int i = 0; i < counter; i++)
{
for(j = 1; j < (counter-1); j++)
//while (street_name[counter] != null)
{
tmprooms = Integer.parseInt (number_rooms[counter]);
tmprooms1 = Integer.parseInt (number_rooms[counter+1]);
if (tmprooms[i] > tmprooms1[i+1])
{
tnumber = street_number[counter];
tname = street_name[counter];
tnrooms = number_rooms[counter];
street_number[counter] = street_number[counter +1];
street_name[counter] = street_name[counter+1];
number_rooms[counter] = number_rooms[counter+1];
number_rooms[counter+1] = tnumber ;
street_name[counter+1] = tname;
number_rooms[counter+1] = tnrooms;
System.out.println(street_number[i]+"\t"+street_name[i]
+"\t"+number_rooms[i]);
}
}
答案 0 :(得分:0)
宣告:
int[] tmprooms;
int[] tmprooms1;
as
int tmprooms;
int tmprooms1;
Integer#parseInt()会返回int
,而不是int[]
。
答案 1 :(得分:0)
试试这个
public static void sort_addresses() throws IOException
{
String tnumber;
String tname;
String tnrooms;
int tmprooms;
int tmprooms1;
int j;
for (int i = 0; i < counter; i++)
{
for(j = 1; j < (counter-1); j++)
//while (street_name[counter] != null)
{
tmprooms = (Integer)number_rooms[counter];
tmprooms1 = (Integer)number_rooms[counter+1];
if (tmprooms[i] > tmprooms1[i+1])
{
tnumber = street_number[counter];
tname = street_name[counter];
tnrooms = number_rooms[counter];
street_number[counter] = street_number[counter +1];
street_name[counter] = street_name[counter+1];
number_rooms[counter] = number_rooms[counter+1];
number_rooms[counter+1] = tnumber ;
street_name[counter+1] = tname;
number_rooms[counter+1] = tnrooms;
System.out.println(street_number[i]+"\t"+street_name[i]
+"\t"+number_rooms[i]);
}
}
答案 2 :(得分:0)
Integer.parseInt()
会返回int
(可以自动装箱到Integer
),而不是int[]
数组。
变量tmprooms
的类型为int[]
,但Integer.parseInt()
不会返回int[]
数组。
以下内容可用于对int[]
数组进行排序:
Arrays.sort(int [])
请参阅以下内容:Arrays#sort(int[])
答案 3 :(得分:0)
一些事情:
tmprooms
和tmprooms1
需要int
,而不是int[]
。j
需要从0
开始。j
,而不是counter
进行数组访问。System.out.println
并没有真正打印任何有意义的内容。number_rooms[counter+1] = tnumber;
应为street_number[counter+1] = tnumber;
Arrays.sort
使用的算法(见下文)。最终代码:
public static void sort_addresses()
{
String snumber, sname, tnrooms;
int tmprooms, tmprooms1;
for (int i = 0; i < counter; i++)
{
for (int j = 0; j < counter - 1; j++)
{
tmprooms = Integer.parseInt(number_rooms[j]);
tmprooms1 = Integer.parseInt(number_rooms[j + 1]);
if (tmprooms > tmprooms1)
{
snumber = street_number[j];
sname = street_name[j];
tnrooms = number_rooms[j];
street_number[j] = street_number[j + 1];
street_name[j] = street_name[j + 1];
number_rooms[j] = number_rooms[j + 1];
street_number[j + 1] = snumber;
street_name[j + 1] = sname;
number_rooms[j + 1] = tnrooms;
}
}
}
}
使用对象的更好方法:(允许您使用Arrays.sort
)
static class Address
{
int numberOfRooms;
String streetNumber;
String streetName;
Address(String streetName, String streetNumber, int numberOfRooms)
{
this.numberOfRooms = numberOfRooms;
this.streetName = streetName;
this.streetNumber = streetNumber;
}
@Override
public String toString()
{
return numberOfRooms + ":" + streetName + ":" + streetNumber;
}
// an alternative is to have the class "implements Comparable<Address>"
// and have a "public int compareTo(Address o)" function
// then you can just say "Arrays.sort(addresses)"
public static Comparator<Address> numberOfRoomsComparator
= new Comparator<Address>() {
@Override
public int compare(Address o1, Address o2)
{
return Integer.valueOf(o1.numberOfRooms).compareTo(o2.numberOfRooms);
}
};
}
static Address[] addresses = {new Address("u1", "a1", 3),
new Address("u2", "a2", 1),
new Address("u3", "a3", 5),
new Address("u4", "a4", 4),
new Address("u5", "a5", 2)};
public static void main(String[] args)
{
Arrays.sort(addresses, Address.numberOfRoomsComparator);
System.out.println(Arrays.toString(addresses));
}