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]);
        }
    }
1
  • Fixed the formatting for you. Commented Jun 13, 2013 at 12:41

4 Answers 4

0

Declare:

  int[] tmprooms;
  int[] tmprooms1;

as

  int tmprooms;
  int tmprooms1;

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

Sign up to request clarification or add additional context in comments.

1 Comment

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

2 Comments

the response was inconvertable types--- tmprooms = (Integer)number_rooms[counter];
Hi shreyansh jogi, I now get number format excption at the tmprooms1 line.
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[])

5 Comments

It returns an int; Integer.valueOf returns an Integer.
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
thanks, I have 3 arrays in the same file. I think that sort will only sort on one
You may want to combine or merge the three int arrays and in this canse you may want to look at the following reference: stackoverflow.com/questions/4697255/combine-two-integer-arrays
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
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));
}

9 Comments

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.
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
For the second piece of code - It shouldn't say that. That code should run as is. Feel free to put the code somewhere 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.
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)
Ive put it at your somewhere
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.