Reputation: 1
So what I'm trying to do with this program is take two arrays, and then merge them into a third array. The main method takes the data from a specified file and then creates arrays for each line, and there are only two lines. The files is formatted like this:
11 -5 -4 -3 -2 -1 6 7 8 9 10 11
8 -33 -22 -11 44 55 66 77 88
After the arrays are created, I try to call the merge method I've created but I get this error from Netbeans:
'.class' expected
unexpected type
required: value
found: class
and I don't really know what that means, and Netbeans tries fixing it by creating other classes. All of my code is below:
import java.io.*;
import java.util.*;
public class ArrayMerge
{
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
File inputDataFile = new File(args[0]);
try
{
Scanner inputFile = new Scanner(inputDataFile);
Scanner keyboardin = new Scanner(System.in);
int aSize = inputFile.nextInt();
int a[] = new int[aSize];
for (int i= 0 ; i<aSize; i++ )
{
a[i] = inputFile.nextInt();
}
int bSize = inputFile.nextInt();
int b[] = new int[bSize];
for (int j = 0; j < bSize; j++)
{
b[j] = inputFile.nextInt();
}
int c[] = ArrayMerge.merge(a, b);
System.out.println(Arrays.toString(c));
}
catch(FileNotFoundException e)
{
System.err.println("FileNotFoundException: " + e.getMessage());
}
}
public static int[] merge(int[] a, int[] b)
{
// merge 2 sorted arrays
int[] c = new int[a.length + b.length];
int i=0, j=0, k=0;
while (i<a.length && j<b.length) {
c[k++] = (a[i]<b[j]? a[i++]: b[j++]);
} // end while
while (j<b.length) {
c[k++] = b[j++];
} // end while
while (i<a.length) {
c[k++] = a[i++];
} // end while
return c;
} // end merge()
}
Update: I solved my own issue. I forgot that arrays aren't called by their name and brackets, but merely the variable name. After realizing this, I figured out that I had to move the line that had the error into the Try{} block. I've updated the code above to reflect these changes.
Upvotes: 0
Views: 56
Reputation: 425073
Your merge()
method is declared outside the class.
Move the closing curly bracket of the class from above the merge()
method to below it.
Upvotes: 1