Click to See Complete Forum and Search --> : array required, but int found


Debindigo
04-22-2005, 06:26 PM
Hi there, I'm new to the list and am a newbie in the Java language. I have no previous language experience, so will be referring to this list often.
I am trying to build a program where I am sorting Student Names and Test scores in arrays. I keep getting the following message "array required, but int found" and would like to know what that means.
Any help would be appreciated.

Khalid Ali
04-22-2005, 06:52 PM
it means that youare trying access or assing a variable a value which compiler things should be an array but its an int,
Show us the code

Debindigo
04-22-2005, 10:04 PM
my code is as follows:
import keyboardInput.*;

public class SortNumerical
{//start of class
static final int SIZE = 20;

public static void main(String args[])
{//start of main
Keyboard in = new Keyboard();

int [] score = new int [SIZE]; //lists scores for students
String [] student = new String [SIZE]; //lists students name
String name;
int number = 0;
int index = 0;
int count = 0;
char answer;
boolean found = true;
int numpairs;
double hold;
boolean inorder;
numpairs = SIZE - 1;
String student_hold;
double score_hold;
String temp;

System.out.print("How many students are to be entered? ");
number = in.readInt();

//load the arrays
for (int i = 0; i < SIZE; i++)
{
System.out.print("Enter Student name " + (i+1));
student [i] = in.readString();

System.out.print("Enter Student mark " + (i + 1));
score [i] = in.readInt();
count++;

System.out.print("Another student to be added...press y to continue");
System.out.print("Press n to stop");
answer = in.readChar();
if (answer == 'n')
break;
}
for (int i = 0; i < count; i++)
{
System.out.println(i + " " + student[i] + "\t" + score[i]);

for (int j = 0; j < numpairs; j++)
{
inorder = true;
for (int a = 0; a < numpairs; a++)
{
if (SIZE[i] > SIZE [i + 1] )
{

temp.compareTo (student_hold);
SIZE[i+1] = SIZE[i];
SIZE[i]=temp;

hold = score_hold;
SIZE[i+1] = SIZE[i];
SIZE[i]=hold;


inorder = false;
}
}
if (inorder == true)
{
j = numpairs;
}
}
}

for (int k = 0; k < SIZE; k++)
{
System.out.println(k + "" + student[k] + "\t" + score[k]);
}
}
}

Khalid Ali
04-23-2005, 01:37 AM
my code is as follows:
static final int SIZE = 20;
if (SIZE[i] > SIZE [i + 1] )
{

temp.compareTo (student_hold);
SIZE[i+1] = SIZE[i];
SIZE[i]=temp;

hold = score_hold;
SIZE[i+1] = SIZE[i];
SIZE[i]=hold;


inorder = false;
}
}

for (int k = 0; k < SIZE; k++)
}

I hope you can see now,
You declare a static variable SIZE which is of type int, then if you see I have color coded in blue, you are using that variable as an array...
Everything in red is int variable and blue is being accessed as array

Debindigo
04-23-2005, 08:02 PM
Thanks for the help. I changed the sorting array as follows but now I get this message:

Deb's stuff\School\java\SortNumerical.java:55: operator && cannot be applied to int,boolean
if ((student[a]).compareTo (student [a + 1] ) && (score[a] > score[a+1]))

Here is the bit of code that I changed. I have tried to make [a] double and have tried to change my variable to double, but it gives me many other errors when I do that.

