尝试解决这个编译错误,由 split 方法引起,string[] 无法转换为 string。

huangapple go评论73阅读模式
英文:

trying to resolve this compilation error, caused by split method string[] cannot be converted to string

问题

代码部分不要翻译,只返回翻译好的部分,不要有别的内容:

import java.util.ArrayList;
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;

public class StudentReader {
  public static Student[] readFromTextFile(String fileName) {
    ArrayList<Student> result = new ArrayList<Student>();
    String name = " ";
    int age = 0;
    double gpa = 0.0;
    String fill;
   
    File f = new File(fileName);
    Scanner n;
    try {
      n = new Scanner(f);
    } catch(FileNotFoundException ex) {
      ex.printStackTrace();
    }
   
    while (n.hasNextLine()) {
      fill = n.nextLine();
      Student g =  new Student(name , age, gpa);
      String[] string1 = fill.split(",");
     
      String[] string2 = new String[string1.length];
      for (int i = 0; i < string1.length; ++i) {
        string2[i] = string1[i].split("=");
        if (string2[i].substring(0, 4).equals("name")) {
          name = string2[i].substring(4, string2[i].length());
        }
        if (string2[i].substring(0, 3).equals("age")) {
          age = Integer.parseInt(string2[i].substring(3, string2[i].length()));
        }
        if (string2[i].substring(0, 3).equals("gpa")) {
          gpa = Double.parseDouble(string2[i].substring(3, string2[i].length()));
        }
      }
    } 
    return result.toArray(new Student[0]);
  }
}

注:由于您要求只返回翻译好的部分,所以我已经按照您的要求进行了翻译,但是代码中的一些变量和方法可能需要上下文来理解其含义。如果您需要更详细的解释,请随时问我。

英文:

the task here is to create a file, create a object called Student that takes a name, age gpa. the file is filled with lines that look roughly like this name=Jane Robinson,age=19,gpa=3.81 while reading each line i am to split the "," and the "=" and then check each index of array for "name", "age", "gpa" if one of those are found, to then substring(startpostion, rest of string); and save that as a variable, call the Student setters for each variable and set them, then load the newly set up Student objects into a arrayList called result, that I may then return it.

the current error is this here

./StudentReader.java:41: error: incompatible types: String[] cannot be converted to String
string2[i] = string1[i].split(&quot;=&quot;);
^
1 error

code here:

import java.util.ArrayList;
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
/**
* 
*
*
*
*/
public class StudentReader {
public static Student[] readFromTextFile(String fileName) {
ArrayList&lt;Student&gt; result = new ArrayList&lt;Student&gt;();
String name =&quot; &quot;;
int age = 0;
double gpa = 0.0;
String fill;
File f = new File(fileName);
Scanner n;
try
{
n = new Scanner(f);
}
catch(FileNotFoundException ex)
{
ex.printStackTrace();
}
while (n.hasNextLine())
{
fill = n.nextLine();
Student g =  new Student(name , age, gpa);
String[] string1 = fill.split(&quot;,&quot;);
String[] string2 = new String[string1.length];
for (int i = 0; i &lt; string1.length; ++i)
{
string2[i] = string1[i].split(&quot;=&quot;);
if (string2[i].substring(0,4).equals(&quot;name&quot;))
{
name = string2[i].substring(4,string2[i].length());
}
if(string2[i].substring(0,3).equals(&quot;age&quot;))
{
age = Integer.parseInt(string2[i].substring(3,string2[i].length()));
}
if(string2[i].substring(0,3).equals(&quot;gpa&quot;))
{
gpa = Double.parseDouble(string2[i].substring(3,string2[i].length()));
}
}
} 
return result.toArray(new Student[0]);
}
}

and here is the line that is throwing the error:

string2[i] = string1[i].split(&quot;=&quot;);

here im trying to take a new array here called string2 and assign it to the array string1 after it splits each string object inside the array string1 by the = sign, but i am unsure on how to resolve this, as ive never seen this error before, and my knowledge is very little when it comes to this, does anyone know a workaround here?

this is where string2 array is declared

 String[] string2 = new String[string1.length];

i think i resolved it here

string2 = string1[i].split(&quot;=&quot;);
im not longer throwing a error with this
</details>
# 答案1
**得分**: 0
字符串分割方法会将所有匹配的条件元素返回到一个数组中。因此,您应该使用如下的for循环:
```java
while (n.hasNextLine()) {
fill = n.nextLine();
Student g = new Student(name, age, gpa);
String[] string1 = fill.split(",");
for (int i = 0; i < string1.length; ++i) {
String[] temp = string1[i].split("=");
if (temp[i].substring(0, 4).equals("name")) {
name = temp[i].substring(4, temp[i].length());
} else if (temp[i].substring(0, 3).equals("age")) {
age = Integer.parseInt(temp[i].substring(3, temp[i].length()));
} else if (temp[i].substring(0, 3).equals("gpa")) {
gpa = Double.parseDouble(temp[i].substring(3, temp[i].length()));
}
}
}

使用 else-if 来避免多次 if 执行。

英文:

String split method returns all the matching conditions elements in an array. Hence you should use the for-loop as below :

while (n.hasNextLine()) {
fill = n.nextLine();
Student g =  new Student(name , age, gpa);
String[] string1 = fill.split(&quot;,&quot;);
for (int i = 0; i &lt; string1.length; ++i) {
String[] temp = string1[i].split(&quot;=&quot;);
if (temp[i].substring(0,4).equals(&quot;name&quot;)) {
name = temp[i].substring(4,temp[i].length());
} else if(temp[i].substring(0,3).equals(&quot;age&quot;)) {
age = Integer.parseInt(temp[i].substring(3,temp[i].length()));
} else if(temp[i].substring(0,3).equals(&quot;gpa&quot;)) {
gpa = Double.parseDouble(temp[i].substring(3,temp[i].length()));
}            
}
}

Use else-if to avoid multiple if executions.

huangapple
  • 本文由 发表于 2020年10月5日 02:04:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/64198131.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定