Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将字符串解析为数组?_Java_Arrays_Parsing - Fatal编程技术网

Java 将字符串解析为数组?

Java 将字符串解析为数组?,java,arrays,parsing,Java,Arrays,Parsing,我试图得到最多25个数字的平均值。现在,我对如何将字符串解析为数组感到困惑。以下是目前的代码: final int SIZE = 25; gradeArray = new double[SIZE]; String s; int numElem = 0; double average = 0; do { s = (String) JOptionPane.showInputDialog("Enter Grade", ""); if (s == null || s == (""))

我试图得到最多25个数字的平均值。现在,我对如何将
字符串
解析为数组感到困惑。以下是目前的代码:

final int SIZE = 25;
gradeArray = new double[SIZE];
String s;
int numElem = 0;
double average = 0;

do {
    s = (String) JOptionPane.showInputDialog("Enter Grade", "");
    if (s == null || s == ("")) {

    } else {
        try {
            grades = Double.parseDouble(s);
            if (grades > SIZE) 

        } catch (NumberFormatException e) {
            JOptionPane.showMessageDialog(null, "Your input must be numeric!",
                    "Bad             Data!", 0);
        }
    }
} while (s != null || !s.equals(""));

SIZE
常量用于测试目的

for(inti=0;i据我所知,您希望将多达25个双常量作为单个字符串读取,并将它们解析为双数组

for(int i=0;i<SIZE;i++)
{
 s = (String)JOptionPane.showInputDialog("Enter Grade","");
    if(s != null && !(s.trim().equals(""))){
        gradeArray[i] = Double.parseDouble(s);
    }
    else{
        gradeArray[i] = 0;
    }

}

double sum=0;
for(int j=0;j<gradeArray.length;j++)
{
      sum+=gradeArray[j];
}


System.out.println("avaerage of grades ="+SIZE+" is ="+(sum/SIZE));
final int SIZE = 25;
gradeArray = new double[SIZE];
String s;
int numElem = 0;
double average = 0;

do {
s = (String) JOptionPane.showInputDialog("Enter Grade", "");
if (s == null || s == ("")) {

} else {
    try {
       // this will find double values in your string but ignores non double values
       Matcher m = Pattern.compile("(?!=\\d\\.\\d\\.)([\\d.]+)").matcher(s);
       int temp = 0;
       while (m.find())
       {
         gradeArray[temp] = Double.parseDouble(m.group(1));
         temp ++;
         if(temp >= SIZE){
             break;
         }
       }

    } catch (NumberFormatException e) {
        JOptionPane.showMessageDialog(null, "Your input must be numeric!",
                "Bad             Data!", 0);
    }
}
} while (s != null || !s.equals(""));

您遇到了什么问题?我看到您有
Double.parseDouble
,因此您确实有一些解析方面的知识。什么对您不起作用?我看不出您在哪里尝试将字符串添加到数组中。但在您的情况下,您不能;您的数组是一个双精度值数组,不能强制转换为双精度值。您最终尝试做什么?如果您想将字符串转换为求和,您已经用行
Double.paraeDouble(s);
line完成了。当我输入该行时,编译器说我不能,因为它是Double[]不是双精度。您可能希望将
while
条件更改为
&
而不是
|
为计数器引入一些变量,然后执行类似
gradeArray[counter++]=grades
…顺便说一句的操作。您的代码质量确实很差(
s==(“”
s!=null | | s.equals(“
),等等)