for (int i = 0; i < count; i++)
{
System.out.println(i + " " + student[i] + "\t" + score[i]);

for (int j = 0; j < numpairs; j++)
{
inorder = true;
for (int a = 0; a < numpairs; a++)
{
if ((student[a]).compareTo (student [a + 1] ) && (score[a] > score[a+1]))
{

temp.compareTo (student_hold);
student[i+1] = student[i];
student[a]=temp;

hold = score_hold;
score[i+1] = score[i];
score[i]=hold;


inorder = false;
}

if (inorder == true)
{
j = numpairs;
}

Debindigo
04-23-2005, 08:10 PM
forget it...I think I fixed it by separating the two and making them seperate quotes.
This is the next message I get and this one has me very stumped considering that student[] is a String:
C:\WINDOWS\Desktop\Deb's stuff\School\java\SortNumerical.java:55: incompatible types
found : int
required: boolean

This is my new code:
import keyboardInput.*;

public class SortNumerical
{//start of class
static final int SIZE = 20;

public static void main(String args[])
{//start of main
Keyboard in = new Keyboard();

double [] score = new double [SIZE]; //lists scores for students
String [] student = new String [SIZE]; //lists students name
String name;
double number = 0;
double index = 0;
double count = 0;
char answer;
boolean found = true;
int numpairs;
double hold;
boolean inorder;
numpairs = SIZE - 1;
String student_hold;
double score_hold;
String temp;

System.out.print("How many students are to be entered? ");
number = in.readInt();

//load the arrays
for (int i = 0; i < SIZE; i++)
{
System.out.print("Enter Student name " + (i+1));
student [i] = in.readString();

System.out.print("Enter Student mark " + (i + 1));
score [i] = in.readInt();
count++;

System.out.print("Another student to be added...press y to continue");
System.out.print("Press n to stop");
answer = in.readChar();
if (answer == 'n')
break;
}
for (int i = 0; i < count; i++)
{
System.out.println(i + " " + student[i] + "\t" + score[i]);

for (int j = 0; j < numpairs; j++)
{
inorder = true;
for (int a = 0; a < numpairs; a++)
{
if (student[a].compareTo (student [a + 1]) )
{

temp.compareTo (student_hold);
student[i+1] = student[i];
student[a]=temp;
}
if (score[a] > score[a+1])
{
hold = score_hold;
score[i+1] = score[i];
score[i]=hold;


inorder = false;
}

if (inorder == true)
{
j = numpairs;
}
}


for (int k = 0; k < SIZE; k++)
{
System.out.println(k + "" + student[k] + "\t" + score[k]);
}
}
}
}
}
if (student[a].compareTo (student [a + 1]) )

Debindigo
04-23-2005, 08:19 PM
In response to my last post, the line that is confusing me is:

if (student[a].compareTo (student [a + 1]))

The error I am getting is as follows:
C:\WINDOWS\Desktop\Deb's stuff\School\java\SortNumerical.java:55: incompatible types
found : int
required: boolean
if (student[a].compareTo (student [a + 1]) )

ray326
04-23-2005, 11:25 PM
compareTo() returns an int representing >, <, ==. What if () is looking for is a boolean, true or false. What are you doing with the test, comparing for equality or compare for a particular inequality? If the former it's compareTo() == 0, which would probably be better done with equals(). If the latter then it's compareTo() > 0 or compareTo() < 0.

Debindigo
04-24-2005, 02:12 PM
Thanks Ray, what I am trying to do is sort a student list, therefore, I need to make the student[] and the score[] jell.
My code is above, and I really don't understand what is happening. I tried using your idea and made it "<0" but then I get an error saying that my String student_hold, String temp and double score_hold may not have been initialized, but they are in the main method. I have been working on this for so long and every time I try something new to resolve a current problem, I end up with a whole new lot of problems. Any ideas?

ray326
04-24-2005, 06:12 PM
Well I wouldn't use two arrays. Actually I wouldn't use arrays at all, I'd use one of the Collections and I'd use a class like so for the student/score.

public class Student
{
protected String name;
protected double score;

public Student()
{
super();
}
public Student(String aName, double aScore)
{
super();
name = aName;
score = aScore;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public double getScore()
{
return score;
}
public void setScore(double score)
{
this.score = score;
}
}

Since your order seems to be by score, that's how your compareTo() would work.

int compareTo(Student s)
{
if (this.getScore() == s.getScore()) return 0;
if (this.getScore() > s.getScore()) return 1;
if (this.getScore() < s.getScore()) return -1;
}

Changing the sense of the inequality tests will change the direction of the sort.