-1

Hi am having trouble with why this error coming up on my code.

Exception in thread "main" java.lang.Error: Unresolved compilation problems: Type mismatch: cannot convert from String to int This is my code:

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

I cant understand why its doing this. i can imagine it being something simple that am just not seeing fs.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
sin lin
  • 25
  • 1
  • 2
  • 1
    1) Don't use `==` to compare `Strings`. Use `.equals()`. 2) `nextLine()` returns a `String` and you are trying to resolve it to an int. Use `nextInt()` instead – GBlodgett Oct 10 '18 at 18:43
  • Follow up links: https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java and https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#nextInt() – GBlodgett Oct 10 '18 at 18:44

3 Answers3

2

Use

score[0] = scan.nextInt();

instead of

score[0] = scan.nextLine();

as score[] is an array of int[].

Also compare strings using .equals. Change opt == "Home teams" to opt.equals("Home teams")

Nicholas K
  • 15,148
  • 7
  • 31
  • 57
0

You're expecting strings in the input, and trying to convert it to an integer. You can fix this by doing scan.nextInt(), or doing Integer.valueOf(scan.nextLine()) whenever you call a value expecting an integer

CBredlow
  • 2,790
  • 2
  • 28
  • 47
0

Whenever you use the scanner it automatically thinks it's a string. So instead do this: name[0]= Integer.valueOf(scan.next()); This automatically turns it into a number.

Rebecca
  • 11
  • 3