'unexpected type' error
///////////////////////////////////////////////
publicstaticvoid sortBySymbol(Stock[] stocks,int high){
if (high > 1){
// Find the largest number and its index
int indexOfMax = 0;
String max = stocks[0].getSymbol();
for (int i=0; i<high; i++){
if (stocks[i].getSymbol().compareToIgnoreCase(max) > 0){
//... Exchange elements
max = stocks[i].getSymbol();
indexOfMax = i;
}
}
// Swap the largest with the last number in the list
*********************************stocks[indexOfMax].getSymbol() = stocks[high].getSymbol();
stocks[high].getSymbol() = max;
*********************************
// Sort the remaining list
sortBySymbol(stocks, high - 1);
}
for (int i = 0; i < stocks.length; i++)
System.out.println(stocks[i]);
}
//////////////////////////////////////////////
I put *'s around the two lines of code that have the error for 'unexpected type'. Can anyone give me more information about this error and what I could be doing wrong? I am trying to run a selection sort, and itmust be selection sort, but the last part is giving me an error.Please, any help will be greatly appreciated.
Message was edited by:
Atria34
[2073 byte] By [
Atria34a] at [2007-11-26 17:51:19]

I might have answered you had it not been that you chose to rudely crosspost it. http://forum.java.sun.com/thread.jspa?threadID=5134681
I assume the getSymbol method returns a value. Do you need a setSymbol method or are you attempting to assign to max?
> I might have answered you had it not been that you> chose to rudely crosspost it.> http://forum.java.sun.com/thread.jspa?threadID=5134681Sorry, I thought I posted it in the wrong forum the first time
> I assume the getSymbol method returns a value. Do you
> need a setSymbol method or are you attempting to
> assign to max?
The program is sorting lines in a file, lets say the file contains these 3 lines:
SUNWSun Microsystems5400$ 3.96$ 4.12+4.04%
RHATRed Hat 2500$11.28$11.40+1.06%
MSFTMicrosoft9075$24.62$25.10+1.95%
The getSymbol method will return the values:
SUNW
RHAT
MSFT
So that you can then sort them using the compareToIgnoreCase to
MSFT
RHAT
SUNW
If I write: System.out.println(stocks[indexOfMax].getSymbol()); it will display:
SUNW
SUNW
SUNW
SUNW
SUNW
SUNW
SUNW
And if I write: System.out.println(stocks[high].getSymbol()); it will display:
BORL
ERIS
CSCO
EPQ
IBM
AAPL
MSFT
So both display a small string of text so I don't understand why I can't set the first one to the second, and why it says 'unexpected type'
Can you tell me why this is an error? If so can you see why your code is also wrong?4 + 7 = sum;
> Can you tell me why this is an error? If so can you
> see why your code is also wrong?
>
> > 4 + 7 = sum;
>
In that case, you'd have to write sum = 4 + 7
I understand that, but switching the order does not fix the problem for me. Is there anyway I can e-mail you or post what I have so you can see the entire program so far?
As I said in reply #2 getSymbol() RETURNS a value, it doesn't accept a value and even if it did you don't do it that way. How to you pass values when you call a method?
http://www.fileden.com/files/2006/11/1/339364/atria34.zip
StockTest is the file that contains the problem.
Ok, so..
"As I said in reply #2 getSymbol() RETURNS a value, it doesn't accept a value and even if it did you don't do it that way. How to you pass values when you call a method? "
Ok so I shouldn't be trying to say "stocks[indexOfMax].getSymbol() = stocks[high].getSymbol()"
Because if I was supposed to send it that value, I'd do this:
stocks[indexOfMax].getSymbol( stocks[high].getSymbol())
But I don't think I'm even supposed to do that. Should I be making new variables that contain those values and then switch them?
stocks[indexOfMax].getSymbol( stocks[high].getSymbol())I don't know what you are trying to achieve but this appears to be correct syntax. All you need to figure out now is are you calling the correct methods on the correct objects and passing the correct values.
ya wat i just posted last was wrong but try flipping around max to the front.Message was edited by: stephensk8s
> stocks[indexOfMax].getSymbol(
> stocks[high].getSymbol())
> I don't know what you are trying to achieve but this
> appears to be correct syntax. All you need to figure
> out now is are you calling the correct methods on the
> correct objects and passing the correct values.
That doesn't work. It's just supposed to sort the list. That part of the selection sort is supposed to switch the largest number with the last one, to start putting them in order.
So it is doing:
Index of the Highest number = last place value
Index of last place = highest number
max shouldn't be first because you are setting that value to the last place in the array. stocks[] is the array. high is the last place in the array. so stocks[array] is the last last place in the stocks array. .getSymbol() is used to truncate the entire string to just the symbol, or SUNW for example, in order sort them.
Nobody?I attached all the files in a .zip up a few posts (atria34.zip) the file is StockTest.java so that you can try it yourself to see what I mean, and realize that your solutions aren't working before posting themMessage was edited by: Atria34
// Swap the largest with the last number in the list
Stock tmp = stocks[indexOfMax];
stocks[indexOfMax] = stocks[high];
stocks[high] = tmp;
Your swapping was just a little messed. The Objects in the array had to be swapped.
Also, I don't quite see why you need to store the String max... but it's fixed as far as compilation is concerned.
That did fix the compilation error, but it did not sort it correctly, so that's not the right solution.The string max allows you to leave out the temp variable, so there IS a way to do it without a temp variable, and that's what I'm supposed to do.
public static void sortBySymbol(Stock[] stocks)
{
for (int i = stocks.length - 1; i >= 1; i--)
{
Stock currentMax = stocks[0];
int currentMaxIndex = 0;
for (int j = 1; j <= i; j++)
{
if (currentMax.getSymbol().compareToIgnoreCase(stocks[j].getSymbol()) < 0)
{
currentMax = stocks[j];
currentMaxIndex = j;
}
}
if (currentMaxIndex != i)
{
stocks[currentMaxIndex] = stocks[i];
stocks[i] = currentMax;
}
}
}
That's it. I figured it out, but seriously thanks everyone who replied.