I have two erros which I have tried everything to fix them

C:\Documents and Settings\java:14: array required, but java.lang.String found
        ID[count] = ID;
          ^
C:\Documents and Settings\java:15: array required, but int found
        mark[count] = mark;

heres the code

class Module
 {
     String[] ID = new String[10];
      int[] score = new int[10];

    int setID(String ID, int mark, int count){
        //id, mark and count get passed from the main method
        ID[count] = ID;
        score[count] = mark;


       }

Dani AI

Generated

The compiler messages mean the code is trying to index something that is not an array. That typically happens when a method parameter or local variable uses the same name as an instance array, so the parameter "shadows" the field. As noted, types must match, but the real root here is the name collision. was right to ask for full code — the call sites and method signatures also reveal mismatches that will cause more compile errors.

Two straightforward fixes:

  • Rename the method parameters so they do not hide the arrays:
class Module {
  private String[] ids = new String[10];
  private int[] marks = new int[10];

  void store(int index, String studentId, int studentMark) {
    ids[index] = studentId;
    marks[index] = studentMark;
  }
}
  • Or explicitly refer to instance fields with this. so the compiler knows you mean the arrays, not the local variables:
class Module {
  private String[] idArray = new String[10];
  private int[] scoreArray = new int[10];

  void put(int i, String sid, int score) {
    this.idArray[i] = sid;
    this.scoreArray[i] = score;
  }
}

Also check these common additional problems visible from the snippets: make sure a constructor exists if you call new Module(...), and make method return types match what you actually return (a void method should not be used where a value is printed). After fixing names/signatures, recompile and address any remaining errors.

For a concise explanation of variable scope and shadowing, see the Oracle Java tutorial on variables: Java variables (Oracle tutorial).

Recommended Answers

All 3 Replies

Would you mind posting the full code in the code brackets so I can get a feel for what your trying to do?

heres the main method then

import javax.swing.*;


public class Test{
/** Main method */
public static void main(String[] args) {


for(int count = 0;count<10;count++){
String ID = JOptionPane.showInputDialog(null,
"Enter the Students ID",
"Programming 2",
JOptionPane.QUESTION_MESSAGE);


String num = JOptionPane.showInputDialog(null,
"Enter the Students score",
"Programming 2",
JOptionPane.QUESTION_MESSAGE);


int mark = Integer.parseInt(num);



Module withData = new Module(ID, mark, count);


System.out.println("Student data   = "+ withData.setID());



}

The elements of ID, mark or whatever have to be of the same type as what those arrays are...

String[] ID = new String[12];

int[] mark = new int[12];

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.