Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.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
类型不匹配:无法从字符串转换为int-java_Java_Arrays_String_Compiler Errors_Int - Fatal编程技术网

类型不匹配:无法从字符串转换为int-java

类型不匹配:无法从字符串转换为int-java,java,arrays,string,compiler-errors,int,Java,Arrays,String,Compiler Errors,Int,嗨,我很难理解为什么我的代码会出现这个错误 线程“main”java.lang中出现异常。错误:未解决的编译问题: 类型不匹配:无法从字符串转换为int 这是我的代码: import java.util.*; public class matchScore{ public static void main(String[] args){ String opt; String home_team_name; String away_team_name; i

嗨,我很难理解为什么我的代码会出现这个错误

线程“main”java.lang中出现异常。错误:未解决的编译问题: 类型不匹配:无法从字符串转换为int 这是我的代码:

import java.util.*;

public class matchScore{

  public static void main(String[] args){

    String opt;
    String home_team_name;
    String away_team_name;
    int home_team_score;
    int away_team_score;

    String[] name = new String[10];
    int[] score = new int[10];

    System.out.println("Retype an option:\n");
    System.out.println("Home teams");
    System.out.println("Away teams");
    System.out.println("Outputs");

    Scanner scan = new Scanner(System.in);

    opt = scan.nextLine();

    if(opt == "Home teams"){
      System.out.println("Entre first home team name: ");
      name[0] = scan.nextLine();
      System.out.println("Entre first home team score: ");
      score[0] = scan.nextLine();

      System.out.println("Entre second home team name: ");
      name[1] = scan.nextLine();
      System.out.println("Entre second home team score: ");
      score[1] = scan.nextLine();

      System.out.println("Entre third home team name: ");
      name[2] = scan.nextLine();
      System.out.println("Entre third home team score: ");
      score[2] = scan.nextLine();

      System.out.println("Entre forth home team name: ");
      name[3] = scan.nextLine();
      System.out.println("Entre forth home team score: ");
      score[3] = scan.nextLine();

      System.out.println("Entre fifth home team name: ");
      name[4] = scan.nextLine();
      System.out.println("Entre fifth home team score: ");
      score[4] = scan.nextLine();

      System.out.println("Entre sixth home team name: ");
      name[5] = scan.nextLine();
      System.out.println("Entre sixth home team score: ");
      score[5] = scan.nextLine();

      System.out.println("Entre seventh home team name: ");
  name[6] = scan.nextLine();

      System.out.println("Entre seventh home team score: ");
      score[6] = scan.nextLine();
    }
  }
}
我不明白它为什么这样做。我可以想象这是一件简单的事情,我只是没有看到fs。

使用

score[0] = scan.nextInt();
而不是

score[0] = scan.nextLine();
as score[]是int[]的数组

还可以使用
.equals
比较字符串。改变
opt==“主队”
opt.equals(“主队”)
使用

score[0] = scan.nextInt();
而不是

score[0] = scan.nextLine();
as score[]是int[]的数组

还可以使用
.equals
比较字符串。改变
opt==“Home teams”
opt.equals(“Home teams”)
您需要输入字符串,并尝试将其转换为整数。您可以通过执行
scan.nextInt()
,或执行
Integer.valueOf(scan.nextLine())
来解决此问题。每当调用需要整数的值时,输入中需要字符串,并尝试将其转换为整数。您可以通过执行
scan.nextInt()
,或执行
Integer.valueOf(scan.nextLine())
来解决此问题,无论何时调用需要整数的值,无论何时使用扫描仪,它都会自动将其视为字符串。因此,我们应该这样做: name[0]=Integer.valueOf(scan.next())
这会自动将其转换为数字。

无论何时使用扫描仪,它都会自动将其视为字符串。因此,我们应该这样做: name[0]=Integer.valueOf(scan.next())
这会自动将其转换为一个数字。

1)不要使用
=
来比较
字符串。使用
.equals()
。2)
nextLine()
返回一个
字符串,您试图将其解析为int。请使用
nextInt()
代替后续链接:1)不要使用
=
来比较
字符串。使用
.equals()
。2)
nextLine()
返回一个
字符串
,您正试图将其解析为int。使用
nextInt()
代替后续链接:这会起作用,但扫描.nextInt()更好,正如GBlodgett在其注释中提到的。这会起作用,但扫描.nextInt()更好,正如GBlodgett在其注释中提到的那样。