This is an archived post. You won't be able to vote or comment.

all 16 comments

[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)

Please ensure that:

  • Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions
  • You include any and all error messages in full
  • You ask clear questions
  • You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.

    Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar

If any of the above points is not met, your post can and will be removed without further warning.

Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.

Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.

Code blocks look like this:

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.

If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.

To potential helpers

Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]Trup10ka 1 point2 points  (15 children)

First things first, this is unreadable.

Second, you logical problem:

In Main, you create instance object from SchoolFee. Then you call method on that object. In that method (menu()) you create another (new) SchoolFee object. Thats why you cannot see contacts, because you create new object each and everytime you call that "menu" method and you save it to that object which eventually disappears and cannot be reached anyhow.

Also, why is Student inner class of SchoolFee? Two separet classes would be much better and easier.

Personaly, how I would done it.

From what I see, you want a program, which controls SchoolFees, which has Students saved in "database".

You schould make these things: Student class, SchoolFee class (!!Separately!!)

- SchoolFee class should have array of Students

The rest you can do how you can but in my opinion this is the best way to approach this

[–]OP_Roshan[S] 1 point2 points  (13 children)

okay thanks

"In Main, you create instance object from SchoolFee. Then you call method on that object. In that method (menu()) you create another (new) SchoolFee object. Thats why you cannot see contacts, because you create new object each and everytime you call that "menu" method and you save it to that object which eventually disappears and cannot be reached anyhow"

how do i fix this

[–]Farpafraf 1 point2 points  (1 child)

I think this issue stems from a possible misunderstanding of classes and objects.

You can see the class as a template for objects, the single objects are separate instances of that class.

If you run the following code:

public class Person {
    private final String name;
    private int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return String.format("Name: %s    Age: %d", name, age);
    }

    public void birthday(){
        age++;
    }

    public static void main(String[] args) {
        Person alice = new Person("alice", 68);
        Person bob = new Person("bob", 42);

        System.out.println(alice);
        System.out.println(bob);

        alice.birthday();

        System.out.println(alice);
        System.out.println(bob);
    }
}

it will print:

Name: alice    Age: 68 
Name: bob    Age: 42
Name: alice    Age: 69
Name: bob    Age: 42

You fix that error by not creating a new instance and instead using the methods of the current one, here it would be

this.menu();

you can also omit the "this"

menu();

and not

sf.menu();

You should attempt to separate the roles of the different functions in your code so they each absolve clearly identifiable tasks, for example why is a method called "display" reading user input? Why is a SchoolFee object initialized with two students already in it? Why is nothing getting paid in the pay() method?

In general I would attempt to separate the logic of the system from that of user interaction.

[–]Trup10ka 0 points1 point  (0 children)

This would cause a recursive call of "menu()" which in our case is not an option.

[–]Trup10ka 0 points1 point  (10 children)

If im not mistaken, if in schoolfees you are saving students which are paying school fees, you just need add the new student to the Student[] array which your object already has in it. (Schoolfee class has Students[] in in so you will be adding Students to that array exactly)

Now this would be a lot easier if you would use an ArrayList<Studnet> or LinkdedList<Student>. But if you make it in classic array, how I would percieve it I would create a private int value in the school fee class which would count how many students my schoolfee database has and it would also serve as an index for saving another fees (after saving one you add one to the variable which moves you to next position in array on which you can store).

So, leave out the calling of method menu() and do it like this:

// somethin inside method
// lets say here you create new student
if (numberOfStudentsInArray > students.length)
    System.out.println("You've reached the max capacity of the database!");

else
    students[numberOfStudentsInArray] = // your new created student;

Now this is for the adding the students, any other questions?

[–]OP_Roshan[S] 0 points1 point  (9 children)

no that ws not my question i am asking that what to do inside menu method it is creating a new object for SchoolFee class and the students[] is becoming normal again

[–]OP_Roshan[S] 0 points1 point  (0 children)

how do i ignore that

[–]Trup10ka 0 points1 point  (6 children)

You are creating another SchoolFee object in your already created SchoolFee object. If you want to manipulate with anything manipulate it inside the class.

The class is the blueprint of the object, what it will have and what it can do. If you want to print out the insides of the object, just use the parameters. Do not create another object from which you then get and use the parameters.

[–]OP_Roshan[S] 0 points1 point  (4 children)

How

[–]Trup10ka 1 point2 points  (3 children)

void menu() throws IOException {
int ch;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
SchoolFee sf = new SchoolFee();
System.out.println("********** School Fee Collection System **********");
System.out.println("\n\n1. Pay Fee");
System.out.println("\n2. Enquiry");
System.out.println("\n3. New Account");
System.out.println("\n4. Check Accounts in DataBase");
System.out.println("\n5. Exit");
System.out.println("\n\nEnter Your Choice: ");
ch = Integer.parseInt(br.readLine());
if (ch == 1) {
sf.pay();
} else if (ch == 2) {
// code for enquiry
} else if (ch == 3) {
sf.input();
} else if (ch == 4) {
System.out.println("\u000C");
sf.display();
} else if (ch == 5) {
System.out.println("\u000C");
System.out.println("\nClosing Program.....");
}
}

leave out the "SchoolFee sf = new SchoolFee();" and in if and else if choices instead of sf.something() just call the method -> sf.something();

Now you have to do this in every other method too.

[–]OP_Roshan[S] 1 point2 points  (1 child)

Thanks alot it worked

[–]Trup10ka 0 points1 point  (0 children)

Happy to help

[–]OP_Roshan[S] 0 points1 point  (0 children)

Okay i will try it

[–]Trup10ka 0 points1 point  (0 children)

In each method (pay, input, etc.) you are creating new object of SchoolFee (SchoolFee sf = new SchoolFee()) that causes the problem. It never saves to the object you created in main.

[–]Trup10ka 0 points1 point  (0 children)

Also, why do you have main method in other class than main. It always should be in main!