I have to find out how many times the

createPerson()

function is called

This is my Main.java file

import java.io.IOException;


public class Main {
	public static void main(String[] args) throws IOException{
		int i;
		Student[] totalStudents = new Student[10];
		 Student.create3Persons();
		  Student.create2Persons();
	}

}

this is my student.java file

import java.io.*;
public class Student {
	
	
	private static void createPerson() throws IOException{
		int number=0;
		File file = new   File("arg.txt", null);
		FileOutputStream fos = new FileOutputStream(file);
		DataOutputStream dos = new DataOutputStream(fos);
		
		FileInputStream fis = new FileInputStream(file);
		DataInputStream dis = new DataInputStream(fis);
		while(dis.readInt()!= -1)
		{
			number++;
			dos.writeInt(1);
		}
	}
	
	 static void create2Persons() throws IOException{
		Student.createPerson();
		Student.createPerson();
	}
	
	static void create3Persons() throws IOException{
		Student.createPerson();
		Student.createPerson();
		Student.createPerson();
	}
}

What is wrong in this code?

i haven't played with files for a long time but i believe your problem is the opening of the same file twice, once as readonly and once as write only. perhaps finding a way to open the file only once as read&write would help you do.... whatever it is you're trying to do!

but i could be wrong.

i personally don't understand the point of calling 5 times the same function , which does the exact same thing everytime it is called , read ints till the end of the opened file while writing and increasing a number variable that will always only go up as often as the reader reads ints , you're never adding additional ints to the file (considering the reading/writing code would work in the first place)

Edited 4 Years Ago by Philippe.Lahaie: n/a

oh actually it is baseless i know but its given as an exercise for us by our sir to "understand and feel" the importance of static variable :)

the above qn has to be done without using static variable

I have to find out how many times the

createPerson()

function is called

This is my Main.java file

import java.io.IOException;


public class Main {
	public static void main(String[] args) throws IOException{
		int i;
		Student[] totalStudents = new Student[10];
		 Student.create3Persons();
		  Student.create2Persons();
	}

}

this is my student.java file

import java.io.*;
public class Student {
	
	
	private static void createPerson() throws IOException{
		int number=0;
		File file = new   File("arg.txt", null);
		FileOutputStream fos = new FileOutputStream(file);
		DataOutputStream dos = new DataOutputStream(fos);
		
		FileInputStream fis = new FileInputStream(file);
		DataInputStream dis = new DataInputStream(fis);
		while(dis.readInt()!= -1)
		{
			number++;
			dos.writeInt(1);
		}
	}
	
	 static void create2Persons() throws IOException{
		Student.createPerson();
		Student.createPerson();
	}
	
	static void create3Persons() throws IOException{
		Student.createPerson();
		Student.createPerson();
		Student.createPerson();
	}
}

What is wrong in this code?

well if all you have to do is calculate how many times that function is called, create a global integer and initialize it to 0, then in the method put the integrs name and add one to it like so maybe:

public class test {
static int methCount=0;
//... other global variable initializations 
public static void main(String[] args) {
//do whatever you have to here which includes calling the createPerson() method
System.out.println("the method has been called: "+methCount);
}
//method to create person
private void createPerson() 
{
methCount++;//this will add 1 to the global integer methCount each time the method is called
}
}

[EDIT] after reading your other post i see you may not use a static variable so i made this, not optimal though but it does whats asked of it-calculate the amount of times the method is called without a static variable

public class test {
    public static void main(String[] args) {
        int methCount = 0;
//do whatever you have to here which includes calling the createPerson() method
        methCount = methCount + createPerson();
        methCount = methCount + createPerson();
        methCount = methCount + createPerson();
        System.out.println("the method has been called: " + methCount);
    }
//method to create person
    private static int createPerson() {//retirns an integer '1'
        return 1;
    }
}

Edited 4 Years Ago by DavidKroukamp: n/a

Sorry if forgot to add we don't have to use static variable to solve this

Sorry if forgot to add we don't have to use static variable to solve this

see my edited post... and just to clarify: you must not use a static variable? or you dont have to? because if its the latter then my first post of code will be the best

use of static variable is forbidden
actually i want help on my posted code will you please red the code and suggest what is wrong??

use of static variable is forbidden
actually i want help on my posted code will you please red the code and suggest what is wrong??

why are you declaring int number=0; in the method, because if thats what you use to determine how many times the method is called it will just keep resetting it to 0, and how does your 'number' variable declared in the createPerson() method get parsed to another method or the main, which can read the value? or are you using int i=0;? if so how is it been added to each time you call the method... this all applies though to the code i showed you, it was to give you an example.

Edited 4 Years Ago by DavidKroukamp: n/a

the declaration int number;

when we call createPerson() method it writes a char to the specified file but the problem one peer told is the file doesn't get appended so before writing to the file i first read the contents of the file and then increment the number by one

then after i get proper number value then i will make createPerson of return type int and modify the source accordingly

is my thinking correct??

the declaration int number;

when we call createPerson() method it writes a char to the specified file but the problem one peer told is the file doesn't get appended so before writing to the file i first read the contents of the file and then increment the number by one

