View Full Version : Help with compiler errors
chevanater
12-01-2006, 04:47 PM
I'm writing this program to insert an array into another array in numerical order. This is what I have but it is giving me compiler errors. Anyone have any ideas or suggestions? Thanks
public class InsertArray
{
public static void main(String[] args)
{
int a [5] = {14, 5, 7, 12, 8}
int b [a.length]
for (i=0; i<5; i++)
{
insert (a[i]);
}
public void insert (int x)
{
for (i=0; i<5; i++)
{
if(b[i] = 0)
b[i] = x;
return;
else if x < b[i]
int temp = b[i];
b[i] = x;
insert[temp];
}
}
}
}
Fatmumuhomer
12-01-2006, 07:12 PM
Well, for one, I think you need braces {} after your if's and else if's:
if(b[i] = 0)
{
b[i] = x;
return;
}
else if x < b[i]
{
int temp = b[i];
b[i] = x;
insert[temp];
}
You also need to remove the code for your insert function from main. It should begin after the } of main.
Aradon
12-01-2006, 07:15 PM
Wow. Okay.
First off let me suggest that you start back at the beginning of methods with java before continuing forward. You've got sort of the right idea but you've got it in the wrong places in your code.
Your code formatted correctly:
1 public class InsertArray
2 {
3 public static void main(String[] args)
4 {
5
6 int a [5] = {14, 5, 7, 12, 8}
7 int b [a.length]
8
9 for (i=0; i<5; i++)
10 {
11 insert (a[i]);
12 }
13
14 public void insert (int x)
15 {
16 for (i=0; i<5; i++)
17 {
18 if(b[i] = 0)
19 b[i] = x;
20 return;
21
22 else if x < b[i]
23 int temp = b[i];
24 b[i] = x;
25 insert[temp];
26
27 }
28 }
29 }
30 }
See how everytime I open a new block either through a method, a class, a for loop or a while loop I indent the next block over? This is a way to figure out what is going on in my code visually. Now first, a few basics.
Let's go down your code lines as ordered above. First off, all statements that aren't conditional statements such as if, else, while, or methods such as public static void main (String args[]) should be ended with a semicolon
So on line 6 it should look like
int a [5] = {14, 5, 7, 12, 8};
Secondly, in java you cannot write a method within a method. There are a few exceptions to this that come into most play when you are writing threads or gui's but at this point in your java career I wouldn't worry about it.
So when you write the method insert, you have to stick it outside of your public static void main. But inside the public class.
Thirdly, all if statements must have ( )'s around them. so your statement
else if x < b[i]
should be
else if (x < b[i])
Now I'm sure you're asking the question, Aradon, how did you know all that? Well for me I just read through it but for a more beginner programmer the solution is in the compiler errors.
Starting from the first error and going down, each error gives you a hint about what is going on.
To fix the errors, you would need to note the line numbers and match them with the corresponding lines in the program. Each line number is listed after the Program name. So one error may be:
InsertArray.java:7: missing or expected ;
In the case of semicolon's it points to the line below it (all other error messages will point to the right line) so we now know that on line 6 we are missing a semicolon.
What other compiler errors do you see? Looking at my formatting and reading through what I wrote what other problems have you run into after trying to fix it?
-Aradon
Fatmumuhomer
12-01-2006, 08:46 PM
I didn't even notice the missing semicolons! Yeah, do everything Aradon said!
chevanater
12-04-2006, 05:18 PM
thanks for the help guys :thumbsup:
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.