Home > Mobile >  how do I fix fix the Identifier issue with this code?
how do I fix fix the Identifier issue with this code?

Time:02-27

public class Patients{
public String Patients;
public int IDNumber;


 Patients(IDNumber){
  this.IDNumber = IDNumber;
   for(int i = 0; IDNumber.length; i  ){
  System.out.println("patient"   i);
  }
 }
}

-calls patients and IDNumber to give a patient an ID number. How do I fix the identifier error here?

CodePudding user response:

You have declared IDNumber as an int, and ints in Java are not objects, and don't have attribute length. I'm not sure on what you want to do with your code, but try to go with:

for(int i = 0; i < IDNumber; i  )

CodePudding user response:

There are many errors in this code:

public class Patients{
public String Patients;
public int IDNumber;


 Patients(IDNumber){
  this.IDNumber = IDNumber;
   for(int i = 0; IDNumber.length; i  ){
  System.out.println("patient"   i);
  }
 }
}

A lot of the following you probably won't understand (yet).

  1. Readability concerns:

    • The indentation is a mess.
    • The whitespace usage is not conformant to style guides.

    OK, the compiler won't care, but other people reading your code will. And if you don't care about readability and maintainability of your code, then you have probably chosen the wrong programming language to learn.

    From (just!) this perspective, the code should look like

    public class Patients {
        public String Patients;
        public int IDNumber;
    
        Patients(IDNumber) {
            this.IDNumber = IDNumber;
            for (int i = 0; IDNumber.length; i  ) {
                System.out.println("patient"   i);
            }
        }
    }
    
  2. public String Patients;

    • Field names should start with a lowercase letter.
    • A field that has the same name as the class is asking for trouble.
    • Fields should not be public because it leads to porous abstractions. In most cases fields should be declared as private.
    • This particular field is not used in your code.
    • The field name Patients (or patients) is incorrect in the sense that it doesn't give any real idea what the String really represents.
  3. public int IDNumber;

    • The field name and access modifier are incorrect; see 2.
    • The type of the field doesn't match how you are trying to use it; see 5.
  4. Patients(IDNumber){

    • This should be public ... unless your intention is to hide the constructor.
    • Method and constructor parameter names should start with a lowercase letter.
    • The parameter declaration is missing the parameter type.
  5. for(int i = 0; IDNumber.length; i ){

    • IDNumber.length needs to be a boolean expression
    • assuming that IDNumber is an int, int values don't have any fields, so IDNumber.length is invalid.
    • if IDNumber is supposed to be an "array of integers", that does have a length field, but then IDNumber.length will be an integer not a boolean. Maybe you should be writing i < IDNumber.length there?
  6. System.out.println("patient" i);

    • It does not make sense to do this kind of thing in a constructor. Do you really need to write stuff the console every time you create a Patients object? Shouldn't this code (the loop!) be somewhere else? Like in a print or toString() method?
  7. public class Patients{

    • On reading and thinking about the code in its totality, it is unclear whether you intend a single instance of this class to represent one patient or a collection of patients. In the former case that class name is incorrect. In the latter case, the fields are incorrect.

This is the question you actually asked:

-calls patients and IDNumber to give a patient an ID number.

The code you have shown us doesn't do (just) that. It also seems to be trying to print stuff.

How do I fix the identifier error here?

It is not clear which specific identifier error you are talking about, so it is hard to understand what you are specifically asking.

However, as you should have realized by now, the problems with this code cannot be resolved by simply "fixing an identifier error". You need to start by understanding the problem that you have been given. Start by figuring out what this Patients class is actually supposed to represent.

CodePudding user response:

You are trying to call the "length" method on an int object. Try changing it the argument in the for loop.

  •  Tags:  
  • java
  • Related