then after i get proper number value then i will make createPerson of return type int and modify the source accordingly

is my thinking correct??

well then you cannot declare the 'number' variable in the method, it would have to be declared elsewhere, and parsed to the createPerson() method, from there you add it like you are, an then next you return the variable number by:

return number;

but to do this you'd need to make your method like this:

private static int createPerson(int number) {
...
number++;
...
return number;
}

in the above method it accepts an integer as its argument which is what the createPerson() method will use to add a value to the variable 'umber' in order to track the number of calls made to it and it will return this number.
so yes your thinking is on track, just the execution

Edited 4 Years Ago by DavidKroukamp: n/a

well then you cannot declare the 'number' variable in the method, it would have to be declared elsewhere, and parsed to the createPerson() method, from there you add it like you are, an then next you return the variable number by:

return number;

but to do this you'd need to make your method like this:

private static int createPerson(int number) {
...
number++;
...
return number;
}

in the above method it accepts an integer as its argument which is what the createPerson() method will use to add a value to the variable 'umber' in order to track the number of calls made to it and it will return this number.
so yes your thinking is on track, just the execution

doing this implies number is static, which he can't do for this assignement.

doing this implies number is static, which he can't do for this assignement.

not actually please see my first post of code:

public class test {
    public static void main(String[] args) {
        int methCount = 0;
//do whatever you have to here which includes calling the createPerson() method
        methCount = methCount + createPerson();
        methCount = methCount + createPerson();
        methCount = methCount + createPerson();
        System.out.println("the method has been called: " + methCount);
    }
//method to create person
    private static int createPerson() {//retirns an integer '1'
        return 1;
    }
}

or as i described in the post you are refering too:

public class test {
    public static void main(String[] args) {
        int methCount = 0;
//do whatever you have to here which includes calling the createPerson() method
        methCount = createPerson(methCount);
        methCount = createPerson(methCount);
        methCount = createPerson(methCount);
        System.out.println("the method has been called: " + methCount);
    }
//method to create person
    private static int createPerson(int number) {
number++;
        return number;
    }
}

Edited 4 Years Ago by DavidKroukamp: n/a

not actually please see my first post of code:

public class test {
    public static void main(String[] args) {
        int methCount = 0;
//do whatever you have to here which includes calling the createPerson() method
        methCount = methCount + createPerson();
        methCount = methCount + createPerson();
        methCount = methCount + createPerson();
        System.out.println("the method has been called: " + methCount);
    }
//method to create person
    private static int createPerson() {//retirns an integer '1'
        return 1;
    }
}

or as i described in the post you are refering too:

public class test {
    public static void main(String[] args) {
        int methCount = 0;
//do whatever you have to here which includes calling the createPerson() method
        methCount = createPerson(methCount);
        methCount = createPerson(methCount);
        methCount = createPerson(methCount);
        System.out.println("the method has been called: " + methCount);
    }
//method to create person
    private static int createPerson(int number) {
number++;
        return number;
    }
}   public static void main(String[] args) {
        int methCount = 0;
//do whatever you have to here which includes calling the createPerson() method
        methCount = methCount + createPerson(methCount);
        methCount = methCount + createPerson(methCount);
        methCount = methCount + createPerson(methCount);
        System.out.println("the method has been called: " + methCount);
    }
//method to create person
    private static int createPerson(int number) {
number++;
        return number;
    }
}

your second code prints this :

the method has been called: 7

your second code prints this :

the method has been called: 7

no check my edited post you must have got it before i had the chance to edit, take a look there's even double code in the post you quoted, the first part is correct not the other... dont know how that got like that lol

Edited 4 Years Ago by DavidKroukamp: n/a

You can open an output file in append mode.
If you append one byte to the file each time you create an instance, then the length of the file will be the same as the number of instances created.
(You'll have to synchronize the relevant code to avoid thread-related problems)

Edited 4 Years Ago by JamesCherrill: n/a

no check my edited post you must have got it before i had the chance to edit, take a look there's even double code in the post you quoted, the first part is correct not the other... dont know how that got like that lol

perhaps, but OP's initial problem stages a static method from a different class.

your solution of keeping a counter within the main is alright but not if multiple programs are running and accessing createPerson, you will only get each individual program's createPerson count.

perhaps, but OP's initial problem stages a static method from a different class.

your solution of keeping a counter within the main is alright but not if multiple programs are running and accessing createPerson, you will only get each individual program's createPerson count.

yup only if(but the OP has not mentioned multiple instances?)... but true and seen as all my attempts are flawed in your opinion, what would you suggest?

as my only other suggestion then would be to create the counter within a constructor in the class and just have a getNumber() method which will return the integer declared withn the class.

Edited 4 Years Ago by DavidKroukamp: n/a

i don't know, the idea of a text file for 1 single number to replace a static variable is...

way overkill but logical... it does make its point since the assignement is to "show the importance of static variables". But OP will need to figure out file input/output cause he's got it all wrong at the moment. Not sure if he should also think about program threads fighting for the file. I honestly haven't played with file I/O in too long to give him any valuable help.

I'm just writing in java forums for fun while waiting for someone to fall on my css thread :'(

This question has already been answered. Start a new discussion instead.