0

Sorry, I'm a Java noob. Any idea why my code isn't compiling? I would like it to work such that I can enter two arrays at the command line, and have them used by the "findNa" method. If elements in the array match I would like NA to be printed on a new line, if there is no match I would like the original value in "array" to be printed. Thanks in advance!

  class naFam {
    static void findNa(String[] array, String[] lookupArray) {
        int i;
        int j;
        for (i = 0; i < array.length; i++) {
            for (j = 0; j < lookupArray.length; j++) {
                if (array[i] == lookupArray[j]) {
                    System.out.println("NA");
                } else {
                    System.out.println(array[i]);
                }
            }
        }
    }
    public static void main(String args[]) {
        String[] array = args[0];
        String[] lookupArray = args[1];
        findNa(array, lookupArray);

    }
}
Amit.rk3
  • 2,417
  • 2
  • 10
  • 16
  • `args[0]` is a `String` not a `String[]` (String array). Same goes for `args[1]` – tim_yates Jul 07 '15 at 21:42
  • 3
    For future reference, when your code doesn't compile, it will say why and point to that location in the code. If that doesn't help you track down the error, at least post the why and where with your question so that we can help you out. – Teepeemm Jul 07 '15 at 21:44
  • Thank you. Okay, but my method "findNa" requires an array. Is there anyway to input an array from the command line? – Steven Lang Jul 07 '15 at 21:50

4 Answers4

4

you are trying to assign String to String[]

String[] array = args[0];
String[] lookupArray = args[1];
Amit.rk3
  • 2,417
  • 2
  • 10
  • 16
  • @StevenLang : Not really sure what you want to do in the code. But I can only understand your method expects String[] , and you are passing String. There seems to be some issue with your method design and the logic you want to write. – Amit.rk3 Jul 07 '15 at 21:50
3
String[] array = args[0];

You are setting an array to be equal to a string (args[0] returns the String which has the index of 0 inside the array).

Same here:

String[] lookupArray = args[1];

If you want to input 2 arrays and then check if they're equal try something like this:

import java.util.Scanner;

class naFam {
    static void findNa(String[] array, String[] lookupArray) {
        boolean equal = true;
        for (int i = 0; i < array.length; i++) {
            if (array[i]!=lookupArray[i]) {
                equal=false;
            }
        }
        if (equal==true) {
             System.out.println("NA");
        } else {
            System.out.println(array[0]);
        }
    }

    public static void main(String[] args) {
        //Create a new scanner
        Scanner scan = new Scanner(System.in);
        //Create the integer for the size of the array they want to input
        int size1;
        //Input the size
        size1 = scan.nextInt();
        //Create and input the arrays
        String[] arrayOne = new String[size1];
        String[] arrayTwo = new String[size1];

        for (int i = 0; i < size1; i++) {
            arrayOne[i] = scan.next();
        }
        for (int i = 0; i < size1; i++) {
            arrayTwo[i] = scan.next();
        }
        findNa(arrayOne,arrayTwo);
    }
}

Haven't tested it yet but it should work.

Tom
  • 16,842
  • 17
  • 45
  • 54
BlerpyDude
  • 71
  • 5
0

You can do this way:

String [] array = new String []{args[0]};
String [] lookupArray = new String [] {args[1]};

But stil you don't i'll have what you want, but two arrays of a single object. You have to put more details of how you will pass this array as parameters.

You can pass the size of the arrays as parameters, and construct the array, like:

class naFam {
    static void findNa(String[] array, String[] lookupArray) {
        boolean na = false;
        for (int i = 0; i < array.length; i++) {
            if (!na) {
                for (int j = 0; j < lookupArray.length; j++) {
                    if (array[i] == lookupArray[j]) {
                        System.out.println("NA");
                        na = true;
                    }
                } 
            }
        }
        if (!na) {
            print(array);
        }

    }

    private static void print(String[] array) {
        for (String s : array) {
            System.out.println(s);
        }
    }

    public static void main(String args[]) {
        //simulate user argument pass
        args = new String[]{"2", "3", "x", "x", "y", "y", "x"};
        int arraySize = Integer.valueOf(args[0]);
        int lookupArraySize = Integer.valueOf(args[1]);

        final int parametersSize = 2;
        String[] array = buildUp(args, parametersSize, arraySize);
        String[] lookupArray = buildUp(args, parametersSize + arraySize, lookupArraySize);

        findNa(array, lookupArray);
    }

    private static String[] buildUp(String[] args, int startPosition, int arraySize) {
        String[] newArray = new String[arraySize];
        int x = 0;
        for (int i = startPosition; i < startPosition + arraySize; i++) {
            newArray[x++] = args[i];
        }
        return newArray;
    }

}
alexpfx
  • 6,412
  • 12
  • 52
  • 88
  • Okay, the goal of the program is to begin with the first index of the array "array". Then, compare it to every index in the array "lookupArray". If there is a match NA is to be printed. If there is no match, the current index of "array" should be printed. – Steven Lang Jul 07 '15 at 21:58
0

The compiler is yelling at you because you are trying to convert a String to an array of Strings.

args is an array of strings that you pass into main from the command line. When you call args[0], you are accessing the first string in that array. You cannot assign String to String[].

It sounds like you need to have your program ask for the user to input the arrays. Check out something like this: How to use readline() method in Java?

Community
  • 1
  • 1
markreed49
  • 41
  • 7