Java 参数 [变量] 的非法修饰符;只允许final

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18794010/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 11:07:19  来源:igfitidea点击:

Illegal modifier for parameter [variable]; only final is permitted

java

提问by iShaalan

I am trying to write a method that gets a string of numbers "123188"

我正在尝试编写一个获取一串数字“123188”的方法

then returns an int[]which contains digits.

然后返回int[]包含数字的 。

that what I've got so far :

到目前为止我所拥有的:

public int[] stringToDig(String a)
{
  char [] ch1 = a.toCharArray();
  int [] conv = new int [ch1.length];

  for (int i=0 ; i<ch1.length ; i++)
      conv[i] = Character.getNumericValue(ch1[i]);

  return conv; 
}

and I got that

我明白了

Multiple markers at this line:

  • Syntax error on token "(", ; expected
  • Syntax error on token ")", ; expected
  • Illegal modifier for parameter stringToDig; only final is permitted

此行的多个标记:

  • 标记 "(", ; 预期的语法错误
  • 标记“)”上的语法错误, ; 预期的
  • 参数 stringToDig 的非法修饰符;只允许final

采纳答案by Cruncher

You can't put methods inside of other methods in Java.

您不能将方法放在 Java 中的其他方法中。

Structure your program like this:

像这样构建你的程序:

public class Test
{
    public static void main(String[] args)
    {
        int[] digits = stringToDig("54235");
    }

    public int[] stringToDig(String a)
    {
        char [] ch1 = a.toCharArray();
        int [] conv = new int [ch1.length];

        for (int i=0 ; i<ch1.length ; i++)
            conv[i] = Character.getNumericValue(ch1[i]);

        return conv; 
    }
}

回答by óscar López

Try this:

尝试这个:

public int[] stringToDig(String a) {

  char[] ch1 = a.toCharArray();
  int[] conv = new int[ch1.length];

  for (int i = 0; i < ch1.length; i++)
    conv[i] = Character.digit(ch1[i], 10); // here's the difference

  return conv; 

}

The preferred method for converting a character digit to the corresponding intvalue is by using the digit()method. Here's another option, although it's a bit of a hack and will only work for digits in base 10 (unlike using digits(), which works for other bases):

将字符数字转换为相应int值的首选方法是使用该digit()方法。这是另一种选择,虽然它有点小技巧,并且只适用于以 10 为基数的数字(与 using 不同digits(),它适用于其他基数):

conv[i] = ch1[i] - '0';

Besides that, @Cruncher is right - the errors shown seem to indicate that you forgot to put the method insidea class! follow his advice.

除此之外,@Cruncher 是对的 - 显示的错误似乎表明您忘记将该方法放入类中!听从他的建议。

回答by kelunik

This code works for me:

这段代码对我有用:

public int[] stringToDig(String a) {
    char[] chars = a.toCharArray();
    int[] ints = new int[chars.length];

    for (int i = 0; i < chars.length; i++)
        ints[i] = chars[i] - 48;

    return ints;
}

回答by SAnDAnGE

import java.util.Arrays;

public class NewClass {

public static void main(String[] args) {
    System.out.println(Arrays.toString(stringToDig("123456789")));
}

public static int[] stringToDig(String a) {
    int [] c = new int[a.length()];
    for (int i = 0; i < c.length; i++) {
        c[i] = Integer.parseInt(String.valueOf(a.charAt(i)));
    }
    return c;
} 

}

导入 java.util.Arrays;

公共类新类{

public static void main(String[] args) {
    System.out.println(Arrays.toString(stringToDig("123456789")));
}

public static int[] stringToDig(String a) {
    int [] c = new int[a.length()];
    for (int i = 0; i < c.length; i++) {
        c[i] = Integer.parseInt(String.valueOf(a.charAt(i)));
    }
    return c;
} 

}