简体   繁体   中英

error: incompatible types: int cannot be converted to int[] & other errors

import java.util.Scanner;

public class BasketBallChart {
    public static void main (String[] args) {
        Scanner reader = new Scanner(System.in);

        System.out.print("Enter the first names of the 5 players: ");
        String nameString = reader.nextLine();
        String[] name = nameString.split(" ");

        for (int i = 0; i < 5; i++) {
            System.out.print("Enter points earned by " +name[i]+ ": ");
            int pL = reader.nextInt();
            int p[i] = pL;
        }

        for (int j = 0; j < 5; j++) {
            System.out.println(p[j]);
        }
    }
}

So basically the first part asks the user to enter the names of 5 players (with spaces) which then I split into a string array.

The for loop I was trying to make it so I wouldn't have to put 5 separate printlns and inputs, so it would be less lines and look cleaner. So I was trying to make it so each point would be associated with the number of the times the for loop has been run for example, say if "i" was 0 and I had entered Oliver for the first name in the first input, name[0] would be Oliver, which then I wanted it to be like the number of points you enter in the first for loop would then be p[0] therefore associated with Oliver, in such that they're both 0.

I used the second for loop for testing purposes.

I keep getting errors and I am not sure why.

Change your code to

String[] name = nameString.split(" ");
int p[] = new int[5];

for (int i = 0; i < 5 && i < name.length; i++) {  // also check name array length
        System.out.print("Enter points earned by " +name[i]+ ": ");
        int pL = reader.nextInt();
        p[i] = pL;
}

This will move the declaration of the p array to before (and outside) of the for loop. I have also added code to check that the looping does not exceed the length of the name array.

As suggested below by @RolandIllig, the array p would be better of being named as points and the name arrays would be better off named as names - giving variables names which reflect their usage makes for easier to understand code.

Here you have two issues with your code:

1) You need to declare array of p before the loop like int[] p = new int[5];

2) Then inside loop assign value to each array element like p[i] = pL;

3) For being safe please check for length of name array like if (name.length == 5)

    public static void main(final String[] args) {

    final Scanner reader = new Scanner(System.in);

    System.out.print("Enter the first names of the 5 players: ");
    final String nameString = reader.nextLine();
    final String[] name = nameString.split(" ");
    if (name.length == 5) {
      final int[] p = new int[5];
      for (int i = 0; i < 5; i++) {
        System.out.print("Enter points earned by " + name[i] + ": ");
        final int pL = reader.nextInt();
        p[i] = pL;
      }

      for (int j = 0; j < 5; j++) {
        System.out.println(p[j]);
      }
    }

  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM