1

I am trying to parse a string to integer but I get incompatible types. My counter is for the amount of records that are in the file, the program can append to the file and read from it. now I want to sort it and write it back to file. How can I perform this task? variables that are not declared here are dclared globally.

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]);
        }
    }
Sebastian Redl
  • 69,373
  • 8
  • 123
  • 157

4 Answers4

0

Declare:

  int[] tmprooms;
  int[] tmprooms1;

as

  int tmprooms;
  int tmprooms1;

Integer#parseInt() returns an int , not an int[] .

AllTooSir
  • 48,828
  • 16
  • 130
  • 164
  • Thanks guys, I forgot to mention 3 string arrays and I want to sort on number_rooms then write back to the file. Number_rooms declared Globally – Paul Dillon Jun 13 '13 at 12:45
0

try this

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]);
            }
        }
shreyansh jogi
  • 2,082
  • 12
  • 20
0

Integer.parseInt() returns an int (which can be auto-boxed to an Integer), not an int[] array.

The variable tmprooms is of the type int[], but Integer.parseInt() does not return an int[] array.

The following can be used to sort an int[] array:

Arrays.sort(int [])

Please see the following: Arrays#sort(int[])

blackpanther
  • 10,998
  • 11
  • 48
  • 78
  • It returns an `int`; `Integer.valueOf` returns an `Integer`. – arshajii Jun 13 '13 at 12:48
  • number_rooms has numbers in it and I want to sort it, how can I do that then please? There are 3 arrays in the file – Paul Dillon Jun 13 '13 at 12:54
  • thanks, I have 3 arrays in the same file. I think that sort will only sort on one – Paul Dillon Jun 13 '13 at 13:00
  • You may want to combine or merge the three int arrays and in this canse you may want to look at the following reference: http://stackoverflow.com/questions/4697255/combine-two-integer-arrays – blackpanther Jun 13 '13 at 13:06
  • blackpanther, The 3 arrays are stored as strings in a .txt file and I think that the best sort to do would be on number_rooms, thats why Im trying to sort it but I also want the rest of the arrays to be in sync – Paul Dillon Jun 13 '13 at 13:16
0

A few things:

  • tmprooms and tmprooms1 need to be int's, not int[]'s.
  • j needs to start from 0.
  • You need to use j, not counter for array accesses.
  • The System.out.println where it is doesn't really print anything meaningful.
  • number_rooms[counter+1] = tnumber; should be street_number[counter+1] = tnumber;
  • This appears to be Bubble sort, just without stopping early (thus even less efficient than the already inefficient algorithm). There are far better algorithms out there, like the one used by Arrays.sort (see below).

Final code:

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

Test.

A better way using objects: (allowing you to use 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));
}
Bernhard Barker
  • 54,589
  • 14
  • 104
  • 138
  • Hi Dukeling, Why would there be an arror after the String in the statement: Address(String streetNumber, String streetName, int numberOfRooms)? It say it was wxpecting a )? Thanks again. – Paul Dillon Jun 14 '13 at 16:45
  • I've tried both sections and the first one is no giving number format exception at tmprooms1 = Integer.parseInt(number_rooms[j + 1]); I thought that porsing the string would overcome that but it has not. thanks again for your input so far. Im extremely new so the inefficiency of the code is why Dukeling – Paul Dillon Jun 14 '13 at 17:16
  • For the second piece of code - It shouldn't say that. That code should run as is. Feel free to put the code [somewhere](http://ideone.com) and I can take a look. A `NumberFormatException` means the string does not contain a parsable integer. The string should contain **only** an integer, nothing else. Try printing the string out and seeing what it looks like. – Bernhard Barker Jun 14 '13 at 17:30
  • Cant seem to be able to paste the code here. but the error is at the first string of Address: -Address(String streetName, String streetNumber, int numberOfRooms) – Paul Dillon Jun 14 '13 at 18:46
  • Ive put it at your somewhere – Paul Dillon Jun 14 '13 at 20:03
  • You need to give me a link to it as well. – Bernhard Barker Jun 14 '13 at 21:10
  • The `NumberFormatException` means the string does not contain a parsable integer. The string should contain only an integer, nothing else. I can't really help you, unless you provide a **complete** example (including the input of the program) that reproduces the problem - you can try printing the string out and seeing what it looks like, and which one gives you a problem, like [this](http://ideone.com/taiY2s). – Bernhard Barker Jun 15 '13 at 21:10
  • Thanks Dukeling, The string array has only numbers so I wanted to sort the records by numbers, parsing number_rooms inro tmprooms and tmprooms1 as per the coding above. – Paul Dillon Jun 15 '13 at 21:43
  • the records in the file: 12b Tetra Mews 2 45 Wellington Avenue 1 100 Camden Road 5 45 Kilburn High Street 3 34 College Row 1 – Paul Dillon Jun 15 '13 at 21:49