I can not increase the studentid value in For

Jon Andrew :

I am doing some coding in Java, but it doesn't work:

import java.util.Scanner;

import java.util.Arrays;

public class Main {

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

        String [] studentId = new String [3];

        Student name;

        for (int i = 0; i < 3; i++) 
        {
        System.out.print("Enter Name => ");
        name = new Student(input.next());
        studentId[i] = name.toString();
        }
        for (int i = 0; i < 3; i++) 
        {
        System.out.println(studentId[i]);
        }    
}
}
public class Student
{
    private static int studentId;
    private String name;

    public Student(String name) 
    {
    this.name=name;    
    setStudentId(studentId++);
    }

    public int getStudentId() 
    {
    return studentId;
    }

    public void setStudentId(int studentId) 
    {
    this.studentId = studentId;
    }

    @Override
    public String toString()
    {
        return (this.studentId +" "+ "student is "+ " " + "(" + this.studentId + ")"+ this.name);
    }
}

I need to auto-increment the Id when a new entry is created. I try everything but still cant increased it.

PhuLuong :

In constructor it should be:

{
    public Student(String name) 
    {
        this.name=name;    
        setStudentId(studentId + 1);
    }
}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=403972&siteId=1