1

我正在尝试将字符串解析为整数,但我得到不兼容的类型。我的计数器是针对文件中的记录数量,程序可以附加到文件并从中读取。现在我想对其进行排序并将其写回文件。我该如何执行此任务?此处未声明的变量是全局声明的。

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]);
        }
    }
4

4 回答 4

0

宣布:

  int[] tmprooms;
  int[] tmprooms1;

作为

  int tmprooms;
  int tmprooms1;

Integer#parseInt()返回一个int,而不是一个int[]

于 2013-06-13T12:43:29.050 回答
0

一些东西:

  • tmprooms并且tmprooms1需要是int's,而不是int[]'s。
  • 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));
}
于 2013-06-13T15:41:02.047 回答
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]);
            }
        }
于 2013-06-13T12:44:08.893 回答
0

Integer.parseInt()返回一个int(可以自动装箱到一个Integer),而不是一个int[]数组。

变量tmprooms的类型为int[],但Integer.parseInt()不返回int[]数组。

以下可用于对int[]数组进行排序:

Arrays.sort(int [])

请参阅以下内容:Arrays#sort(int[])

于 2013-06-13T12:46:09.423 回答