Java的ARGS不兼容的类型 [英] Java args Incompatible types

查看:104
本文介绍了Java的ARGS不兼容的类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对不起,我是一个Java小白。任何想法,为什么我的code未编制?
我想它的工作,这样我可以在命令行中输入两个数组,并让他们使用的findNa的方法。如果数组元素的比赛,我想NA要打印一个新行,如果没有比赛,我想在阵的原始值进行打印。
在此先感谢!

 类naFam {
    静态无效findNa(String []数组,字符串[] lookupArray){
        INT I;
        诠释J;
        对于(i = 0; I< array.length,我++){
            为(J = 0; J< lookupArray.length; J ++){
                如果(阵列[我] == lookupArray [J]){
                    的System.out.println(NA);
                }其他{
                    的System.out.println(数组[我]);
                }
            }
        }
    }
    公共静态无效的主要(字符串ARGS []){
        String []数组= ARGS [0];
        的String [] = lookupArray ARGS [1];
        findNa(数组,lookupArray);    }
}


解决方案

  String []数组= ARGS [0];

您要设置一个数组等于一个字符串( ARGS [0] 返回字符串有的0阵列内的索引)。

同样在这里:

 的String [] = lookupArray ARGS [1];

如果您想输入2个数组,然后检查他们是平等的尝试是这样的:

 进口java.util.Scanner中;类naFam {
    静态无效findNa(String []数组,字符串[] lookupArray){
        布尔等于= TRUE;
        的for(int i = 0; I< array.length,我++){
            如果(阵列[I]!= lookupArray [I]){
                等于= FALSE;
            }
        }
        如果(等于==真){
             的System.out.println(NA);
        }其他{
            的System.out.println(数组[0]);
        }
    }    公共静态无效的主要(字串[] args){
        //创建一个新的扫描仪
        扫描程序扫描=新的扫描仪(System.in);
        //创建他们想输入的数组的大小的整数
        INT尺寸1;
        //输入的大小
        尺寸1 = scan.nextInt();
        //创建并输入数组
        的String [] = arrayOne新的String [尺寸1];
        的String [] = arrayTwo新的String [尺寸1];        的for(int i = 0; I<尺寸1,我++){
            arrayOne [I] = scan.next();
        }
        的for(int i = 0; I<尺寸1,我++){
            arrayTwo [I] = scan.next();
        }
        findNa(arrayOne,arrayTwo);
    }
}

没有测试它尚未但它应该工作。

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);

    }
}

解决方案

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.

这篇关于Java的ARGS不兼容的类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